2018-07-09 16:20:40 +02:00
|
|
|
#!/usr/bin/env bash
|
2016-09-02 10:00:18 +02:00
|
|
|
# Install Nerd Fonts
|
2022-04-22 07:45:16 +02:00
|
|
|
__ScriptVersion="0.4"
|
2015-02-24 17:46:42 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
# Default values for option variables:
|
|
|
|
quiet=false
|
|
|
|
mode="copy"
|
|
|
|
clean=false
|
2022-04-22 07:45:16 +02:00
|
|
|
dry=false
|
2016-09-02 10:00:18 +02:00
|
|
|
extension="otf"
|
2022-04-22 08:05:08 +02:00
|
|
|
patches=("Complete") # There are no other variants then Complete anymore
|
2016-09-02 10:00:18 +02:00
|
|
|
compat=()
|
2017-07-30 05:08:20 +02:00
|
|
|
installpath="user"
|
2015-02-24 17:46:42 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
# Usage info
|
|
|
|
usage() {
|
|
|
|
cat << EOF
|
2022-04-22 07:45:16 +02:00
|
|
|
Usage: ./install.sh [-q -v -h] [[--copy | --link] --clean | --list | --remove]
|
2016-09-02 10:00:18 +02:00
|
|
|
[--use-single-width-glyphs] [--windows] [--otf | --ttf]
|
2017-07-30 05:08:20 +02:00
|
|
|
[--install-to-user-path | --install-to-system-path ]
|
2022-04-22 08:05:08 +02:00
|
|
|
[FONT...]
|
2016-05-09 10:39:54 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
General options:
|
2016-05-09 10:39:54 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
-q, --quiet Suppress output.
|
|
|
|
-v, --version Print version number and exit.
|
|
|
|
-h, --help Display this help and exit.
|
|
|
|
|
|
|
|
-c, --copy Copy the font files [default].
|
|
|
|
-l, --link Symlink the font files.
|
|
|
|
-L, --list List the font files to be installed (dry run).
|
|
|
|
|
|
|
|
-C, --clean Recreate the root Nerd Fonts target directory
|
|
|
|
(clean out all previous copies or symlinks).
|
|
|
|
|
2022-04-22 07:45:16 +02:00
|
|
|
--remove Remove all Nerd Fonts (that have been installed
|
|
|
|
with this script).
|
|
|
|
Can be combined with -L for a dry run.
|
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
-s, --use-single-width-glyphs Install single-width glyphs variants.
|
|
|
|
-w, --windows Install with limited internal font names.
|
|
|
|
|
2017-07-30 05:08:20 +02:00
|
|
|
-U, --install-to-user-path Install fonts to users home font path [default].
|
|
|
|
-S, --install-to-system-path Install fonts to global system path for all users, requires root.
|
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
-O, --otf Prefer OTF font files [default].
|
|
|
|
-T, --ttf Prefer TTF font files.
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
# Print version
|
|
|
|
version() {
|
|
|
|
echo "Nerd Fonts installer -- Version $__ScriptVersion"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Parse options
|
2017-07-30 05:08:20 +02:00
|
|
|
optspec=":qvhclLCsSUwOTAM-:"
|
2016-09-02 10:00:18 +02:00
|
|
|
while getopts "$optspec" optchar; do
|
|
|
|
case "${optchar}" in
|
|
|
|
|
|
|
|
# Short options
|
|
|
|
q) quiet=true;;
|
|
|
|
v) version; exit 0;;
|
|
|
|
h) usage; exit 0;;
|
|
|
|
c) mode="copy";;
|
|
|
|
l) mode="link";;
|
2022-04-22 07:45:16 +02:00
|
|
|
L) dry=true
|
|
|
|
[ "$mode" != "remove" ] && mode="list";;
|
2016-09-02 10:00:18 +02:00
|
|
|
C) clean=true;;
|
|
|
|
s) compat=( "${compat[@]}" "Nerd Font*Mono" );;
|
|
|
|
w) compat=( "${compat[@]}" "Windows Compatible" );;
|
|
|
|
O) extension="otf";;
|
|
|
|
T) extension="ttf";;
|
2017-07-30 05:08:20 +02:00
|
|
|
S) installpath="system";;
|
|
|
|
U) installpath="user";;
|
2016-09-02 10:00:18 +02:00
|
|
|
|
|
|
|
-)
|
|
|
|
case "${OPTARG}" in
|
|
|
|
# Long options
|
|
|
|
quiet) quiet=true;;
|
|
|
|
version) version; exit 0;;
|
|
|
|
help) usage; exit 0;;
|
|
|
|
copy) mode="copy";;
|
|
|
|
link) mode="link";;
|
2022-04-22 07:45:16 +02:00
|
|
|
list) dry=true
|
|
|
|
[ "$mode" != "remove" ] && mode="list";;
|
|
|
|
remove) mode="remove";;
|
2016-09-02 10:00:18 +02:00
|
|
|
clean) clean=true;;
|
|
|
|
use-single-width-glyphs) compat=( "${compat[@]}" "Nerd Font*Mono" );;
|
|
|
|
windows) compat=( "${compat[@]}" "Windows Compatible" );;
|
|
|
|
otf) extension="otf";;
|
|
|
|
ttf) extension="ttf";;
|
2017-07-30 05:08:20 +02:00
|
|
|
install-to-system-path) installpath="system";;
|
|
|
|
install-to-user-path) installpath="user";;
|
2016-09-02 10:00:18 +02:00
|
|
|
*)
|
2022-04-22 08:05:08 +02:00
|
|
|
echo "Unknown option --${OPTARG}" >&2
|
|
|
|
usage >&2;
|
|
|
|
exit 1
|
|
|
|
;;
|
2016-09-02 10:00:18 +02:00
|
|
|
esac;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
echo "Unknown option -${OPTARG}" >&2
|
|
|
|
usage >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
|
|
|
|
esac
|
|
|
|
done
|
2018-01-13 05:20:52 +02:00
|
|
|
shift $((OPTIND-1))
|
2016-09-02 10:00:18 +02:00
|
|
|
|
|
|
|
# Set source and target directories, default: all fonts
|
2017-08-31 23:33:45 +02:00
|
|
|
nerdfonts_root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/patched-fonts"
|
2016-09-02 10:00:18 +02:00
|
|
|
nerdfonts_dirs=("$nerdfonts_root_dir")
|
|
|
|
|
|
|
|
# Accept font / directory names, to avoid installing all fonts
|
2019-08-11 09:13:55 +02:00
|
|
|
if [ -n "$*" ]; then
|
2016-09-02 10:00:18 +02:00
|
|
|
nerdfonts_dirs=()
|
|
|
|
for font in "${@}"; do
|
2019-08-11 09:13:55 +02:00
|
|
|
if [ -n "$font" ]; then
|
2016-09-02 10:00:18 +02:00
|
|
|
# Ensure that directory exists, and offer suggestions if not
|
|
|
|
if [[ ! -d "$nerdfonts_root_dir/$font" ]]; then
|
2018-01-13 05:20:52 +02:00
|
|
|
echo -e "Font $font doesn't exist. Options are: \\n"
|
2016-09-02 10:00:18 +02:00
|
|
|
find "$nerdfonts_root_dir" -maxdepth 1 -type d \( \! -name "$(basename "$nerdfonts_root_dir")" \) -exec basename {} \;
|
2019-07-25 03:08:21 +02:00
|
|
|
exit 255
|
2016-09-02 10:00:18 +02:00
|
|
|
fi
|
|
|
|
nerdfonts_dirs=( "${nerdfonts_dirs[@]}" "$nerdfonts_root_dir/$font" )
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
#
|
|
|
|
# Start constructing `find` expression
|
|
|
|
#
|
|
|
|
implode() {
|
|
|
|
# $1 is return variable name
|
|
|
|
# $2 is sep
|
|
|
|
# $3... are the elements to join
|
|
|
|
local retname=$1 sep=$2 ret=$3
|
|
|
|
shift 3 || shift $(($#))
|
2018-01-13 05:20:52 +02:00
|
|
|
printf -v "$retname" "%s" "$ret${*/#/$sep}"
|
2016-09-02 10:00:18 +02:00
|
|
|
}
|
|
|
|
find_include=
|
|
|
|
find_exclude=
|
|
|
|
|
|
|
|
# If we have patches or compat, define what to include
|
|
|
|
include=()
|
2019-08-11 09:13:55 +02:00
|
|
|
if [ -n "${patches[*]}" ]; then
|
2016-09-02 10:00:18 +02:00
|
|
|
include=( "${include[@]}" "${patches[@]}" )
|
|
|
|
fi
|
2019-08-11 09:13:55 +02:00
|
|
|
if [ -n "${compat[*]}" ]; then
|
2016-09-02 10:00:18 +02:00
|
|
|
include=( "${include[@]}" "${compat[@]}" )
|
2016-05-09 10:39:54 +02:00
|
|
|
fi
|
2016-09-02 10:00:18 +02:00
|
|
|
# Delete empty elements
|
|
|
|
for i in "${!include[@]}"; do
|
2018-01-13 05:20:52 +02:00
|
|
|
[ "${include[$i]}" = '' ] && unset include["$i"]
|
2016-09-02 10:00:18 +02:00
|
|
|
done
|
2019-08-11 09:13:55 +02:00
|
|
|
if [ -n "${include[*]}" ]; then
|
2016-09-02 10:00:18 +02:00
|
|
|
implode find_include "*' -and -name '*" "${include[@]}"
|
|
|
|
find_include="-and -name '*${find_include}*'"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Exclude everything we didn’t include
|
2022-09-13 07:06:41 +02:00
|
|
|
exclude=("Complete" "Font Awesome" "Font Logos" "Octicons" "Pomicons" "Nerd Font*Mono" "Windows Compatible")
|
2016-09-02 10:00:18 +02:00
|
|
|
for delete in "${include[@]}"; do
|
|
|
|
exclude=( "${exclude[@]/$delete}" )
|
|
|
|
done
|
|
|
|
# Delete empty elements
|
|
|
|
for i in "${!exclude[@]}"; do
|
2018-01-13 05:20:52 +02:00
|
|
|
[ "${exclude[$i]}" = '' ] && unset exclude["$i"]
|
2016-09-02 10:00:18 +02:00
|
|
|
done
|
2019-08-11 09:13:55 +02:00
|
|
|
if [ -n "${exclude[*]}" ]; then
|
2018-01-13 05:20:52 +02:00
|
|
|
implode find_exclude "*' -and \\! -name '*" "${exclude[@]}"
|
|
|
|
find_exclude="-and \\! -name '*${find_exclude}*'"
|
2016-09-02 10:00:18 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Construct directories to be searched
|
|
|
|
implode find_dirs "\" \"" "${nerdfonts_dirs[@]}"
|
|
|
|
find_dirs="\"$find_dirs\""
|
|
|
|
|
|
|
|
# Put it all together into the find command we want
|
2018-01-13 05:20:52 +02:00
|
|
|
find_command="find $find_dirs \\( \\( -name '*.[o,t]tf' -or -name '*.pcf.gz' \\) $find_include $find_exclude \\) -type f -print0"
|
2016-09-02 10:00:18 +02:00
|
|
|
|
|
|
|
# Find all the font files and store in array
|
|
|
|
files=()
|
|
|
|
while IFS= read -r -d $'\0'; do
|
|
|
|
files+=("$REPLY")
|
|
|
|
done < <(eval "$find_command")
|
2016-05-09 10:39:54 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
#
|
|
|
|
# Remove duplicates (i.e. when both otf and ttf version present)
|
|
|
|
#
|
2015-02-24 17:46:42 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
# Get list of file names without extensions
|
|
|
|
files_dedup=( "${files[@]}" )
|
|
|
|
for i in "${!files_dedup[@]}"; do
|
|
|
|
files_dedup[$i]="${files_dedup[$i]%.*}"
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove duplicates
|
|
|
|
for i in "${!files_dedup[@]}"; do
|
|
|
|
for j in "${!files_dedup[@]}"; do
|
2018-01-13 05:20:52 +02:00
|
|
|
[ "$i" = "$j" ] && continue
|
2016-09-02 10:00:18 +02:00
|
|
|
if [ "${files_dedup[$i]}" = "${files_dedup[$j]}" ]; then
|
|
|
|
ext="${files[$i]##*.}"
|
|
|
|
# Only remove if the extension is the one we don’t want
|
|
|
|
if [ "$ext" != "$extension" ]; then
|
2018-01-13 05:20:52 +02:00
|
|
|
unset files["$i"]
|
2016-09-02 10:00:18 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
done
|
|
|
|
|
|
|
|
# Get target root directory
|
2015-02-24 17:46:42 +02:00
|
|
|
if [[ $(uname) == 'Darwin' ]]; then
|
|
|
|
# MacOS
|
2022-04-22 07:45:16 +02:00
|
|
|
sys_font_dir="/Library/Fonts"
|
|
|
|
usr_font_dir="$HOME/Library/Fonts"
|
2015-02-24 17:46:42 +02:00
|
|
|
else
|
|
|
|
# Linux
|
2022-04-22 07:45:16 +02:00
|
|
|
sys_font_dir="/usr/local/share/fonts"
|
|
|
|
usr_font_dir="$HOME/.local/share/fonts"
|
|
|
|
fi
|
|
|
|
sys_font_dir="${sys_font_dir}/NerdFonts"
|
|
|
|
usr_font_dir="${usr_font_dir}/NerdFonts"
|
|
|
|
|
|
|
|
if [[ "system" == "$installpath" ]]; then
|
|
|
|
font_dir="${sys_font_dir}"
|
|
|
|
else
|
|
|
|
font_dir="${usr_font_dir}"
|
2015-02-24 17:46:42 +02:00
|
|
|
fi
|
2016-09-02 10:00:18 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
# Take the desired action
|
|
|
|
#
|
|
|
|
case $mode in
|
|
|
|
|
|
|
|
list)
|
|
|
|
for file in "${files[@]}"; do
|
|
|
|
file=$(basename "$file")
|
|
|
|
echo "$font_dir/${file#$nerdfonts_root_dir/}"
|
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
;;
|
2015-02-24 17:46:42 +02:00
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
copy | link)
|
|
|
|
if [ "$clean" = true ]; then
|
|
|
|
[ "$quiet" = false ] && rm -rfv "$font_dir"
|
|
|
|
[ "$quiet" = true ] && rm -rf "$font_dir"
|
|
|
|
fi
|
|
|
|
[ "$quiet" = false ] && mkdir -pv "$font_dir"
|
|
|
|
[ "$quiet" = true ] && mkdir -p "$font_dir"
|
|
|
|
case $mode in
|
|
|
|
copy)
|
|
|
|
[ "$quiet" = false ] && cp -fv "${files[@]}" "$font_dir"
|
|
|
|
[ "$quiet" = true ] && cp -f "${files[@]}" "$font_dir"
|
|
|
|
;;
|
|
|
|
link)
|
|
|
|
[ "$quiet" = false ] && ln -sfv "${files[@]}" "$font_dir"
|
|
|
|
[ "$quiet" = true ] && ln -sf "${files[@]}" "$font_dir"
|
|
|
|
;;
|
|
|
|
esac;;
|
|
|
|
|
2022-04-22 07:45:16 +02:00
|
|
|
remove)
|
|
|
|
if [[ "true" == "$dry" ]]; then
|
|
|
|
echo "Dry run. Would issue these commands:"
|
|
|
|
[ "$quiet" = false ] && echo rm -rfv "$sys_font_dir" "$usr_font_dir"
|
|
|
|
[ "$quiet" = true ] && echo rm -rf "$sys_font_dir" "$usr_font_dir"
|
|
|
|
else
|
|
|
|
[ "$quiet" = false ] && rm -rfv "$sys_font_dir" "$usr_font_dir"
|
|
|
|
[ "$quiet" = true ] && rm -rf "$sys_font_dir" "$usr_font_dir"
|
|
|
|
fi
|
|
|
|
font_dir="$sys_font_dir $usr_font_dir"
|
|
|
|
;;
|
|
|
|
|
2016-09-02 10:00:18 +02:00
|
|
|
esac
|
2015-02-24 17:46:42 +02:00
|
|
|
|
|
|
|
# Reset font cache on Linux
|
2019-08-11 16:30:38 +02:00
|
|
|
if [[ -n $(command -v fc-cache) ]]; then
|
2022-04-22 07:45:16 +02:00
|
|
|
if [[ "true" == "$dry" ]]; then
|
|
|
|
[ "$quiet" = false ] && echo fc-cache -vf "$font_dir"
|
|
|
|
[ "$quiet" = true ] && echo fc-cache -f "$font_dir"
|
|
|
|
else
|
|
|
|
[ "$quiet" = false ] && fc-cache -vf "$font_dir"
|
|
|
|
[ "$quiet" = true ] && fc-cache -f "$font_dir"
|
|
|
|
fi
|
2018-01-13 13:47:57 +02:00
|
|
|
case $? in
|
|
|
|
[0-1])
|
|
|
|
# Catch fc-cache returning 1 on a success
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
exit $?
|
|
|
|
;;
|
|
|
|
esac
|
2016-05-09 10:39:54 +02:00
|
|
|
fi
|