]> git.madduck.net Git - etc/zsh.git/commitdiff

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

move Git prompt handling to theme
authormartin f. krafft <madduck@madduck.net>
Thu, 28 Feb 2019 03:15:28 +0000 (16:15 +1300)
committermartin f. krafft <madduck@madduck.net>
Thu, 28 Feb 2019 03:15:28 +0000 (16:15 +1300)
.zsh/themes/prompt_madduck_setup
.zsh/zshrc/99-prompt_setup

index c9a04bc251f3cc3ec0316efe51178adccceb57d1..14e85ef665af0a2d15fd6a7671cf04ce7818c679 100644 (file)
@@ -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,147 +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; 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]="$DEBIAN_CHROOT"
+  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; cached=(${(f)"$(gitdiffstat --cached | output cached)"})
-  local changed; 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
-  # unless we're on a network filesystem:
-  if __on_networkfs; then
-    echo netfs
-    return
-  fi
-  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()
 {
@@ -241,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
       ;;
@@ -280,19 +219,7 @@ __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
 
@@ -312,66 +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, unless psvar[4] is set, and
-  # a space
-  echo -n "%(4V.%4v.%#) "
-  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
-}
-typeset -g RPS1="$(make_rps1)"
-unfunction make_rps1
-
 # vim:ft=zsh
index 62101c9d8a8480286a1ac6fc308e7e9748a0166b..115e796990555466efcbc97719d7bc16a699b96c 100644 (file)
@@ -7,6 +7,8 @@
 # Source repository: http://git.madduck.net/v/etc/zsh.git
 #
 
+setopt prompt_subst
+
 autoload -Uz promptinit
 declare -la fpath_saved
 fpath_saved=($fpath)