]> git.alrj.org Git - zsh.d.git/commitdiff
Rework the prompt config.
authorAmand Tihon <amand.tihon@iba-group.com>
Wed, 4 May 2011 09:26:51 +0000 (11:26 +0200)
committerAmand Tihon <amand.tihon@iba-group.com>
Wed, 4 May 2011 14:55:11 +0000 (16:55 +0200)
 * Move colors definitions in its own file.
 * Use prompt_subst for the path part, including git status when applicable.
 * Rework the battery status part of the prompt.
 * Rework the exit status.
 * Show the exit status only once, clear on empty line.
 * Rework git status, now clean of psvar
 * Now free of psvar.

S10_zshopts
S11_colors [new file with mode: 0644]
S22_exitcode
S60_git
S61_konsoletabs
S70_battery
S90_prompt

index 2697a16f3b2d7161281b34d9c1be6962c660af80..36840e29ade5b8684025cf0347ad0f131597cf8e 100644 (file)
@@ -20,13 +20,6 @@ typeset -ga chpwd_functions
 # use emacs bindings
 bindkey -e
 
-# This must be done very early.
-clear_psvar() {
-  psvar=()
-}
-precmd_functions+='clear_psvar'
-
-
 [ -w $zsh_cache ] && HISTFILE=$zsh_cache/history
 
 HISTSIZE=10000              # size of history
diff --git a/S11_colors b/S11_colors
new file mode 100644 (file)
index 0000000..6fb4572
--- /dev/null
@@ -0,0 +1,19 @@
+#! /usr/bin/zsh
+
+#
+# Prepare the colors for the prompt
+#
+C_NO="%{$reset_color%}"
+
+# normal
+C_CYAN="%{${fg[cyan]}%}"
+C_WHITE="%{${fg[white]}%}"
+C_MAGENTA="%{${fg[magenta]}%}"
+C_GREEN="%{${fg[green]}%}"
+
+# bright/bold
+C_BCYAN="%{${fg_bold[cyan]}%}"
+C_BYELLOW="%{${fg_bold[yellow]}%}"
+C_BRED="%{${fg_bold[red]}%}"
+C_MAGENTA="%{${fg_bold[magenta]}%}"
+C_BGREEN="%{${fg_bold[green]}%}"
index 230294ddde62482a0b99b9b767177a7fe6d0b9d8..003c9f0ed7d4b2f010c23116fe7458354cbf24b7 100644 (file)
@@ -3,12 +3,15 @@
 
 parse_exitcode() {
   EX=`print -P %?`
-  psvar[1]=$EX
+  
+  [[ $EX -eq 0 ]] && return
 
   if [[ $EX -ge 128 && $EX -le (127+${#signals}) ]]
   then
-    psvar[1]="${signals[${EX}-127]}"
+    EX=${signals[${EX}-127]}
   fi
-}
+  
+  echo "${C_BRED}[ $EX ]
+${C_NO}"
 
-precmd_functions+='parse_exitcode'
+}
diff --git a/S60_git b/S60_git
index 2972c391444caa3727df116269b7b3969959cee8..256afc8c08ec9951e9cb3c1abfd52ad442937ea2 100644 (file)
--- a/S60_git
+++ b/S60_git
@@ -1,10 +1,12 @@
 #! /usr/bin/zsh
 
+# Those can be used, for instance to construct the prompt:
 __ZSH_GIT_BASEDIR=""
 __ZSH_GIT_SUBDIR=""
 __ZSH_GIT_BRANCH=""
 __ZSH_GIT_ACTION=""
 __ZSH_GIT_STATUS=""
+
 __ZSH_GIT_VARS_INVALID=1
 __ZSH_GIT_STATUS_INVALID=1
 
@@ -59,40 +61,16 @@ git_get_status() {
 
 
 git_parse() {
-    # psvar[5] == current action (merge, rebase, ...)
-    # psvar[6] == current branch
-    # psvar[7] == repository base directory
-    # psvar[8] == current subdir into repository
-    # psvar[9] == status (untracked, unstaged, staged)
-
     local git_dir ref base_dir sub_dir action branch gitstat gitstatus
 
-
     # If nothing has been invalidated
     if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then
-
-      # reuse previous values
-      psvar[5]=${__ZSH_GIT_ACTION}
-      psvar[6]=${__ZSH_GIT_BRANCH}
-      psvar[7]=${__ZSH_GIT_BASEDIR}
-      psvar[8]=${__ZSH_GIT_SUBDIR}
-      psvar[9]=${__ZSH_GIT_STATUS}
-
       return
     fi
 
     # If only status has been invalidated
     if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then
-
-      # reuse previous values
-      psvar[5]=${__ZSH_GIT_ACTION}
-      psvar[6]=${__ZSH_GIT_BRANCH}
-      psvar[7]=${__ZSH_GIT_BASEDIR}
-      psvar[8]=${__ZSH_GIT_SUBDIR}
-
       __ZSH_GIT_STATUS=$(git_get_status)
-      psvar[9]=${__ZSH_GIT_STATUS}
-
       __ZSH_GIT_STATUS_INVALID=0
       return
     fi
@@ -134,28 +112,18 @@ git_parse() {
       action="-merge"
       branch="$ref"
     else
-      test -f "$git_dir/BISECT_LOG" && psvar[3]="bisect"
+      test -f "$git_dir/BISECT_LOG" && action="-bisect"
       branch="$(git symbolic-ref HEAD 2>/dev/null)" || \
         branch="$(git describe --exact-match HEAD 2>/dev/null)" || \
         branch="$(cut -c1-7 "$git_dir/HEAD")..."
     fi
 
-    # Status
-    gitstatus=`git_get_status`
-
-    # Got here, we're in git
-    psvar[5]=${action}
-    psvar[6]=${branch#refs/heads/}
-    psvar[7]=${base_dir}
-    psvar[8]=${sub_dir}
-    psvar[9]=${gitstatus}
-    
-    # Save for next time
+
     __ZSH_GIT_BASEDIR="${base_dir}"
     __ZSH_GIT_SUBDIR="${sub_dir}"
     __ZSH_GIT_BRANCH="${branch#refs/heads/}"
     __ZSH_GIT_ACTION="${action}"
-    __ZSH_GIT_STATUS="${gitstatus}"
+    __ZSH_GIT_STATUS=`git_get_status`
 
     __ZSH_GIT_VARS_INVALID=0
     __ZSH_GIT_STATUS_INVALID=0
index 58dbb9fdde8f5a82ed2c0d61190bdcf3b318b9dd..340eb1644f396eb30627a0ca998c0e0e03ece2b3 100644 (file)
@@ -1,4 +1,4 @@
-#! /usr/binn/zsh
+#! /usr/bin/zsh
 
 # Change konsole tab to current path
 konsole-rename-path () {
index 023f0042d5b018ba297f9c13645a774565ed6c3d..bdc1750df88da3a6c50d358c4b05ad33f67d89e6 100644 (file)
@@ -1,51 +1,42 @@
 #! /usr/bin/zsh
 
-#
-# Prepare a "battery level" gauge ready to be used in prompt.
-#
-
 battery_level()
 {
+  # Return a "battery level" gauge ready to be used in prompt.
+  
   local online remaining bstatus gauge gcolor
 
   online=0
   acpi -a 2> /dev/null | grep -q "on-line" && online=1
 
-
   bstatus="$(acpi -b 2> /dev/null)"
   remaining="$(echo ${bstatus[(w)4]} | sed -r 's/(^[0-9]+)%.*/\1/')"
 
   if [[ -z "$remaining" ]]; then
-    psvar[2]=""
     return
   elif [[ "$remaining" -eq "100" && "$online" -eq "1" ]]; then
-    psvar[2]=""
     return
-  #elif [[ "$remaining" -gt "95" ]]; then
-  #  export BATT_GAUGE=""
-  #  return
   elif [[ "$remaining" -lt "10" ]]; then
-    psvar[2]="►"
-    psvar[3]="red"
+    echo "${C_RED}►${C_NO}"
     return
   fi
 
   gauge=""
-  gcolor="green"
+  rgauge=""
+  gcolor=${C_BGREEN}
 
   for i in $(seq ${remaining[1]}); do
     gauge="${gauge}►"
   done
+  for i in $(seq $(( 10 - ${remaining[1]})) ); do
+    rgauge="${rgauge}·"
+  done
 
   if [[ "${#gauge}" -lt "3" ]]; then
-    gcolor="red"
+    gcolor=${C_BRED}
   elif [[ "${#gauge}" -lt "6" ]]; then
-    gcolor="yellow"
+    gcolor=${C_BYELLOW}
   fi
 
-  psvar[2]="${gauge}"
-  psvar[3]="${gcolor}"
-
+  echo "${gcolor}${gauge}${C_NO}${C_WHITE}${rgauge}${C_NO}"
 }
-
-precmd_functions+="battery_level"
index 10832a2d896c751adc9909302a298541197c440c..b9a7d4e9bb85d349e2fe92aee747a2e873bd66bb 100644 (file)
@@ -1,39 +1,53 @@
 #! /usr/bin/zsh
 
-#
-# Prepare colors, ignoring length
-#
-C_NO="%{$reset_color%}"
-C_BYELLOW="%{${fg_bold[yellow]}%}"
-C_BRED="%{${fg_bold[red]}%}"
-C_BCYAN="%{${fg_bold[cyan]}%}"
-C_GREEN="%{${fg[green]}%}"
-C_BGREEN="%{${fg_bold[green]}%}"
-C_MAGENTA="%{${fg_bold[magenta]}%}"
-
-
 
 #
-# Prompt parts
+# Exit code.
 #
-_username="${C_BYELLOW}%n${C_NO}"
-_machine="${C_BRED}%m${C_NO}"
-_hour="${C_BCYAN}[%D{%H:%M}]${C_NO}"
-
-_exitcode="%(?::${C_BRED}[ %1v ]${C_NO}
-)$(true)"
+# Hide exit code after a blank line.
+# Inspired from https://github.com/robbyrussell/oh-my-zsh/blob/66b7fe1b27637feba61a4b47e113b18b69432bf2/themes/dieter.zsh-theme
+
+
+get_exitcode=true
+
+function accept-line-or-clear-warning () {
+  if [[ -z $BUFFER ]]; then
+    get_exitcode=true
+  else
+    get_exitcode=parse_exitcode
+  fi
+  zle accept-line
+}
+zle -N accept-line-or-clear-warning
+bindkey '^M' accept-line-or-clear-warning
+
+
+setprompt()
+{
+  # This function sets the entire prompt.
+  # It requires that prompt_subst is set.
+  
+  _username="${C_BYELLOW}%n${C_NO}"
+  _machine="${C_BRED}%m${C_NO}"
+
+  # Git status in the pwd, if applicable
+  if [[ -n $__ZSH_GIT_BASEDIR ]]; then
+    _basedir="${C_GREEN}%20<..<${__ZSH_GIT_BASEDIR}%<<${C_NO}"
+    _branch="${C_MAGENTA}@${__ZSH_GIT_BRANCH}${__ZSH_GIT_ACTION}${C_NO}"
+    _status="${__ZSH_GIT_STATUS}"
+    _subdir="${C_GREEN}/%15<..<${__ZSH_GIT_SUBDIR}%<<${C_NO}"
+    _path="${_basedir}${_branch}${_status}${_subdir}"
+  else
+    _path="${C_GREEN}%25<..<%~%<<${C_NO}"
+  fi
+  
+  
+  echo ${_username}@${_machine}:${_path}
+}
 
 
-_action="%(5v,%5v%,)"
-_path='${C_GREEN}%(7v,%20<..<%7v%<<${C_NO}${C_MAGENTA}@%6v%5v${__ZSH_GIT_STATUS}${C_GREEN}/%15<..<%8v%<<,%25<..<%~%<<)${C_NO}'
-
-
-_batt='%{${fg_bold[$psvar[3]]}%}%2v%{$reset_color%} '
-
 #
 # Now, the prompt itself
 #
-#PROMPT="${_exitcode}${_hour} ${_username}@${_machine}:${_vcs}${_path}$ "
-PROMPT="${_exitcode}${_username}@${_machine}:${_vcs}${_path}$ "
-
-RPROMPT="${_batt}"
\ No newline at end of file
+PROMPT='$($get_exitcode)$(setprompt)$ '
+RPROMPT='$(battery_level)'