dotfiles

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit f69f2dcfdfdc21533c7c6e3ed4d6495772afb69c
parent 2b9895e355649b7bcdb285fce3c51df29e72a069
Author: Janis Pagel <janis.pagel@ims.uni-stuttgart.de>
Date:   Tue, 22 Jan 2019 14:55:25 +0100

Clean up git prompt

Diffstat:
Mzsh/zsh_functions | 48++++++++++++++++++++++++++++++++++++++++++++++++
Mzsh/zshrc | 150+++++++++++++++++++------------------------------------------------------------
2 files changed, 83 insertions(+), 115 deletions(-)

diff --git a/zsh/zsh_functions b/zsh/zsh_functions @@ -22,3 +22,51 @@ function get_stripped_dir_count() { unset SUB unset RES } + +# Git + +parse_git_branch() { +# Show Git branch/tag, or name-rev if on detached head +( git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD ) 2> /dev/null +} + +parse_git_state() { + # Show different symbols as appropriate for various Git repository states + # Compose this value via multiple conditional appends. + local GIT_STATE="" + local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')" + if [ "$NUM_AHEAD" -gt 0 ]; then + GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD} + fi + local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')" + if [ "$NUM_BEHIND" -gt 0 ]; then + GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND} + fi + local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)" + if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then + GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING + fi + if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then + GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED + fi + if ! git diff --quiet 2> /dev/null; then + GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED + fi + if ! git diff --cached --quiet 2> /dev/null; then + GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED + fi + if [[ -n $GIT_STATE ]]; then + echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX" + fi +} + +git_prompt_string() { + local git_where="$(parse_git_branch)" + + # If inside a Git repository, print its branch and state + [ -n "$git_where" ] && echo "$GIT_PROMPT_SYMBOL$(parse_git_state)$GIT_PROMPT_PREFIX%{$fg[yellow]%}${git_where#(refs/heads/|tags/)}$GIT_PROMPT_SUFFIX" + + # If not inside the Git repo, print exit codes of last command (only if it failed) + [ ! -n "$git_where" ] && echo "%{$fg[red]%} %(?..[%?])" +} + diff --git a/zsh/zshrc b/zsh/zshrc @@ -1,3 +1,12 @@ +################# +### Functions ### +################# + +# Source functions +if [ -f ~/.zsh_functions ]; then + source ~/.zsh_functions +fi + ######################## #### Autocomplete ###### ######################## @@ -28,6 +37,13 @@ zstyle ':completion:*' cache-path ~/.zsh/cache # Get completion for hostname if in ~/.ssh/config _hosts() { compadd $(getent hosts | tr -s ' ' '\t' | cut -f2) } +# Use autosuggestion +if [ -f /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ]; then + source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh + ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20 + ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8' +fi + ####################### ### Options ########### ####################### @@ -83,17 +99,28 @@ colors ####### Prompt ####### ###################### -# set prompt +# set left prompt PROMPT='%b%# %E' -################# -### Functions ### -################# +# set right prompt +###### +# This whole git prompt stuff was taken from Manjaro's zsh setup, slightly modified +###### -# Source functions -if [ -f ~/.zsh_functions ]; then - source ~/.zsh_functions -fi +# Modify the colors and symbols in these variables as desired. +GIT_PROMPT_SYMBOL="%{$fg[blue]%}±" # plus/minus - clean repo +GIT_PROMPT_PREFIX="%{$fg[white]%}[%{$reset_color%}" +GIT_PROMPT_SUFFIX="%{$fg[white]%}]%{$reset_color%}" +GIT_PROMPT_AHEAD="%{$fg[red]%}ANUM%{$reset_color%}" # A"NUM" - ahead by "NUM" commits +GIT_PROMPT_BEHIND="%{$fg[cyan]%}BNUM%{$reset_color%}" # B"NUM" - behind by "NUM" commits +GIT_PROMPT_MERGING="%{$fg_bold[magenta]%}!%{$reset_color%}" # merge conflict +GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}+%{$reset_color%}" # untracked files +GIT_PROMPT_MODIFIED="%{$fg_bold[yellow]%}+%{$reset_color%}" # tracked files modified +GIT_PROMPT_STAGED="%{$fg_bold[green]%}->%{$reset_color%}" # staged changes present = ready for "git push" +RPROMPT='$(git_prompt_string)' + +# Right prompt with exit status of previous command if not successful +RPROMPT="$RPROMPT %{$fg[red]%} %(?..[%?])" ################# ##### Misc ###### @@ -113,113 +140,6 @@ if [ -n "$TMUX" ]; then # If tmux is running export PS1=$PS1'$( [ -n $TMUX ] && tmux setenv -g TMUX_PWD_$(tmux display -p "#D" | tr -d %) $PWD)' fi -###### -# This whole git prompt stuff was taken from Manjaro's zsh setup, slightly modified -###### - -# Modify the colors and symbols in these variables as desired. -GIT_PROMPT_SYMBOL="%{$fg[blue]%}±" # plus/minus - clean repo -GIT_PROMPT_PREFIX="%{$fg[green]%}[%{$reset_color%}" -GIT_PROMPT_SUFFIX="%{$fg[green]%}]%{$reset_color%}" -GIT_PROMPT_AHEAD="%{$fg[red]%}ANUM%{$reset_color%}" # A"NUM" - ahead by "NUM" commits -GIT_PROMPT_BEHIND="%{$fg[cyan]%}BNUM%{$reset_color%}" # B"NUM" - behind by "NUM" commits -GIT_PROMPT_MERGING="%{$fg_bold[magenta]%}⚡%{$reset_color%}" # lightning bolt - merge conflict -GIT_PROMPT_UNTRACKED="%{$fg_bold[red]%}●%{$reset_color%}" # red circle - untracked files -GIT_PROMPT_MODIFIED="%{$fg_bold[yellow]%}●%{$reset_color%}" # yellow circle - tracked files modified -GIT_PROMPT_STAGED="%{$fg_bold[green]%}●%{$reset_color%}" # green circle - staged changes present = ready for "git push" - -parse_git_branch() { -# Show Git branch/tag, or name-rev if on detached head -( git symbolic-ref -q HEAD || git name-rev --name-only --no-undefined --always HEAD ) 2> /dev/null -} - -parse_git_state() { - # Show different symbols as appropriate for various Git repository states - # Compose this value via multiple conditional appends. - local GIT_STATE="" - local NUM_AHEAD="$(git log --oneline @{u}.. 2> /dev/null | wc -l | tr -d ' ')" - if [ "$NUM_AHEAD" -gt 0 ]; then - GIT_STATE=$GIT_STATE${GIT_PROMPT_AHEAD//NUM/$NUM_AHEAD} - fi - local NUM_BEHIND="$(git log --oneline ..@{u} 2> /dev/null | wc -l | tr -d ' ')" - if [ "$NUM_BEHIND" -gt 0 ]; then - GIT_STATE=$GIT_STATE${GIT_PROMPT_BEHIND//NUM/$NUM_BEHIND} - fi - local GIT_DIR="$(git rev-parse --git-dir 2> /dev/null)" - if [ -n $GIT_DIR ] && test -r $GIT_DIR/MERGE_HEAD; then - GIT_STATE=$GIT_STATE$GIT_PROMPT_MERGING - fi - if [[ -n $(git ls-files --other --exclude-standard 2> /dev/null) ]]; then - GIT_STATE=$GIT_STATE$GIT_PROMPT_UNTRACKED - fi - if ! git diff --quiet 2> /dev/null; then - GIT_STATE=$GIT_STATE$GIT_PROMPT_MODIFIED - fi - if ! git diff --cached --quiet 2> /dev/null; then - GIT_STATE=$GIT_STATE$GIT_PROMPT_STAGED - fi - if [[ -n $GIT_STATE ]]; then - echo "$GIT_PROMPT_PREFIX$GIT_STATE$GIT_PROMPT_SUFFIX" - fi -} - -git_prompt_string() { - local git_where="$(parse_git_branch)" - - # If inside a Git repository, print its branch and state - [ -n "$git_where" ] && echo "$GIT_PROMPT_SYMBOL$(parse_git_state)$GIT_PROMPT_PREFIX%{$fg[yellow]%}${git_where#(refs/heads/|tags/)}$GIT_PROMPT_SUFFIX" - - # If not inside the Git repo, print exit codes of last command (only if it failed) - [ ! -n "$git_where" ] && echo "%{$fg[red]%} %(?..[%?])" -} - -# Right prompt with exit status of previous command if not successful - #RPROMPT="%{$fg[red]%} %(?..[%?])" -# Right prompt with exit status of previous command marked with ✓ or ✗ - RPROMPT="%(?.%{$fg[green]%}✓ %{$reset_color%}.%{$fg[red]%}✗ %{$reset_color%})" - -case $(basename "$(cat "/proc/$PPID/comm")") in - login) - RPROMPT="%{$fg[red]%} %(?..[%?])" - ;; - urxvt) - RPROMPT='$(git_prompt_string)' - # Use autosuggestion - if [ -f /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ]; then - source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh - ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20 - ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8' - fi - ;; - konsole|qterminal) - RPROMPT='$(git_prompt_string)' - ;; - *) - if $(ps -p$PPID| grep -q -e konsole -e qterminal); then - RPROMPT='$(git_prompt_string)' - else - RPROMPT='$(git_prompt_string)' - ## Base16 Shell color themes. - #possible themes: 3024, apathy, ashes, atelierdune, atelierforest, atelierhearth, - #atelierseaside, bespin, brewer, chalk, codeschool, colors, default, eighties, - #embers, flat, google, grayscale, greenscreen, harmonic16, isotope, londontube, - #marrakesh, mocha, monokai, ocean, paraiso, pop (dark only), railscasts, shapesifter, - #solarized, summerfruit, tomorrow, twilight - theme="monokai" - #Possible variants: dark and light - shade="dark" - BASE16_SHELL="/usr/share/zsh/scripts/base16-shell/base16-$theme.$shade.sh" - [[ -s $BASE16_SHELL ]] && source $BASE16_SHELL - # Use autosuggestion - if [ -f /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh ]; then - source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh - ZSH_AUTOSUGGEST_BUFFER_MAX_SIZE=20 - ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=8' - fi - fi - ;; -esac - # Color man pages export LESS_TERMCAP_mb=$'\E[01;32m' export LESS_TERMCAP_md=$'\E[01;32m'