Skip to content

Instantly share code, notes, and snippets.

@aidanharris
Last active November 23, 2017 12:09
Show Gist options
  • Save aidanharris/e99158bcab775423dd949595ca41c227 to your computer and use it in GitHub Desktop.
Save aidanharris/e99158bcab775423dd949595ca41c227 to your computer and use it in GitHub Desktop.
Displays a fontawesome cheatsheet in your terminal - Requires patched fonts such as nerd-fonts (https://github.com/ryanoasis/nerd-fonts)
#!/usr/bin/env bash
set -e
if [[ -z "$ENABLE_CACHING" ]]
then
export ENABLE_CACHING=1
fi
if [[ -z "$FONTAWESOME_CACHE" ]]
then
export FONTAWESOME_CACHE="$HOME/.cache"
fi
mkdir -p "$FONTAWESOME_CACHE"
if [[ -z "$LAST_MODIFIED_FILE" ]]
then
if [[ "$ENABLE_CACHING" -ne 1 ]]
then
export LAST_MODIFIED_FILE=/dev/null
else
export LAST_MODIFIED_FILE="$FONTAWESOME_CACHE/fontawesome.txt"
fi
fi
if [[ -z "$FONTAWESOME_ICONS" ]]
then
if [[ "$ENABLE_CACHING" -ne 1 ]]
then
export FONTAWESOME_ICONS=/dev/null
else
export FONTAWESOME_ICONS="$FONTAWESOME_CACHE/fontawesome-icons.txt"
fi
fi
# Basic filtering with grep e.g `./fontawesome.sh music`
# will output "fa-music: [f001] ". For more advanced
# filtering pipe to another program directly e.g:
# `./fontawesome.sh | fzf`
if [[ ! -z "$1" ]]
then
$0 | grep -Ei "fa-.*$1.*"
exit "$?"
fi
IFS=$'\n'
# To Do:
# * For the sake of simplicity two HTTP requests are made.
# Can we do this with one?
get_icons() {
curl --head -s -L http://fontawesome.io/cheatsheet/ | grep -E '^Last-Modified:' | head -1 > "$LAST_MODIFIED_FILE"
curl -s -L http://fontawesome.io/cheatsheet/ -H 'Accept-Encoding: gzip' | gunzip -cf | html2text | grep -E 'fa-.* \[.*\]' | sed -e 's/&#x//' -e 's/;//' -e 's/^.* fa-/fa-/g' -e 's/&#x//' -e 's/;//'
}
# Extremely basic cache checking - When the Last-Modified changes
# our cache will be invalidated.
# To Do:
# * What happens if there's no Internet? curl will probably fail
# which might get treated as the cache being invalidated?
# Can we detect this and use our local cache?
check_cache() {
if [[ "$ENABLE_CACHING" -ne 1 ]]
then
return 1
fi
if [[ -f "$LAST_MODIFIED_FILE" ]]
then
CURRENT_MODIFIED="$(< "$LAST_MODIFIED_FILE")"
else
CURRENT_MODIFIED=""
fi
LAST_MODIFIED="$(curl --head -s -L http://fontawesome.io/cheatsheet/ | grep -E '^Last-Modified:' | head -1)"
if [[ "$CURRENT_MODIFIED" == "$LAST_MODIFIED" ]]
then
return 0
else
return 1
fi
}
if check_cache
then
if [[ -f "$FONTAWESOME_ICONS" ]]
then
column -x "$FONTAWESOME_ICONS"
exit "$?"
fi
fi
for icon in $(get_icons)
do
if grep -qE ' \(alias\) ' <<< "$icon"
then
ALIAS="(alias)"
else
ALIAS=""
fi
NAME=$(awk '{print $1}' <<< "$icon")
ICON=$(grep -ohE '\[.*\]' <<< "$icon")
UNICODE="$(cut -c 2- <<< "$ICON" | rev | cut -c 2- | rev)"
printf "%s: %s %s \U$UNICODE\n" "$NAME" "$ALIAS" "$ICON" | tee -a "$FONTAWESOME_ICONS"
done | column -x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment