From f0b48143ee05d40cd2e7a8d43dc97b59e6a81a39 Mon Sep 17 00:00:00 2001 From: meliurwen Date: Thu, 5 May 2022 14:19:56 +0200 Subject: [PATCH] Completely refactored and optimized the `prompt_git()` of zsh's `powermeli` theme --- zsh/.config/zsh/plugins/powermeli.zsh-theme | 49 ++++++++++----------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/zsh/.config/zsh/plugins/powermeli.zsh-theme b/zsh/.config/zsh/plugins/powermeli.zsh-theme index 902cc71..c4530a5 100755 --- a/zsh/.config/zsh/plugins/powermeli.zsh-theme +++ b/zsh/.config/zsh/plugins/powermeli.zsh-theme @@ -41,46 +41,45 @@ prompt_context() { fi } +# TODO: remove `grep` commands prompt_git() { - if [ "$ZSH_THEME_GIT_MODE" = "disabled" ]; then + if ! command -pv git > /dev/null 2>&1; then return fi - if ! type git > /dev/null 2>&1; then - return - fi - PL_BRANCH_CHAR="" # $'\ue0a0' - if [ "$(git rev-parse --is-inside-work-tree 2> /dev/null)" = "true" ]; then - repo_path=$(git rev-parse --git-dir 2> /dev/null) + repo_path="$(git rev-parse --git-dir 2> /dev/null)" + if [ -n "$repo_path" ]; then ref=$(git symbolic-ref HEAD 2> /dev/null) || ref="➦ $(git rev-parse --short HEAD 2> /dev/null)" - if [ -n "$(git status --porcelain --ignore-submodules=dirty 2> /dev/null | tail -n 1)" ]; then + git_status="$(git status --porcelain --ignore-submodules=dirty 2> /dev/null)" + git_str="" + if [ -n "$git_status" ]; then prompt_segment "$SEGMENT_SEPARATOR" yellow black + if [ -n "$(printf "%s" "$git_status" | grep -e "^[MADRCU? ][MADRCU?]")" ]; then + git_str="$git_str±" # Unstaged + fi + if [ -n "$(printf "%s" "$git_status" | grep -e "^[MADRCU]")" ]; then + git_str="$git_str✚" # Staged + fi else prompt_segment "$SEGMENT_SEPARATOR" green black fi if [ -e "${repo_path}/BISECT_LOG" ]; then - mode=" " + git_str="$git_str " elif [ -e "${repo_path}/MERGE_HEAD" ]; then - mode=" >M<" + git_str="$git_str >M<" elif [ -e "${repo_path}/rebase" ] || [ -e "${repo_path}/rebase-apply" ] || [ -e "${repo_path}/rebase-merge" ] || [ -e "${repo_path}/../.dotest" ]; then - mode=" >R>" + git_str="$git_str >R>" fi - autoload -Uz vcs_info - - zstyle ':vcs_info:*' enable git - zstyle ':vcs_info:*' get-revision true - zstyle ':vcs_info:*' check-for-changes true - zstyle ':vcs_info:*' stagedstr '✚' - zstyle ':vcs_info:*' unstagedstr '±' - zstyle ':vcs_info:*' formats ' %u%c' - zstyle ':vcs_info:*' actionformats ' %u%c' - vcs_info - PROMPT_LINE="$PROMPT_LINE${${ref:gs/%/%%}/refs\/heads\// $PL_BRANCH_CHAR }${vcs_info_msg_0_%% }${mode} " - unset repo_path ref mode + [ -n "$git_str" ] && git_str="$git_str " + + branch_char="" # $'\ue0a0' + PROMPT_LINE="$PROMPT_LINE${${ref:gs/%/%%}/refs\/heads\// $branch_char } ${git_str}" + + unset ref git_status git_str branch_char fi - unset PL_BRANCH_CHAR + unset repo_path } prompt_virtualenv() { @@ -106,7 +105,7 @@ build_prompt() { prompt_virtualenv prompt_context prompt_segment "$SEGMENT_SEPARATOR" blue black " %~ " - prompt_git + [ "$ZSH_THEME_GIT_MODE" = "disabled" ] || prompt_git PROMPT_LINE="$PROMPT_LINE%{%k%F{${OLD_BG:-blue}}%}$SEGMENT_SEPARATOR%{%f%}" unset OLD_BG OLD_SEP printf "%s%s " "%{%f%b%k%}" "$PROMPT_LINE"