John Ankarström avatar John Ankarström committed c779545

Git and Mercurial prompt info

Comments (0)

Files changed (6)

+# get the name of the branch we are on
+function git_prompt_info() {
+  ref=$(git symbolic-ref HEAD 2> /dev/null) || return
+  echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX"
+}
+
+# Checks if working tree is dirty
+parse_git_dirty() {
+  if [[ -n $(git status -s 2> /dev/null) ]]; then
+    echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
+  else
+    echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
+  fi
+}
+
+# Checks if there are commits ahead from remote
+function git_prompt_ahead() {
+  if $(echo "$(git log origin/$(current_branch)..HEAD 2> /dev/null)" | grep '^commit' &> /dev/null); then
+    echo "$ZSH_THEME_GIT_PROMPT_AHEAD"
+  fi
+}
+
+# Formats prompt string for current git commit short SHA
+function git_prompt_short_sha() {
+  SHA=$(git rev-parse --short HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
+}
+
+# Formats prompt string for current git commit long SHA
+function git_prompt_long_sha() {
+  SHA=$(git rev-parse HEAD 2> /dev/null) && echo "$ZSH_THEME_GIT_PROMPT_SHA_BEFORE$SHA$ZSH_THEME_GIT_PROMPT_SHA_AFTER"
+}
+
+# Get the status of the working tree
+git_prompt_status() {
+  INDEX=$(git status --porcelain 2> /dev/null)
+  STATUS=""
+  if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
+  fi
+  if $(echo "$INDEX" | grep '^A  ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
+  elif $(echo "$INDEX" | grep '^M  ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
+  fi
+  if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
+  elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
+  elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
+  fi
+  if $(echo "$INDEX" | grep '^R  ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
+  fi
+  if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
+  elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
+  fi
+  if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
+    STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
+  fi
+  echo $STATUS
+}
+function hg_prompt_info {
+    hg prompt --angle-brackets "%{$PR_BLUE%}[<branch>%{$PR_GREEN%}<status|modified|unknown>%{$PR_BLUE%}]%{$PR_NO_COLOR%}" 2>/dev/null
+}

load/functions.zsh

 load fn/dir
 load fn/str
 load fn/syntax/zsh-syntax-highlighting
-load fn/history-substring-search/zsh-history-substring-search
+load fn/git
+load fn/hg
 setopt CompleteInWord      2>/dev/null
     # Attempt to spell-check command names - I mistype a lot.
 setopt Correct             2>/dev/null
+    # Allow variable substitution in prompt
+setopt prompt_subst        2>/dev/null
 PR_NO_COLOR="%{$terminfo[sgr0]%}"
 
 function prompt-setup {
-    PROMPT="${PR_GREEN}$(whoami) ${PR_WHITE}%~ ${PR_NO_COLOR}"
-    RPROMPT="%{$PR_YELLOW%}%D{%H:%M}%{$PR_NO_COLOR%}"
+    ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[blue]%}["
+    ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%}"
+    ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[green]%}!"
+    ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?"
+    ZSH_THEME_GIT_PROMPT_CLEAN=""
+
+    PROMPT="%{$PR_GREEN%}%n %{$PR_WHITE%}%~ %{$PR_NO_COLOR%}"
+    RPROMPT='%{$PR_BLUE%}$(git_prompt_info)$(hg_prompt_info) %{$PR_YELLOW%}%D{%H:%M}%{$PR_NO_COLOR%}'
 }
 
 prompt-setup
 load helper
 load history
 load completion
-source $ZDOTDIR/load/prompt.zsh
+load prompt
+load fn/history-substring-search/zsh-history-substring-search # didn't work in functions.zsh for some reason
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.