#!/usr/bin/env bash
if [ "$(uname -s)" == "Linux" ]; then
    NF_LS_DIR="$HOME/.cache/fzf/nf-ls"
elif [ "$(uname -s)" == "Darwin" ]; then
    NF_LS_DIR="/Library/Caches/fzf/nf-ls"
fi

get_nf_cheat_sheets() {
    echo -ne "$(
		curl -s https://www.nerdfonts.com/cheat-sheet |
			grep -o '<div class="class-name">.*</div>' |
			sed -e 's/<div class="class-name">\(.*\)<\/div><div title="Copy Hex Code to Clipboard" class="codepoint">\(.*\)<\/div>/\\u\2\tnf-\1/'
    )"
}

update_nf-ls_cache() {
    get_nf_cheat_sheets >"$NF_LS_DIR"/nf-ls-cheat.sh
}
export -f update_nf-ls_cache

if [ ! -d "$NF_LS_DIR" ]; then
    mkdir -p "$NF_LS_DIR"
    update_nf-ls_cache
fi

awk '{print $1 "    -- " $2}' "$NF_LS_DIR"/nf-ls-cheat.sh | column -t |
fzf -m \
    --header "Nerd Font Cheat.sh" \
    --bind "enter:execute(wl-copy {+1}; notify-send 'Nerd Font Cheat.sh' '{+1} {+3} - icon has been copied')" \
    --bind "alt-u:execute(bash -c update_nf-ls_cache)"
