X-Git-Url: https://git.madduck.net/etc/zsh.git/blobdiff_plain/e7b57bc5c5383a6eac9b7d8d0af4f1c63723b948..2188fcdf52262cc8ed492d9ad439e6ca91f5e9ca:/.zsh/themes/prompt_madduck_setup diff --git a/.zsh/themes/prompt_madduck_setup b/.zsh/themes/prompt_madduck_setup index 0b6dddc..64f7bc0 100644 --- a/.zsh/themes/prompt_madduck_setup +++ b/.zsh/themes/prompt_madduck_setup @@ -13,6 +13,12 @@ zstyle -m :madduck:prompt:default path-maxlen '*' \ || zstyle :madduck:prompt:default path-maxlen 25 zstyle -m :madduck:prompt:default path-minlen '*' \ || zstyle :madduck:prompt:default path-minlen 10 +for i in {1..9}; do + zstyle -m :madduck:prompt:default psvar${i}-style-pre '*' \ + || zstyle :madduck:prompt:default psvar${i}-style-pre '' + zstyle -m :madduck:prompt:default psvar${i}-style-post '*' \ + || zstyle :madduck:prompt:default psvar${i}-style-post '' +done __on_networkfs() { @@ -23,142 +29,83 @@ __on_networkfs() return 1 } -__git_get_reporoot() -{ +__get_prompt_path_len() { emulate -L zsh - # return the full path to the root of the current git repository - [ -d "$GIT_DIR" ] && echo "$GIT_DIR" && return 0 - local dir; dir="$PWD/$(git rev-parse --show-cdup)" - # do not use --show-toplevel because it resolves symlinks - echo $dir:a + local result + zstyle -s ":madduck:prompt:$PWD" path-${1}len result + [ -z "$result" ] && zstyle -s ':madduck:prompt:default' path-${1}len result + echo "$result" } -__git_get_branch() -{ +__get_prompt_psvar_style() { emulate -L zsh - # use oh-my-zsh prompt info function if it exists - $(command -v git_prompt_info) && return - - # return the name of the git branch we're on - local ref gitdir - gitdir="$(git rev-parse --git-dir)" - ref=$(git --git-dir="$gitdir" symbolic-ref -q HEAD 2>/dev/null \ - || git --git-dir="$gitdir" name-rev --name-only HEAD 2>/dev/null) || return 1 - echo "${ref#refs/heads/}" + local result + zstyle -s ":madduck:prompt:$PWD" psvar${1}-style-${2} result + [ -z "$result" ] && zstyle -s ':madduck:prompt:default' psvar${1}-style-${2} result + echo "$result" } -__git_print_preprompt() -{ - emulate -L zsh - [ "$(git config --get core.bare)" = false ] || return - __on_networkfs && return - - local COLUMNS=${COLUMNS:-80} - local LINES=${LINES:-25} - - function output() { - emulate -L zsh - local title="$@" - local output=(${(f)"$(cat)"}) - - [[ ${#output} -ge 1 ]] || return - - local statl="$(echo ${output[-1]} | sed -re 's@^\s*([0-9]+)[^,]+(, ([0-9]+) [^(]+\(([-+])\))(, ([0-9]+) [^(]+\(([-+])\))?@\1/\4\3/\7\6@')" - - if [[ ${output[-2]## } = '...' ]]; then - print "${title} (${statl%/}, abbrev.):" - print "${(F)output[1,-3]}" - print " …" - else - print "${title} (${statl%/}):" - print "${(F)output[1,-2]}" - fi - } - - function gitdiffstat() { - emulate -L zsh - local common_options="--stat=$((COLUMNS/2-1)),$((COLUMNS/4-2)),$(($LINES/3)) --relative" - eval git diff $common_options "$@" 2>/dev/null - } +# define defaults for the $psvar array, which will actually be emptied later, +# but $PS1 then contains these as default values for each psvar element, i.e. +# we can override psvar[1] later, but we don't have to +psvar[1]='%(2L.+.)' +psvar[2]="$SUBSHELL_PREFIX" + zstyle :madduck:prompt:default psvar2-style-pre '%S' + zstyle :madduck:prompt:default psvar2-style-post '%s@' +psvar[3]='%m' + zstyle :madduck:prompt:default psvar3-style-pre '%B' + zstyle :madduck:prompt:default psvar3-style-post '%b:' + +function __set_prompt_pwd() { + psvar[4]="%$(__get_prompt_path_len max)<…<%~%<<" +} +# make this an early hook so that it's always set unless overridden +# by a later handler +add-zsh-hook chpwd __set_prompt_pwd && __set_prompt_pwd + +psvar[9]='%# ' + +local function make_ps1() { + local pre post psv + for i in {1..9}; do + pre="\$(__get_prompt_psvar_style $i pre)" + post="\$(__get_prompt_psvar_style $i post)" + psv="${psvar[$i]}" + echo -n "%(${i}V;${pre}\${psvar[$i]}${post};${psv:+${pre}${psv}${post}})" + done +} +PS1=$(make_ps1) +unfunction make_ps1 +psvar=() +PS2="%{$fg[red]%}%_>%{$reset_color%}" - local cached=(${(f)"$(gitdiffstat --cached | output cached)"}) - local changed=(${(f)"$(gitdiffstat | output changed)"}) +local function make_rps1() { + # First, a comment character and parens + echo -n '#(' - local max=${#changed} - [[ $max -lt ${#cached} ]] && max=${#cached} + # Next, if the returncode was non-zero, make it stand-out + # and include a trailing space + echo -n "%(0?..%{$fg[red]%}%S%?%s%{$reset_color%} )" - ((max == 0)) && return + # If there are background jobs, print their number, followed by + # '@': + echo -n '%(1j.%j@.)' - local width=$(((COLUMNS-3)/2)) + # and then the terminal line we're using + echo -n '%l' - if (( ${#cached} > 0 && ${#changed} > 0 )); then - local i - for (( i=1 ; i <= max ; i++ )) do - printf "%-${width}s │ %-${width}s\n" "${cached[$i]}" "${changed[$i]}" - done - else - print ${(F)cached}${(F)changed} - fi -} + # this concludes the first part, but there's more + echo -n ') ' -__hg_get_reporoot() -{ - emulate -L zsh - hg root + # the timestamp will finish it off: + echo -n '%D{%d %H:%M:%S.%.}' + echo } +typeset -g RPS1="$(make_rps1)" +unfunction make_rps1 -__hg_get_branch() -{ - emulate -L zsh - echo "hg:$(hg branch)" -} -__bzr_get_reporoot() -{ - emulate -L zsh - local reporoot - reporoot="$(bzr info | sed -rne 's, *branch root: ,,p')" - case "$reporoot" in - .) echo "$PWD";; - *) echo "$reporoot";; - esac -} -__bzr_get_branch() -{ - emulate -L zsh - local branch revno - bzr version-info | while read i j; do - case "$i" in - revno:) revno="$j";; - branch-nick:) branch="$j";; - esac - done - echo "bzr:${branch}@$revno" -} - -__vcs_get_repo_type() -{ - emulate -L zsh - # return the type of the closest repository in the path hierarchy - local dir - while true; do - [ -d ${dir}.git ] && echo git && break - [ -d "$GIT_DIR" ] && echo git && break - [ -d ${dir}.bzr ] && echo bzr && break - [ -d ${dir}.hg ] && echo hg && break - [ "$(readlink -f ${dir:-.})" = / ] && echo NONE && break - dir="../$dir" - done -} - -__get_prompt_path_len() { - emulate -L zsh - local result - zstyle -s ":madduck:prompt:$PWD" path-${1}len result - [ -z "$result" ] && zstyle -s ':madduck:prompt:default' path-${1}len result - echo $result -} __vcs_get_prompt_path_components() { @@ -236,10 +183,7 @@ __vcs_set_prompt_variables() else eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") if [ -d "$GIT_DIR" ]; then - # poor man's replace until I find out how to do named dirs properly - # here: - local _D="${GIT_DIR/$HOME/~}" - set -- "$_D" "$2" "${${1#$_D}%/}" + set -- "${(D)GIT_DIR}" "$2" "${${1#$_D}%/}" fi fi ;; @@ -259,7 +203,7 @@ __vcs_set_prompt_variables() ;; *) case "$repotype" in - NONE) :;; + NONE|netfs) :;; *) warn "$repotype repositories not (yet) supported in the prompt";; esac local MAXLEN MINLEN @@ -275,27 +219,17 @@ __vcs_set_prompt_variables() psvar[1,3]=($1 $2 $3) } -__vcs_print_preprompt() -{ - emulate -L zsh - local repotype="${1:-$(__vcs_get_repo_type)}" - - case "$repotype" in - git) - __git_print_preprompt - ;; - esac -} - -if ! is_root; then +if false && ! is_root; then # too dangerous to be run as root autoload -U add-zsh-hook _update_vcs_prompt_vars_if_vcs_ran() { local vcs="$(__vcs_get_repo_type)" - case "$(history $(($HISTCMD - 1)))" in + case "$vcs/$(history $(($HISTCMD - 1)))" in # $vcs appeared in last command, so be sure to update - *${vcs}*) __vcs_set_prompt_variables "$vcs" + NONE/*) :;; + netfs/*) :;; + */*${vcs}*) __vcs_set_prompt_variables "$vcs" esac } add-zsh-hook precmd _update_vcs_prompt_vars_if_vcs_ran @@ -305,65 +239,8 @@ if ! is_root; then } add-zsh-hook chpwd _update_vcs_prompt_vars - _print_preprompt() { - [[ $? -eq 0 ]] && __vcs_print_preprompt - } - add-zsh-hook precmd _print_preprompt - # call it once _update_vcs_prompt_vars fi -function make_ps1() { - # start with '+' if in a subshell - echo -n '%(2L.+.)' - - # the machine name, bold or underlined based on non-root/root - local ps1_hl=B - is_root && ps1_hl=U - echo -n "%${ps1_hl:=B}%m%${(L)ps1_hl}" - - # if we're in a Debian chroot, make that stand out - echo -n "${DEBIAN_CHROOT:+/%S$DEBIAN_CHROOT%s}" - - # we end this with a : - echo -n : - - # now comes the working directory, composed from parts in $psvar, - # which is managed by $ZDOTDIR/zshrc/06-vcsprompt - echo -n '%1v%(2v.|%B%2v%b|.)%(3v.%3v.)' - - # and we finish with #/% for root/non-root, and a space - echo -n '%# ' - echo -} -PS1=$(make_ps1) -unfunction make_ps1 -PS2="%{$fg[red]%}%_>%{$reset_color%}" - -function make_rps1() { - # First, a comment character and parens - echo -n '#(' - - # Next, if the returncode was non-zero, make it stand-out - # and include a trailing space - echo -n "%(0?..%{$fg[red]%}%S%?%s%{$reset_color%} )" - - # If there are background jobs, print their number, followed by - # '@': - echo -n '%(1j.%j@.)' - - # and then the terminal line we're using - echo -n '%l' - - # this concludes the first part, but there's more - echo -n ') ' - - # the timestamp will finish it off: - echo -n '%D{%d %H:%M:%S.%.}' - echo -} -RPS1=$(make_rps1) -unfunction make_rps1 - # vim:ft=zsh