new git-aware prompt based on zsh-git by jcorbin
This commit is contained in:
parent
c965d344da
commit
aade651214
2 changed files with 260 additions and 97 deletions
254
zsh/functions/prompt_sjs_setup
Normal file
254
zsh/functions/prompt_sjs_setup
Normal file
|
|
@ -0,0 +1,254 @@
|
||||||
|
# sjs's prompt theme
|
||||||
|
|
||||||
|
autoload -U zgitinit
|
||||||
|
zgitinit
|
||||||
|
|
||||||
|
prompt_sjs_help () {
|
||||||
|
cat <<'EOF'
|
||||||
|
|
||||||
|
prompt sjs
|
||||||
|
|
||||||
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
revstring() {
|
||||||
|
git describe --always $1 2>/dev/null ||
|
||||||
|
git rev-parse --short $1 2>/dev/null
|
||||||
|
}
|
||||||
|
|
||||||
|
coloratom() {
|
||||||
|
local off=$1 atom=$2
|
||||||
|
if [[ $atom[1] == [[:upper:]] ]]; then
|
||||||
|
off=$(( $off + 60 ))
|
||||||
|
fi
|
||||||
|
echo $(( $off + $colorcode[${(L)atom}] ))
|
||||||
|
}
|
||||||
|
colorword() {
|
||||||
|
local fg=$1 bg=$2 att=$3
|
||||||
|
local -a s
|
||||||
|
|
||||||
|
if [ -n "$fg" ]; then
|
||||||
|
s+=$(coloratom 30 $fg)
|
||||||
|
fi
|
||||||
|
if [ -n "$bg" ]; then
|
||||||
|
s+=$(coloratom 40 $bg)
|
||||||
|
fi
|
||||||
|
if [ -n "$att" ]; then
|
||||||
|
s+=$attcode[$att]
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "%{"$'\e['${(j:;:)s}m"%}"
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt_sjs_setup() {
|
||||||
|
typeset -A colorcode
|
||||||
|
colorcode[black]=0
|
||||||
|
colorcode[red]=1
|
||||||
|
colorcode[green]=2
|
||||||
|
colorcode[yellow]=3
|
||||||
|
colorcode[blue]=4
|
||||||
|
colorcode[magenta]=5
|
||||||
|
colorcode[cyan]=6
|
||||||
|
colorcode[white]=7
|
||||||
|
colorcode[default]=9
|
||||||
|
colorcode[k]=$colorcode[black]
|
||||||
|
colorcode[r]=$colorcode[red]
|
||||||
|
colorcode[g]=$colorcode[green]
|
||||||
|
colorcode[y]=$colorcode[yellow]
|
||||||
|
colorcode[b]=$colorcode[blue]
|
||||||
|
colorcode[m]=$colorcode[magenta]
|
||||||
|
colorcode[c]=$colorcode[cyan]
|
||||||
|
colorcode[w]=$colorcode[white]
|
||||||
|
colorcode[.]=$colorcode[default]
|
||||||
|
|
||||||
|
typeset -A attcode
|
||||||
|
attcode[none]=00
|
||||||
|
attcode[bold]=01
|
||||||
|
attcode[faint]=02
|
||||||
|
attcode[standout]=03
|
||||||
|
attcode[underline]=04
|
||||||
|
attcode[blink]=05
|
||||||
|
attcode[reverse]=07
|
||||||
|
attcode[conceal]=08
|
||||||
|
attcode[normal]=22
|
||||||
|
attcode[no-standout]=23
|
||||||
|
attcode[no-underline]=24
|
||||||
|
attcode[no-blink]=25
|
||||||
|
attcode[no-reverse]=27
|
||||||
|
attcode[no-conceal]=28
|
||||||
|
|
||||||
|
local -A pc
|
||||||
|
pc[default]='default'
|
||||||
|
pc[date]='cyan'
|
||||||
|
pc[time]='Blue'
|
||||||
|
pc[host]='Green'
|
||||||
|
pc[user]='cyan'
|
||||||
|
pc[punc]='yellow'
|
||||||
|
pc[line]='magenta'
|
||||||
|
pc[hist]='green'
|
||||||
|
pc[path]='Cyan'
|
||||||
|
pc[shortpath]='default'
|
||||||
|
pc[rc]='red'
|
||||||
|
pc[scm_branch]='Cyan'
|
||||||
|
pc[scm_commitid]='Yellow'
|
||||||
|
pc[scm_status_dirty]='Red'
|
||||||
|
pc[scm_status_staged]='Green'
|
||||||
|
pc[#]='Yellow'
|
||||||
|
for cn in ${(k)pc}; do
|
||||||
|
pc[${cn}]=$(colorword $pc[$cn])
|
||||||
|
done
|
||||||
|
pc[reset]=$(colorword . . 00)
|
||||||
|
|
||||||
|
typeset -Ag sjs_prompt_colors
|
||||||
|
sjs_prompt_colors=(${(kv)pc})
|
||||||
|
|
||||||
|
local p_rc
|
||||||
|
|
||||||
|
PROMPT=
|
||||||
|
PROMPT+="%(?..$pc[rc]-%1v- $pc[reset])"
|
||||||
|
PROMPT+="$pc[path]%(2~.%~.%/)$pc[reset]"
|
||||||
|
PROMPT+="\$(prompt_sjs_scm_branch)"
|
||||||
|
PROMPT+=" $pc[#]%#$pc[reset] "
|
||||||
|
|
||||||
|
RPROMPT="$pc[user]%n$pc[reset]@$pc[host]%m$pc[reset]"
|
||||||
|
|
||||||
|
export PROMPT RPROMPT
|
||||||
|
precmd_functions+='prompt_sjs_precmd'
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt_sjs_precmd() {
|
||||||
|
local ex=$?
|
||||||
|
psvar=()
|
||||||
|
|
||||||
|
if [[ $ex -ge 128 ]]; then
|
||||||
|
sig=$signals[$ex-127]
|
||||||
|
psvar[1]="sig${(L)sig}"
|
||||||
|
else
|
||||||
|
psvar[1]="$ex"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt_sjs_scm_status() {
|
||||||
|
zgit_isgit || return
|
||||||
|
local -A pc
|
||||||
|
pc=(${(kv)sjs_prompt_colors})
|
||||||
|
|
||||||
|
head=$(zgit_head)
|
||||||
|
gitcommit=$(revstring $head)
|
||||||
|
|
||||||
|
local -a commits
|
||||||
|
|
||||||
|
if zgit_rebaseinfo; then
|
||||||
|
orig_commit=$(revstring $zgit_info[rb_head])
|
||||||
|
orig_name=$(git name-rev --name-only $zgit_info[rb_head])
|
||||||
|
orig="$pc[scm_branch]$orig_name$pc[punc]($pc[scm_commitid]$orig_commit$pc[punc])"
|
||||||
|
onto_commit=$(revstring $zgit_info[rb_onto])
|
||||||
|
onto_name=$(git name-rev --name-only $zgit_info[rb_onto])
|
||||||
|
onto="$pc[scm_branch]$onto_name$pc[punc]($pc[scm_commitid]$onto_commit$pc[punc])"
|
||||||
|
|
||||||
|
if [ -n "$zgit_info[rb_upstream]" ] && [ $zgit_info[rb_upstream] != $zgit_info[rb_onto] ]; then
|
||||||
|
upstream_commit=$(revstring $zgit_info[rb_upstream])
|
||||||
|
upstream_name=$(git name-rev --name-only $zgit_info[rb_upstream])
|
||||||
|
upstream="$pc[scm_branch]$upstream_name$pc[punc]($pc[scm_commitid]$upstream_commit$pc[punc])"
|
||||||
|
commits+="rebasing $upstream$pc[reset]..$orig$pc[reset] onto $onto$pc[reset]"
|
||||||
|
else
|
||||||
|
commits+="rebasing $onto$pc[reset]..$orig$pc[reset]"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local -a revs
|
||||||
|
revs=($(git rev-list $zgit_info[rb_onto]..HEAD))
|
||||||
|
if [ $#revs -gt 0 ]; then
|
||||||
|
commits+="\n$#revs commits in"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f $zgit_info[dotest]/message ]; then
|
||||||
|
mess=$(head -n1 $zgit_info[dotest]/message)
|
||||||
|
commits+="on $mess"
|
||||||
|
fi
|
||||||
|
elif [ -n "$gitcommit" ]; then
|
||||||
|
local track_merge=$(zgit_tracking_merge)
|
||||||
|
if [ -n "$track_merge" ]; then
|
||||||
|
if git rev-parse --verify -q $track_merge >/dev/null; then
|
||||||
|
local track_remote=$(zgit_tracking_remote)
|
||||||
|
local tracked=$(revstring $track_merge 2>/dev/null)
|
||||||
|
|
||||||
|
local -a revs
|
||||||
|
revs=($(git rev-list --reverse $track_merge..HEAD))
|
||||||
|
if [ $#revs -gt 0 ]; then
|
||||||
|
local base=$(revstring $revs[1]~1)
|
||||||
|
local base_name=$(git name-rev --name-only $base)
|
||||||
|
local base_short=$(revstring $base)
|
||||||
|
local word_commits
|
||||||
|
if [ $#revs -gt 1 ]; then
|
||||||
|
word_commits='commits'
|
||||||
|
else
|
||||||
|
word_commits='commit'
|
||||||
|
fi
|
||||||
|
|
||||||
|
local conj="since"
|
||||||
|
if [[ "$base" == "$tracked" ]]; then
|
||||||
|
conj+=" tracked"
|
||||||
|
tracked=
|
||||||
|
fi
|
||||||
|
commits+="$#revs $word_commits $conj $pc[scm_branch]$base_name$pc[punc]($pc[scm_commitid]$base_short$pc[punc])$pc[reset]"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
gitsvn=$(git rev-parse --verify -q --short git-svn)
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
gitsvnrev=$(zgit_svnhead $gitsvn)
|
||||||
|
gitsvn=$(revstring $gitsvn)
|
||||||
|
if [ -n "$gitsvnrev" ]; then
|
||||||
|
local svninfo=''
|
||||||
|
local -a revs
|
||||||
|
svninfo+="$pc[default]svn$pc[punc]:$pc[scm_branch]r$gitsvnrev"
|
||||||
|
revs=($(git rev-list git-svn..HEAD))
|
||||||
|
if [ $#revs -gt 0 ]; then
|
||||||
|
svninfo+="$pc[punc]@$pc[default]HEAD~$#revs"
|
||||||
|
svninfo+="$pc[punc]($pc[scm_commitid]$gitsvn$pc[punc])"
|
||||||
|
fi
|
||||||
|
commits+=$svninfo
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $#commits -gt 0 ]; then
|
||||||
|
echo -n " ${(j: :)commits}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt_sjs_scm_branch() {
|
||||||
|
zgit_isgit || return
|
||||||
|
local -A pc
|
||||||
|
pc=(${(kv)sjs_prompt_colors})
|
||||||
|
|
||||||
|
echo -n "$pc[punc]:$pc[scm_branch]$(zgit_head)"
|
||||||
|
|
||||||
|
if zgit_inworktree; then
|
||||||
|
if ! zgit_isindexclean; then
|
||||||
|
echo -n "$pc[scm_status_staged]+"
|
||||||
|
fi
|
||||||
|
|
||||||
|
local -a dirty
|
||||||
|
if ! zgit_isworktreeclean; then
|
||||||
|
dirty+='!'
|
||||||
|
fi
|
||||||
|
|
||||||
|
if zgit_hasunmerged; then
|
||||||
|
dirty+='*'
|
||||||
|
fi
|
||||||
|
|
||||||
|
if zgit_hasuntracked; then
|
||||||
|
dirty+='?'
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $#dirty -gt 0 ]; then
|
||||||
|
echo -n "$pc[scm_status_dirty]${(j::)dirty}"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo $pc[reset]
|
||||||
|
}
|
||||||
|
|
||||||
|
prompt_sjs_setup "$@"
|
||||||
101
zsh/zshrc
101
zsh/zshrc
|
|
@ -141,108 +141,17 @@ bindkey '\ep' history-beginning-search-backward
|
||||||
# 6. Prompt Subsystem
|
# 6. Prompt Subsystem
|
||||||
# ===================
|
# ===================
|
||||||
|
|
||||||
# define some colours to use in english instead of ANSI
|
# Load the prompt theme system
|
||||||
# notation:
|
autoload -U promptinit
|
||||||
# -background is black unless the var is named colourOnBlue
|
promptinit
|
||||||
# -prefix of b, e.g. bGreen means bright green
|
|
||||||
none='%{\e[0m%}'
|
|
||||||
|
|
||||||
red='%{\e[40;31m%}'
|
|
||||||
green='%{\e[40;32m%}'
|
|
||||||
yellow='%{\e[40;33m%}'
|
|
||||||
blue='%{\e[40;34m%}'
|
|
||||||
purple='%{\e[40;35m%}'
|
|
||||||
cyan='%{\e[40;36m%}'
|
|
||||||
white='%{\e[40;37m%}'
|
|
||||||
grey='%{\e[1;40;30m%}'
|
|
||||||
|
|
||||||
bRed='%{\e[1;40;31m%}'
|
|
||||||
bGreen='%{\e[1;40;32m%}'
|
|
||||||
bYellow='%{\e[1;40;33m%}'
|
|
||||||
bBlue='%{\e[1;40;34m%}'
|
|
||||||
bPurple='%{\e[1;40;35m%}'
|
|
||||||
bCyan='%{\e[1;40;36m%}'
|
|
||||||
bWhite='%{\e[1;40;37m%}'
|
|
||||||
|
|
||||||
whiteOnRed='%{\e[41;37m%}'
|
|
||||||
whiteOnBlue='%{\e[44;37m%}'
|
|
||||||
bWhiteOnRed='%{\e[1;41;37m%}'
|
|
||||||
|
|
||||||
# PROMPT SUBTITUTION
|
|
||||||
# ------------------
|
|
||||||
# %l - tty %M - full machine hostname
|
|
||||||
# %m - hostname %n - USERNAME
|
|
||||||
# %y - tty w/ prefix %# - # or %
|
|
||||||
# %? - last exit code %d,%/ - PWD
|
|
||||||
# %~ - ~ for home etc %h,%! - history event #
|
|
||||||
# %j - # of jobs
|
|
||||||
#
|
|
||||||
# %B - bold %E - clear to EOL
|
|
||||||
# %U - underline %S - standout
|
|
||||||
#
|
|
||||||
# %([n]x.true.false) '.' is arbitrary
|
|
||||||
# 'x' can be:
|
|
||||||
# ! - running with privs
|
|
||||||
# # - effective uid is 'n'
|
|
||||||
# ? exit status was 'n'
|
|
||||||
# C
|
|
||||||
# / - PWD has >= 'n' elements
|
|
||||||
# c
|
|
||||||
# .
|
|
||||||
# ~ - PWD with prefix replacements has >= 'n' elements
|
|
||||||
# D - month = 'n' (jan = 0)
|
|
||||||
# d - day of month = 'n'
|
|
||||||
# g - gid is 'n'
|
|
||||||
# j - # jobs >= 'n'
|
|
||||||
# l - 'n' chars already printed on line
|
|
||||||
# T - hours = 'n'
|
|
||||||
# t - minutes = 'n'
|
|
||||||
# w - day of week = 'n' (sun = 0)
|
|
||||||
|
|
||||||
# looks like:
|
|
||||||
# user@host% [~]
|
|
||||||
#
|
|
||||||
# non-zero exit code:
|
|
||||||
# -1- user@host% [~]
|
|
||||||
#
|
|
||||||
# jobs running:
|
|
||||||
# [1] user@host% [~]
|
|
||||||
|
|
||||||
eval exitCode='%(0?..${bWhiteOnRed}-%?-${none} )'
|
|
||||||
eval dirpath='${bGreen}%(4~!.../%3~!%~)${none}'
|
|
||||||
eval job='%(1j.${bYellow}[%j] .)'
|
|
||||||
#hist="${whiteOnBlue}%5h${none}|"
|
|
||||||
eval user='%(0#.${bRed}.${yellow})%n${none}'
|
|
||||||
eval at='${blue}@${none}'
|
|
||||||
|
|
||||||
case $HOSTNAME in
|
|
||||||
Samis-MacBook)
|
|
||||||
host="${cyan}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
Samis-Workstation)
|
|
||||||
host="${bWhite}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
zaphod)
|
|
||||||
host="${bRed}"
|
|
||||||
;;
|
|
||||||
|
|
||||||
*)
|
|
||||||
host="${bGreen}"
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
eval host='${host}%m${none}'
|
|
||||||
|
|
||||||
if [[ "x$INSIDE_EMACS" != "x" ]] || [[ "$EMACS" = "t" ]]; then
|
if [[ "x$INSIDE_EMACS" != "x" ]] || [[ "$EMACS" = "t" ]]; then
|
||||||
export PROMPT="%d %% "
|
export PROMPT="%d %% "
|
||||||
export PAGER=cat
|
export PAGER=cat
|
||||||
export MANPAGER=cat
|
export MANPAGER=cat
|
||||||
else
|
else
|
||||||
PROMPT=`echo -n "${exitCode}${job}${dirpath} ${git} ${bWhite}%#${none} "`
|
# Use my prompt theme, based on wunjo (zsh-git)
|
||||||
RPROMPT=`echo -n "${user}${at}${host}"`
|
prompt sjs
|
||||||
eval export PROMPT=$'${PROMPT}'
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue