Created
August 5, 2017 13:51
-
-
Save trynity/70e5b33855bb853c3672c879a56aa3f6 to your computer and use it in GitHub Desktop.
This file contains 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
# GIT | |
GIT_UNCOMMITTED="${GIT_UNCOMMITTED:-+}" | |
GIT_UNSTAGED="${GIT_UNSTAGED:-!}" | |
GIT_UNTRACKED="${GIT_UNTRACKED:-?}" | |
GIT_STASHED="${GIT_STASHED:-$}" | |
GIT_UNPULLED="${GIT_UNPULLED:-โฃ}" | |
GIT_UNPUSHED="${GIT_UNPUSHED:-โก}" | |
# Output name of current branch. | |
git_current_branch() { | |
local ref | |
ref=$(command git symbolic-ref --quiet HEAD 2> /dev/null) | |
local ret=$? | |
if [[ $ret != 0 ]]; then | |
[[ $ret == 128 ]] && return # no git repo. | |
ref=$(command git rev-parse --short HEAD 2> /dev/null) || return | |
fi | |
echo ${ref#refs/heads/} | |
} | |
# Uncommitted changes. | |
# Check for uncommitted changes in the index. | |
git_uncomitted() { | |
if ! $(git diff --quiet --ignore-submodules --cached); then | |
echo -n "${GIT_UNCOMMITTED}" | |
fi | |
} | |
# Unstaged changes. | |
# Check for unstaged changes. | |
git_unstaged() { | |
if ! $(git diff-files --quiet --ignore-submodules --); then | |
echo -n "${GIT_UNSTAGED}" | |
fi | |
} | |
# Untracked files. | |
# Check for untracked files. | |
git_untracked() { | |
if [ -n "$(git ls-files --others --exclude-standard)" ]; then | |
echo -n "${GIT_UNTRACKED}" | |
fi | |
} | |
# Stashed changes. | |
# Check for stashed changes. | |
git_stashed() { | |
if $(git rev-parse --verify refs/stash &>/dev/null); then | |
echo -n "${GIT_STASHED}" | |
fi | |
} | |
# Unpushed and unpulled commits. | |
# Get unpushed and unpulled commits from remote and draw arrows. | |
git_unpushed_unpulled() { | |
# check if there is an upstream configured for this branch | |
command git rev-parse --abbrev-ref @'{u}' &>/dev/null || return | |
local count | |
count="$(command git rev-list --left-right --count HEAD...@'{u}' 2>/dev/null)" | |
# exit if the command failed | |
(( !$? )) || return | |
# counters are tab-separated, split on tab and store as array | |
count=(${(ps:\t:)count}) | |
local arrows left=${count[1]} right=${count[2]} | |
(( ${right:-0} > 0 )) && arrows+="${GIT_UNPULLED}" | |
(( ${left:-0} > 0 )) && arrows+="${GIT_UNPUSHED}" | |
[ -n $arrows ] && echo -n "${arrows}" | |
} | |
# 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~') | |
touchBarState='' | |
npmScripts=() | |
lastPackageJsonPath='' | |
function _clearTouchbar() { | |
echo -ne "\033]1337;PopKeyLabels\a" | |
} | |
function _unbindTouchbar() { | |
for fnKey in "$fnKeys[@]"; do | |
bindkey -s "$fnKey" '' | |
done | |
} | |
function _displayDefault() { | |
_clearTouchbar | |
_unbindTouchbar | |
touchBarState='' | |
# CURRENT_DIR | |
# ----------- | |
echo -ne "\033]1337;SetKeyLabel=F1=๐ $(echo $(pwd) | awk -F/ '{print $(NF-1)"/"$(NF)}')\a" | |
bindkey -s '^[OP' 'pwd \n' | |
# RVM | |
# --- | |
echo -ne "\033]1337;SetKeyLabel=F2=๐ $(rvm current)\a"; | |
bindkey -s '^[OQ' 'rvm current \n' | |
# NVM | |
# --- | |
echo -ne "\033]1337;SetKeyLabel=F3=๐node $(nvm current)\a"; | |
bindkey -s '^[OR' 'nvm current \n' | |
# GIT | |
# --- | |
# Check if the current directory is in a Git repository. | |
command git rev-parse --is-inside-work-tree &>/dev/null || return | |
# Check if the current directory is in .git before running git checks. | |
if [[ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]]; then | |
# Ensure the index is up to date. | |
git update-index --really-refresh -q &>/dev/null | |
# String of indicators | |
local indicators='' | |
indicators+="$(git_uncomitted)" | |
indicators+="$(git_unstaged)" | |
indicators+="$(git_untracked)" | |
indicators+="$(git_stashed)" | |
indicators+="$(git_unpushed_unpulled)" | |
[ -n "${indicators}" ] && touchbarIndicators="๐ฅ[${indicators}]" || touchbarIndicators="๐"; | |
echo -ne "\033]1337;SetKeyLabel=F4=๐ $(git_current_branch)\a" | |
echo -ne "\033]1337;SetKeyLabel=F5=$touchbarIndicators\a" | |
# bind git actions | |
bindkey -s '^[OS' 'git branch -a \n' | |
bindkey -s '^[[15~' 'git status \n' | |
fi | |
# PACKAGE.JSON | |
# ------------ | |
if [[ -f package.json ]]; then | |
echo -ne "\033]1337;SetKeyLabel=F5=โก๏ธ npm-run\a" | |
bindkey "${fnKeys[5]}" _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 => !name.includes(':')).sort((a, b) => a.localeCompare(b)).filter((name, idx) => idx < 12).join(' '))")) | |
fi | |
_clearTouchbar | |
_unbindTouchbar | |
touchBarState='npm' | |
fnKeysIndex=1 | |
for npmScript in "$npmScripts[@]"; do | |
fnKeysIndex=$((fnKeysIndex + 1)) | |
bindkey -s $fnKeys[$fnKeysIndex] "npm run $npmScript \n" | |
echo -ne "\033]1337;SetKeyLabel=F$fnKeysIndex=$npmScript\a" | |
done | |
echo -ne "\033]1337;SetKeyLabel=F1=๐ back\a" | |
bindkey "${fnKeys[1]}" _displayDefault | |
} | |
zle -N _displayDefault | |
zle -N _displayNpmScripts | |
precmd_iterm_touchbar() { | |
if [[ $touchBarState == 'npm' ]]; then | |
_displayNpmScripts | |
else | |
_displayDefault | |
fi | |
} | |
autoload -Uz add-zsh-hook | |
add-zsh-hook precmd precmd_iterm_touchbar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment