summary refs log tree commit diff
path: root/.zsh/gitprompt.zsh
diff options
context:
space:
mode:
Diffstat (limited to '.zsh/gitprompt.zsh')
-rw-r--r--.zsh/gitprompt.zsh80
1 files changed, 80 insertions, 0 deletions
diff --git a/.zsh/gitprompt.zsh b/.zsh/gitprompt.zsh
new file mode 100644
index 00000000..433481b9
--- /dev/null
+++ b/.zsh/gitprompt.zsh
@@ -0,0 +1,80 @@
+# Ported from http://sebastiancelis.com/2009/11/16/zsh-prompt-git-users/
+
+function _gitprompt_update {
+  unset _git_branch
+  unset _git_status
+  unset _git_dirty
+
+  local st="$(git status 2> /dev/null)"
+  if [[ -n "$st" ]]; then
+    local -a arr
+    arr=(${(f)st})
+
+    if [[ $arr[1] =~ 'Not currently on any branch.' ]]; then
+      _git_branch='none'
+    else
+      _git_branch="${arr[1][(w)-1]}"
+    fi
+
+    if [[ $arr[2] =~ 'Your branch is' ]]; then
+      if [[ $arr[2] =~ 'ahead' ]]; then
+        _git_status='ahead'
+      elif [[ $arr[2] =~ 'diverged' ]]; then
+        _git_status='diverged'
+      else
+        _git_status='behind'
+      fi
+    fi
+
+    if [[ ! $st =~ 'nothing' ]]; then
+      _git_dirty=1
+    fi
+  fi
+}
+
+function gitprompt {
+  if [[ -n "$_git_branch" ]]; then
+    local s
+    [[ -z "$1" ]] && s="%{${fg[yellow]}%}"
+
+    if [[ -n "$_git_dirty" ]]; then
+      s+="⚡"
+    else
+      s+=":"
+    fi
+
+    s+="$_git_branch"
+    case "$_git_status" in
+      ahead)
+        s+="↑"
+        ;;
+      diverged)
+        s+="↕"
+        ;;
+      behind)
+        s+="↓"
+        ;;
+    esac
+
+    echo "$s"
+  fi
+}
+
+function _gitprompt_preexec {
+  [[ "$1" =~ "^g" ]] && _git_command=1
+}
+
+function _gitprompt_precmd {
+  if [[ -n "$_git_command" ]]; then
+    _gitprompt_update
+    unset _git_command
+  fi
+}
+
+typeset -ga preexec_functions
+typeset -ga precmd_functions
+typeset -ga chpwd_functions
+
+preexec_functions+='_gitprompt_preexec'
+precmd_functions+='_gitprompt_precmd'
+chpwd_functions+='_gitprompt_update'
6Add The Just CityJune McEnroe 2019-09-12Only GET the final redirect locationJune McEnroe 2019-09-12Consume entire bodyJune McEnroe 2019-09-10Add title -v flagJune McEnroe 2019-09-10Use curl error bufferJune McEnroe 2019-09-10Set Accept-Encoding in titleJune McEnroe 2019-09-08Set title User-AgentJune McEnroe 2019-09-07Add -x flag to titleJune McEnroe 2019-09-07Ignore SIGPIPE in relayJune McEnroe 2019-09-07Add A Memory Called EmpireJune McEnroe 2019-09-05Handle lack of Content-TypeJune McEnroe 2019-09-05Use CURLINFO_CONTENT_TYPEJune McEnroe 2019-09-05Decode entities in titlesJune McEnroe 2019-09-05Print title as soon as it's availableJune McEnroe 2019-09-05Use CURL_PREFIX to set flagsJune McEnroe 2019-09-05Add titleJune McEnroe 2019-09-04Add Avorter n'est pas tuerJune McEnroe 2019-08-29Unset executable on shell scriptsJune McEnroe 2019-08-29Add long-missing setopt to bin.7June McEnroe 2019-08-29Add editJune McEnroe