Skip to content

Instantly share code, notes, and snippets.

@DarkoKukovec
Created August 25, 2016 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarkoKukovec/f037ff657cf061f72d53d49ed9f5618c to your computer and use it in GitHub Desktop.
Save DarkoKukovec/f037ff657cf061f72d53d49ed9f5618c to your computer and use it in GitHub Desktop.
SEGMENT_SEPARATOR_LEFT="\ue0b0"
PRIM_PROMP_COLOR="blue"
ERR_PROMP_COLOR="red"
PROMP_FORMAT="%2c"
function __spacer_left() {
bg="%{$bg[$1]%}"
fg="%{$fg[$2]%}"
print -n "$bg$fg$SEGMENT_SEPARATOR_LEFT"
}
function __prompter() {
bg="%{$bg[$2]%}"
fg="%{$fg_bold[$3]%}"
print -n "$bg$fg $1 %{$reset_color%}"
}
function __errorer() {
if [[ $? -ne 0 ]]; then
print -n "$(__prompter ! $ERR_PROMP_COLOR white)$(__spacer_left $1 $ERR_PROMP_COLOR)"
fi
}
function __parse_node_version() {
(command node -v | sed -e 's/v//g') 2>/dev/null;
}
function __get_node_version() {
print -n "%{$fg_bold[green]%}N%{$reset_color%}:$(__parse_node_version)%{$reset_color%}"
}
function __parse_git_dirty() {
if command git diff-index --quiet HEAD 2> /dev/null; then
if [[ $(git rev-list @{u}.. 2> /dev/null) ]]; then
print -n "%{$fg[yellow]%}$(__git_custom_status)%{$reset_color%}"
else
print -n "%{$fg[green]%}$(__git_custom_status)%{$reset_color%}"
fi
else
print -n "%{$fg[red]%}$(__git_custom_status)%{$reset_color%}"
fi
}
function __parse_git_branch() {
(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
}
function __git_custom_status() {
local git_where="$(__parse_git_branch)"
[ -n "$git_where" ] && print -n "${git_where#(refs/heads/|tags/)}%{$reset_color%} |"
}
function __mstime() {
perl -MTime::HiRes -e 'printf("%.0f\n",Time::HiRes::time()*1000)'
}
function preexec() {
TIMER=$(__mstime)
}
function precmd() {
export PROMPT='$(__errorer $PRIM_PROMP_COLOR)$(__prompter $PROMP_FORMAT $PRIM_PROMP_COLOR black)$(__spacer_left none $PRIM_PROMP_COLOR) '
if [[ $TIMER -ne 0 ]]; then
timer_show=$(($(__mstime) - $TIMER))
t="%F{cyan}${timer_show}ms%{$reset_color%} | "
unset TIMER
else
unset t
fi
export RPROMPT='$(__parse_git_dirty) $t$(__get_node_version)'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment