【问题标题】:Windows Git bash prompt not displaying colours from a functionWindows Git bash 提示不显示函数的颜色
【发布时间】:2023-03-19 14:01:01
【问题描述】:

我在我的 .bash_prompt 中使用以下颜色代码/功能(来自 Paul Irish),以在 Windows 上的 Git bash 中显示分支名称和状态。

MAGENTA="\033[1;31m"
ORANGE="\033[1;33m"
GREEN="\033[1;32m"
PURPLE="\033[1;35m"
WHITE="\033[1;37m"
BOLD=""
RESET="\033[m"

function git_info() {

        # check if we're in a git repo
        git rev-parse --is-inside-work-tree &>/dev/null || return

        # quickest check for what branch we're on
        branch=$(git symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')

        # check if it's dirty (via github.com/sindresorhus/pure)
        dirty=$(git diff --quiet --ignore-submodules HEAD &>/dev/null; [ $? -eq 1 ]&& echo -e "*")

        echo $WHITE" on "$PURPLE$branch$dirty
}

该函数正确显示信息,但不是为这两个部分着色,而是像这样打印出颜色代码:

\[\033[1;37m\] on \[\033[1;35m\]working*
$

这是使用以下(简化以说明问题)PS1线:

PS1="\$(git_info)\n$ "

我应该注意,如果代码直接添加到 PS1 行,颜色可以正常工作,即:

PS1="\[$PURPLE\]\w\n$ \[$RESET\]"

我正在摸索所有这些 bash 自定义,所以可能会做一些明显愚蠢的事情!

【问题讨论】:

    标签: windows bash prompt git-bash


    【解决方案1】:

    也许echo -e。来自man echo

    -e enable interpretation of backslash escapes

    此外,您可以在双引号内使用变量:

    echo "$WHITE on $PURPLE$branch$dirty"
    

    【讨论】:

      猜你喜欢
      • 2015-04-11
      • 2012-05-15
      • 2016-02-03
      • 2011-09-29
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2012-01-19
      • 2015-06-18
      相关资源
      最近更新 更多