# # 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 # # 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 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() { emulate -L zsh case $(df -T . | sed -rne '$s,^[^[:space:]]+[[:space:]]+([^[:space:]]+).*,\1,p') in (cifs|nfs) return 0;; esac return 1 } __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" } __get_prompt_psvar_style() { emulate -L zsh 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" } # 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 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 } typeset -g RPS1="$(make_rps1)" unfunction make_rps1 __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="${(%):-%${MAXLEN}<…<%~%<<}" echo "$pwdnamed" return fi local reporoot branch reporoot="${1%%/}" branch="$2" # replace named directories in the PWD, we need thi for the proper component # count later local pwdnamed pwdnamed="${(%):-%~}" # store paths in arrays for component count calculation typeset -la apwd apwdnamed areporoot apwd=(${(s:/:)PWD}) apwdnamed=(${(s:/:)pwdnamed}) areporoot=(${(s:/:)reporoot}) # get the number of leading and trailing path components. Since we're using # %~ later and then /home/madduck suddenly becomes ~, which is 1, not # 2 components, we calculate the leading component count by using the named # path and the number of post components local precomps postcomps postcomps=$(($#apwd - $#areporoot)) precomps=$(($#apwdnamed - $postcomps)) local postfix (( $postcomps > 0 )) && postfix="${(%):-%${postcomps}~}" # we don't want the prompt to get too long, so keep the total prompt length # under $MAXLEN, but ensure that the prefix is not shorter # than $MINLEN, no matter what local prelen minlen prefix prelen=$((${MAXLEN} - $#branch - $#postfix)) minlen=${MINLEN} (( $prelen < $minlen )) && prelen=$minlen prefix="${(%):-%${prelen}<…<%-${precomps}~%<<}" echo "'$prefix'" "'$branch'" "'$postfix'" } __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)}" case "$repotype" in git) reporoot="$(__git_get_reporoot)" || { zerror "could not determine git repository root"; return 1 } branch="$(__git_get_branch)" || { 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" "$1${3:+/$3}" else eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") if [ -d "$GIT_DIR" ]; then set -- "${(D)GIT_DIR}" "$2" "${${1#$_D}%/}" fi fi ;; hg) reporoot="$(__hg_get_reporoot)" || { zerror "could not determine hg repository root"; return 1 } branch="$(__hg_get_branch)" || { zerror "could not determine hg branch"; return 1 } eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") ;; bzr) reporoot="$(__bzr_get_reporoot)" || { zerror "could not determine bzr repository root"; return 1 } branch="$(__bzr_get_branch)" || { zerror "could not determine bzr branch"; return 1 } eval set -- $(__vcs_get_prompt_path_components "$reporoot" "$branch") ;; *) case "$repotype" in NONE|netfs) :;; *) 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 # content, that's all we get for now psvar=("${(%)p}") return esac psvar[1,3]=($1 $2 $3) } 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 "$vcs/$(history $(($HISTCMD - 1)))" in # $vcs appeared in last command, so be sure to update NONE/*) :;; netfs/*) :;; */*${vcs}*) __vcs_set_prompt_variables "$vcs" esac } add-zsh-hook precmd _update_vcs_prompt_vars_if_vcs_ran _update_vcs_prompt_vars() { __vcs_set_prompt_variables } add-zsh-hook chpwd _update_vcs_prompt_vars # call it once _update_vcs_prompt_vars fi # vim:ft=zsh