aboutsummarylogtreecommitdiffstats
path: root/nerdfonts_installer.sh
blob: 8299b75a0d7912d774d9568d2d6fb99110ea9a9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/bin/bash -e

# Function to detect the OS and set the package manager
detect_os_and_set_package_manager() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        case "$ID" in
            ubuntu|debian)
                PKG_MANAGER="sudo apt-get update && sudo apt-get install -y"
                ;;
            fedora)
                PKG_MANAGER="sudo dnf install -y"
                ;;
            centos|rhel)
                PKG_MANAGER="sudo yum install -y"
                ;;
            arch)
                PKG_MANAGER="sudo pacman -Syu --noconfirm"
                ;;
            *)
                printf "%b\n" '\033[0;31mUnsupported OS: '"$ID"'\033[0m'
                exit 1
                ;;
        esac
    else
        printf "%b\n" '\033[0;31mOS detection failed. Please install curl, tar, and fontconfig manually.\033[0m'
        exit 1
    fi
}

# Function to check and install dependencies
install_dependencies() {
    if ! command -v curl >/dev/null 2>&1; then
        printf "%b\n" '\033[0;33mcurl not found. Installing curl...\033[0m'
        $PKG_MANAGER curl
    fi

    if ! command -v unzip >/dev/null 2>&1; then
        printf "%b\n" '\033[0;33munzip not found. Installing unzip...\033[0m'
        $PKG_MANAGER unzip
    fi

    if ! command -v fc-cache >/dev/null 2>&1; then
        printf "%b\n" '\033[0;33mfontconfig (fc-cache) not found. Installing fontconfig...\033[0m'
        $PKG_MANAGER fontconfig
    fi
}

# Detect OS and set package manager, and then check and install dependencies
detect_os_and_set_package_manager
install_dependencies

# Create directory for fonts
mkdir -p "$HOME/.local/share/fonts"

# Create tmp directory if it doesn't exist
mkdir -p "$HOME/tmp"

# List of available fonts using release page names
fonts=(
"0xProto"
"3270"
"Agave"
"AnonymousPro"
"Arimo"
"AurulentSansMono"
"BigBlueTerminal"
"BitstreamVeraSansMono"
"CascadiaCode"
"CascadiaMono"
"CodeNewRoman"
"ComicShannsMono"
"CommitMono"
"Cousine"
"D2Coding"
"DaddyTimeMono"
"DejaVuSansMono"
"DroidSansMono"
"FantasqueSansMono"
"FiraCode"
"FiraMono"
"GeistMono"
"Go-Mono"
"Gohu"
"Hack"
"Hasklig"
"Hermit"
"iA-Writer"
"IBMPlexMono"
"Inconsolata"
"InconsolataGo"
"InconsolataLGC"
"IntelOneMono"
"Iosevka"
"IosevkaTerm"
"IosevkaTermSlab"
"JetBrainsMono"
"Lekton"
"LiberationMono"
"Lilex"
"MartianMono"
"Meslo"
"Monaspace"
"Monofur"
"Monoid"
"Mononoki"
"MPlus"
"NerdFontsSymbolsOnly"
"Noto"
"OpenDyslexic"
"Overpass"
"ProFont"
"ProggyClean"
"Recursive"
"RobotoMono"
"ShareTechMono"
"SourceCodePro"
"SpaceMono"
"Terminus"
"Tinos"
"Ubuntu"
"UbuntuMono"
"UbuntuSans"
"VictorMono"
"ZedMono"
)

# Display menu of available fonts in multiple columns based on terminal width
print_fonts_in_columns() {
    cols=$(tput cols)
    items_per_col=20
    total_fonts=${#fonts[@]}
    columns=$((total_fonts / items_per_col))
    if ((total_fonts % items_per_col != 0)); then
        columns=$((columns + 1))
    fi

    for ((i=0; i<items_per_col; i++)); do
        for ((j=0; j<columns; j++)); do
            idx=$((i + j * items_per_col))
            if ((idx < total_fonts)); then
                printf "%-30s" "$((idx + 1)). ${fonts[idx]}"
            fi
        done
        echo
    done
}

# Display the font list
printf "%b\n" '\033[0;32mSelect fonts to install (separate with spaces, or enter "all" to install all fonts):\033[0m'
printf "%b\n" "---------------------------------------------"
print_fonts_in_columns
printf "%b\n" "---------------------------------------------"

# Prompt user to select fonts and validate input
while true; do
    printf "%b\n" '\033[0;36mEnter the numbers of the fonts to install (e.g., "1 2 3") or type "all" to install all fonts: \033[0m'
    read -r font_selection

    # Check if user selected "all"
    if [ "$font_selection" == "all" ]; then
        selected_fonts=("${fonts[@]}")  # Set all fonts
        break
    elif [ -n "$font_selection" ]; then
        selected_fonts=()
        for selection in $font_selection; do
            font_index=$((selection - 1))  # Adjust for zero-based indexing
            if ((font_index >= 0 && font_index < total_fonts)); then
                selected_fonts+=("${fonts[font_index]}")
            else
                printf "%b\n" '\033[0;31mInvalid selection: '"$selection"'\033[0m'
                continue 2
            fi
        done
        break  # Exit loop if input is valid
    else
        printf "%b\n" '\033[0;31mPlease select at least one font.\033[0m'
    fi
done

# Download and install selected fonts
for font in "${selected_fonts[@]}"; do
    printf "%b\n" '\033[0;34mDownloading and installing '"$font"'\033[0m'
    font_name=$(printf "%b\n" "$font" | awk '{print $1}')
    curl -sSLo "$HOME/tmp/$font_name.zip" "https://github.com/ryanoasis/nerd-fonts/releases/latest/download/$font_name.zip"
    unzip -o "$HOME/tmp/$font_name.zip" -d "$HOME/.local/share/fonts"
    rm "$HOME/tmp/$font_name.zip"
done

# Update font cache
fc-cache -vf