Friday, September 16, 2022

My Minimalist ZSH Prompt

I have experimented with using many ZSH prompts. On some slow systems, the latency of rendering the prompt matters. I ended up going with the following solution which is by far the fastest prompt I have come across.

function git_prompt_info() {
    ref=$(git-branch-name -q -h 12 -b 64) || return
    echo " ${ref}"
}

function git_prompt() {
    #BRANCH=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/*\(.*\)/\1/')
    BRANCH="$(git_prompt_info)"
    if [ ! -z $BRANCH ]; then
        echo -n "%F{yellow}$BRANCH"
        if [ ! -z "$(git status --short)" ]; then
            echo " %F{red}?"
        fi
    fi
}

setopt PROMPT_SUBST
PROMPT=$'%F{blue}%~$(git_prompt)\n%F{244}%# %F{reset}'

I use git-branch-name (https://github.com/itchyny/git-branch-name) to improve the speed of retrieving the branch name. If you won't wish to use git-branch-name, toggle the comment of the two BRANCH lines.


No comments: