]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - zsh/rc
zsh/rc: Display staged/unstaged info next to cwd in prompt.
[config/dotfiles.git] / zsh / rc
diff --git a/zsh/rc b/zsh/rc
index dea48042bb4ee6ff80c5bc56b1acbcad5e75e700..c6691783224feb182fc95edfc7f521d98fbad12b 100644 (file)
--- a/zsh/rc
+++ b/zsh/rc
 source_debug '. ~/.zsh/rc'
 
 
+# Warn when creating global variables from inside a function. Needs to be set
+# before declaring a function.
+setopt warn_create_global
+
+
 # HELPER FUNCTIONS
 
 # Return the name of the program which is called in the foreground with `fg`.
@@ -229,16 +234,18 @@ if [[ $ZSH_VERSION == (4.3.<9->*|4.<4->*|<5->*) ||
     if [[ $ZSH_VERSION == (4.3.<11->*|4.<4->*|<5->*) ||
           -d ~/.zsh/functions/vcs_info ]]; then
         zstyle ':vcs_info:*' formats \
-            "($green%b%u%c$default:$blue%s$default)"
+            "(${green}%b%u%c${default}:${blue}%s${default})" \
+            "${green}%u%c${default}"
         zstyle ':vcs_info:*' actionformats \
-            "($green%b%u%c$default/$red%a$default:$blue%s$default)"
+            "(${green}%b%u%c${default}/${red}%a${default}:${blue}%s${default})" \
+            "${green}%u%c${default}"
     else
         # In older versions %u and %c are not defined yet and are not
         # correctly expanded.
         zstyle ':vcs_info:*' formats \
-            "($green%b$default:$blue%s$default)"
+            "(${green}%b${default}:${blue}%s${default})"
         zstyle ':vcs_info:*' actionformats \
-            "($green%b$default/$red%a$default:$blue%s$default)"
+            "(${green}%b${default}/${red}%a${default}:${blue}%s${default})"
     fi
     # Set style for formats/actionformats when unstaged (%u) and staged (%c)
     # changes are detected in the repository; check-for-changes must be set to
@@ -362,6 +369,7 @@ zshrc_prompt_precmd() {
         vcs_info
     else
         vcs_info_msg_0_=
+        vcs_info_msg_1_=
     fi
 
     # Setup. Create variables holding the formatted content.
@@ -369,6 +377,9 @@ zshrc_prompt_precmd() {
     # Current directory in yellow, truncated if necessary (WIDTH is replaced
     # below).
     local directory="${yellow}%WIDTH<..<%~%<<${default}"
+    # Minimal information about the VCS, only displayed if there are
+    # unstaged/staged changes.
+    local vcs_staged="${vcs_info_msg_1_}"
 
     # Information about the VCS in this directory.
     local vcs="${vcs_info_msg_0_}"
@@ -395,20 +406,25 @@ zshrc_prompt_precmd() {
 
     # Combine them to create the prompt.
 
+    local top_left="${vcs_staged}"
     local top_right="${vcs}(${seconds})"
 
     local width_top_prefix=${#${(S%%)top_prefix//$~zero/}}
+    local width_top_left=${#${(S%%)top_left//$~zero/}}
     local width_top_right=${#${(S%%)top_right//$~zero/}}
 
     # Calculate the maximum width of ${top_left}. -2 are the braces of
     # ${top_left}, -1 is one separator from ${top_separator} (we want at least
     # one between left and right parts).
     local top_left_width_max=$((
-        COLUMNS - $width_top_prefix - 2 - 1 - $width_top_right
+        COLUMNS - $width_top_prefix
+                - $width_top_left - 2
+                - 1
+                - $width_top_right
     ))
     # Truncate directory if necessary.
-    local top_left="(${directory/WIDTH/${top_left_width_max}})"
-    local width_top_left=${#${(S%%)top_left//$~zero/}}
+    top_left="(${directory/WIDTH/${top_left_width_max}})${top_left}"
+    width_top_left=${#${(S%%)top_left//$~zero/}}
 
     # Calculate the width of the top prompt to fill the middle with "-".
     local width=$((
@@ -444,10 +460,6 @@ if [[ $TERM == screen* || $TERM == xterm* || $TERM == rxvt* ]]; then
     # Is set to a non empty value to reset the window name in the next
     # precmd() call.
     zshrc_window_reset=yes
-    # Is set to a non empty value when the shell is running as root.
-    if [[ $UID -eq 0 ]]; then
-        zshrc_window_root=yes
-    fi
 
     zshrc_window_preexec() {
         # Get the program name with its arguments.
@@ -492,7 +504,7 @@ if [[ $TERM == screen* || $TERM == xterm* || $TERM == rxvt* ]]; then
 
         # Add an exclamation mark at the beginning if running with sudo or if
         # running zsh as root.
-        if [[ -n $program_sudo || -n $zshrc_window_root ]]; then
+        if [[ -n $program_sudo || $UID -eq 0 ]]; then
             program_name=!$program_name
         fi
 
@@ -531,7 +543,7 @@ if [[ $TERM == screen* || $TERM == xterm* || $TERM == rxvt* ]]; then
         fi
 
         # Prepend prefixes like in window_preexec().
-        if [[ -n $zshrc_window_root ]]; then
+        if [[ $UID -eq 0 ]]; then
             name="!$name"
         fi
         if [[ -n $SSH_CONNECTION ]]; then