commit 19b3cfc47e6d235c82a7c288c26f0adcea24347e
parent 7837e676f40d444e5e6d218158c8373bf3042c85
Author: Oscar Benedito <oscar@oscarbenedito.com>
Date:   Tue, 24 Nov 2020 18:49:01 +0100

Add git branch to prompt if not on default branch

Diffstat:
M.config/zsh/.zshrc | 3++-
A.local/bin/git-head-abbrev | 10++++++++++
2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc @@ -1,7 +1,8 @@ # vim: filetype=zsh # Load colors and set up prompt autoload -U colors && colors -PS1="%B%{$fg[magenta]%}%n%{$fg[blue]%}@%{$fg[green]%}%M %{$fg[yellow]%}%~%b%{$fg[red]%}$%{$reset_color%} " +setopt PROMPT_SUBST +PS1='%B%{$fg[magenta]%}%n%{$fg[blue]%}@%{$fg[green]%}%M %{$fg[yellow]%}%~%b%{$fg[cyan]%}$(git-head-abbrev)%{$fg[red]%}$%{$reset_color%} ' # Set up history and history file HISTSIZE=1000 diff --git a/.local/bin/git-head-abbrev b/.local/bin/git-head-abbrev @@ -0,0 +1,10 @@ +#!/usr/bin/env sh + +c="$(git rev-parse --abbrev-ref HEAD 2>/dev/null)" +if [ "$c" = "HEAD" ]; then + c="$(git describe --tags 2>/dev/null)" + [ -z "$c" ] && c="$(git rev-parse --short HEAD 2>/dev/null)" +fi +[ "$c" = "master" ] && unset c +[ "$c" = "main" ] && unset c +[ -n "$c" ] && printf " ($c)"