如何使 git-prompt.sh 的输出(分支等)变为粗体?

如何使 git-prompt.sh 的输出(分支等)变为粗体?

我使用Git 信息git-prompt.sh自定义提示符,例如分支。bash

https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

我希望git-prompt.sh的输出为粗体,因为我的提示的其他部分是粗体(见PS1下文)。我不想更新git-prompt.sh,而是使其输出在 中加粗~/.bashrc

但是,我无法让它发挥作用。如果我加粗$fmt,那么只有分支变成粗体,而不是状态信息。

有任何想法吗?

~/.bashrc

# ps1 {{{1

PS1='\[\033[01;32m\]\u@\h\[\033[00m\] \[\033[01;34m\]\W\[\033[00m\] \$ '

if declare -F __git_ps1 &>/dev/null; then
    __git_ps1_venv() {
        local pre="$1"
        local post="$2"
        local fmt=" (%s)"

        if [ -n "${VIRTUAL_ENV}" ] && [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ]; then
            if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
                # special case for Aspen magic directories
                # see http://www.zetadev.com/software/aspen/
                pre="[`basename \`dirname \"$VIRTUAL_ENV\"\``] ${pre}"
            else
                pre="(`basename \"$VIRTUAL_ENV\"`) ${pre}"
            fi
        fi

        __git_ps1 "${pre}" "${post}" "${fmt}"
    }

    GIT_PS1_SHOWDIRTYSTATE=1
    GIT_PS1_SHOWSTASHSTATE=1
    GIT_PS1_SHOWUNTRACKEDFILES=1
    GIT_PS1_SHOWUPSTREAM="name"
    GIT_PS1_STATESEPARATOR=" "
    GIT_PS1_SHOWCOLORHINTS=1
    GIT_PS1_HIDE_IF_PWD_IGNORED=1

    PROMPT_COMMAND='__git_ps1_venv "'"${PS1% \\\$ }"'" " \\\$ "'
fi

答案1

git-prompt.sh

# __git_ps1 requires 2 or 3 arguments when called from PROMPT_COMMAND (pc)
# in that case it _sets_ PS1. The arguments are parts of a PS1 string.
# when two arguments are given, the first is prepended and the second appended
# to the state string when assigned to PS1.
# The optional third parameter will be used as printf format string to further
# customize the output of the git-status string.

在执行之前附加\e[1m(SGR Bold Sequence)应该适合您的情况。在您的情况下,您可以这样做,因为初始化为 的第一个参数,您可以像这样放置并使用来重置它。${pre}__git_ps1${pre}__git_ps1_venv\e[1m\e[0m

PROMPT_COMMAND='__git_ps1_venv "'"${PS1% \\\$ }\e[1m"'" "\e[0m \\\$ "'

相关内容