X-Git-Url: https://git.madduck.net/etc/zsh.git/blobdiff_plain/e45b0db3d66ca9e041622db3d801d026fb4d3724:/.zsh/zshrc/60_vcsprompt..33230b65afa1076218abff057d0bbcc7486af0a8:/.zsh/func/prompt_madduck_setup diff --git a/.zsh/zshrc/60_vcsprompt b/.zsh/func/prompt_madduck_setup similarity index 56% rename from .zsh/zshrc/60_vcsprompt rename to .zsh/func/prompt_madduck_setup index d0d33c1..0b6dddc 100644 --- a/.zsh/zshrc/60_vcsprompt +++ b/.zsh/func/prompt_madduck_setup @@ -1,17 +1,22 @@ -# zshrc/60_vcsprompt # -# Make git information available to the prompt +# My zsh prompt theme # # Copyright © 1994–2017 martin f. krafft # Released under the terms of the Artistic Licence 2.0 # # Source repository: http://git.madduck.net/v/etc/zsh.git # -# Shamelessly based on http://glandium.org/blog/?p=170 +# vcs stuff shamelessly based on http://glandium.org/blog/?p=170 # +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 + __on_networkfs() { + emulate -L zsh case $(df -T . | sed -rne '$s,^[^[:space:]]+[[:space:]]+([^[:space:]]+).*,\1,p') in (cifs|nfs) return 0;; esac @@ -20,6 +25,7 @@ __on_networkfs() __git_get_reporoot() { + 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)" @@ -29,6 +35,10 @@ __git_get_reporoot() __git_get_branch() { + 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)" @@ -39,34 +49,73 @@ __git_get_branch() __git_print_preprompt() { + emulate -L zsh [ "$(git config --get core.bare)" = false ] || return __on_networkfs && return - local output - output=(${(f):-"$(git diff --stat --relative 2>/dev/null)"}) - if [[ ${#output} -gt 1 ]]; then - echo changes on filesystem: - print "${${(F)output[1,-2]}//\.\.\./…}" - fi - output=(${(f):-"$(git diff --cached --stat --relative 2>/dev/null)"}) - if [[ ${#output} -gt 1 ]]; then - echo cached/staged changes: - print "${${(F)output[1,-2]}//\.\.\./…}" + 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 + } + + local cached=(${(f)"$(gitdiffstat --cached | output cached)"}) + local changed=(${(f)"$(gitdiffstat | output changed)"}) + + local max=${#changed} + [[ $max -lt ${#cached} ]] && max=${#cached} + + ((max == 0)) && return + + local width=$(((COLUMNS-3)/2)) + + 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 } __hg_get_reporoot() { + emulate -L zsh hg root } __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 @@ -77,6 +126,7 @@ __bzr_get_reporoot() __bzr_get_branch() { + emulate -L zsh local branch revno bzr version-info | while read i j; do case "$i" in @@ -89,6 +139,7 @@ __bzr_get_branch() __vcs_get_repo_type() { + emulate -L zsh # return the type of the closest repository in the path hierarchy local dir while true; do @@ -101,14 +152,27 @@ __vcs_get_repo_type() 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() { + emulate -L zsh # return formatted path components (prefix branch postfix) given # the repository root and the branch. + local MAXLEN MINLEN + MAXLEN=$(__get_prompt_path_len max) + MINLEN=$(__get_prompt_path_len min) + # shortcut: if there are no arguments, return a default prompt if [ -z "${1:-}" ]; then - pwdnamed="${(%):-%${_PROMPT_PATH_MAXLEN}<…<%~%<<}" + pwdnamed="${(%):-%${MAXLEN}<…<%~%<<}" echo "$pwdnamed" return fi @@ -140,11 +204,11 @@ __vcs_get_prompt_path_components() (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}" # we don't want the prompt to get too long, so keep the total prompt length - # under $_PROMPT_PATH_MAXLEN (25), but ensure that the prefix is not shorter - # than $_PROMPT_PATH_MINLEN (10), no matter what + # under $MAXLEN, but ensure that the prefix is not shorter + # than $MINLEN, no matter what local prelen minlen prefix - prelen=$((${_PROMPT_PATH_MAXLEN:-25} - $#branch - $#postfix)) - minlen=${_PROMPT_PATH_MINLEN:-10} + prelen=$((${MAXLEN} - $#branch - $#postfix)) + minlen=${MINLEN} (( $prelen < $minlen )) && prelen=$minlen prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}" @@ -153,6 +217,7 @@ __vcs_get_prompt_path_components() __vcs_set_prompt_variables() { + emulate -L zsh # set psvar[1..3] depending on repo type, or just psvar[1] if no repo found local reporoot branch repotype repotype="${1:-$(__vcs_get_repo_type)}" @@ -160,14 +225,14 @@ __vcs_set_prompt_variables() case "$repotype" in git) reporoot="$(__git_get_reporoot)" || - { error "could not determine git repository root"; return 1 } + { zerror "could not determine git repository root"; return 1 } branch="$(__git_get_branch)" || - { error "could not determine git branch"; return 1 } + { zerror "could not determine git branch"; return 1 } if [ -n "$VCSH_REPO_NAME" ]; then # if vcsh is used to get a subshell, then the repo root is the home # directory, but we want to indicate the vcsh context too: eval set -- $(__vcs_get_prompt_path_components "$HOME" "$branch") - set -- "vcsh:$VCSH_REPO_NAME" "$2" "$3" + set -- "vcsh:$VCSH_REPO_NAME" "$2" "$1${3:+/$3}" else eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") if [ -d "$GIT_DIR" ]; then @@ -180,16 +245,16 @@ __vcs_set_prompt_variables() ;; hg) reporoot="$(__hg_get_reporoot)" || - { error "could not determine hg repository root"; return 1 } + { zerror "could not determine hg repository root"; return 1 } branch="$(__hg_get_branch)" || - { error "could not determine hg branch"; return 1 } + { zerror "could not determine hg branch"; return 1 } eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") ;; bzr) reporoot="$(__bzr_get_reporoot)" || - { error "could not determine bzr repository root"; return 1 } + { zerror "could not determine bzr repository root"; return 1 } branch="$(__bzr_get_branch)" || - { error "could not determine bzr branch"; return 1 } + { zerror "could not determine bzr branch"; return 1 } eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") ;; *) @@ -197,6 +262,8 @@ __vcs_set_prompt_variables() NONE) :;; *) warn "$repotype repositories not (yet) supported in the prompt";; esac + local MAXLEN MINLEN + MAXLEN=$(__get_prompt_path_len max) local p="%${MAXLEN}<…<%~%<<" #TODO find a better way so we don't have to nuke $psvar, but since the # %(nv.true.false) check for prompts checks element count, not @@ -210,8 +277,8 @@ __vcs_set_prompt_variables() __vcs_print_preprompt() { - local reporoot - repotype="${1:-$(__vcs_get_repo_type)}" + emulate -L zsh + local repotype="${1:-$(__vcs_get_repo_type)}" case "$repotype" in git) @@ -222,6 +289,7 @@ __vcs_print_preprompt() if ! 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)" @@ -230,20 +298,72 @@ if ! is_root; then *${vcs}*) __vcs_set_prompt_variables "$vcs" esac } - precmd_functions+=_update_vcs_prompt_vars_if_vcs_ran + add-zsh-hook precmd _update_vcs_prompt_vars_if_vcs_ran _update_vcs_prompt_vars() { __vcs_set_prompt_variables } - chpwd_functions+=_update_vcs_prompt_vars + add-zsh-hook chpwd _update_vcs_prompt_vars _print_preprompt() { [[ $? -eq 0 ]] && __vcs_print_preprompt } - precmd_functions+=_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