Created
November 15, 2018 11:49
-
-
Save ChristopherNeuwirth/4e1efedad26942b430657651b63e0be9 to your computer and use it in GitHub Desktop.
extends the pkgjson-iterm-touchbar.plugin.zsh plugin to be more visual
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# F1-12: https://github.com/vmalloc/zsh-config/blob/master/extras/function_keys.zsh | |
fnKeys=('^[OP' '^[OQ' '^[OR' '^[OS' '^[[15~' '^[[17~' '^[[18~' '^[[19~' '^[[20~' '^[[21~' '^[[23~' '^[[24~', '^[[25~', '^[[26~', '^[[27~', '^[[28~', '^[[29~', '^[[30~', '^[[31~', '^[[32~', '^[[33~', '^[[34~', '^[[35~', '^[[36~') | |
touchBarState='' | |
npmScripts=() | |
lastPackageJsonPath='' | |
useYarn=false | |
function _clearTouchbar() { | |
# echo -ne "\033]1337;PopKeyLabels\a" | |
echo -ne "\033]1337;SetKeyLabel=F1= No package.json in π \a" | |
} | |
function _unbindTouchbar() { | |
for fnKey in "$fnKeys[@]"; do | |
bindkey -s "$fnKey" '' | |
done | |
} | |
function _displayDefault() { | |
_clearTouchbar | |
_unbindTouchbar | |
touchBarState='' | |
# PACKAGE.JSON | |
# ------------ | |
if [[ -f package.json ]]; then | |
_displayNpmScripts | |
fi | |
} | |
function _displayNpmScripts() { | |
# find available npm run scripts only if new directory | |
if [[ $lastPackageJsonPath != $(echo "$(pwd)/package.json") ]]; then | |
lastPackageJsonPath=$(echo "$(pwd)/package.json") | |
npmScripts=($(node -e "console.log(Object.keys($(npm run --json)).filter((name, idx) => idx < 24).join(' '))")) | |
fi | |
_clearTouchbar | |
_unbindTouchbar | |
touchBarState='npm' | |
fnKeysIndex=1 | |
for npmScript in "$npmScripts[@]"; do | |
if [[ $useYarn == true ]]; then | |
bindkey -s $fnKeys[$fnKeysIndex] "yarn $npmScript \n" | |
else | |
bindkey -s $fnKeys[$fnKeysIndex] "npm run $npmScript \n" | |
fi | |
echo -ne "\033]1337;SetKeyLabel=F$fnKeysIndex=ππ» $npmScript\a" | |
fnKeysIndex=$((fnKeysIndex + 1)) | |
done | |
for fnKey in "$fnKeys[@]"; do | |
echo -ne "\033]1337;SetKeyLabel=F$fnKeysIndex=β\a" | |
fnKeysIndex=$((fnKeysIndex + 1)) | |
done | |
} | |
zle -N _displayDefault | |
zle -N _displayNpmScripts | |
precmd() { | |
if [[ $touchBarState == 'npm' ]]; then | |
_displayNpmScripts | |
else | |
_displayDefault | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment