From 05eb5709bd8127befe8c8b8822e2c77ecb692c75 Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 7 Nov 2021 20:07:00 +0000 Subject: [PATCH] Update the prompt --- .config/zsh/.zshrc | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/.config/zsh/.zshrc b/.config/zsh/.zshrc index 5a1258f..6e08eef 100644 --- a/.config/zsh/.zshrc +++ b/.config/zsh/.zshrc @@ -2,7 +2,41 @@ # Enable colors and set prompt autoload -U colors && colors -PS1="%B%{$fg[red]%}[%{$fg[yellow]%}%n%{$fg[green]%}@%{$fg[blue]%}%M %{$fg[magenta]%}%~%{$fg[red]%}]%{$reset_color%}$%b " +setopt prompt_subst + +function git_prompt_info() { + local STATUS + # Use the dotfiles git repo if in the home directory + if [[ "$PWD" = "$HOME" ]]; then + STATUS=$(git --git-dir "$HOME/.config/dotfiles/.git" --work-tree "$HOME" status --porcelain | tail -1) + else + if ! git rev-parse --git-dir &> /dev/null; then + return 0 + fi + STATUS=$(git status --porcelain | tail -1) + fi + + if [[ -n $STATUS ]]; then + echo "%{$fg[yellow]%} %{$fg[reset_color]%}" + fi +} + +function git_current_branch { + # No need to show the branch for the home directory + local ref + ref=$(git symbolic-ref --quiet HEAD 2> /dev/null) + local ret=$? + if [[ $ret != 0 ]]; then + [[ $ret == 128 ]] && return # no git repo + ref=$(git rev-parse --short HEAD 2> /dev/null) || return + fi + echo ${ref#refs/heads/} +} + +# %B (%b) for bold | %c shows the top directory +PS1='%B%(?:%{$fg[green]%} :%{$fg[red]%} )%{$fg[magenta]%}%c $(git_prompt_info)%b' +RPS1='%{$fg[blue]%}$(git_current_branch)' + setopt autocd # Automatically cd into typed directory. stty stop undef # Disable ctrl-s to freeze terminal. setopt interactive_comments