【问题标题】:Determine previous color used in PS1确定 PS1 中使用的先前颜色
【发布时间】:2018-07-31 13:35:26
【问题描述】:

现在我的 PS1 是这样的

我希望“▸”背景颜色根据最后一部分的颜色而改变。所以,如果我不在 git repo 中,它应该是蓝色的,但是当我在 git repo 中时,它应该是黄色的。

这是我的 PS1 在我的.bash_profile 中的样子

# git info on prompt
function __git_info() {
  local -r SYMBOL_GIT_BRANCH="⑂";
  local -r SYMBOL_GIT_MODIFIED="*";
  local -r SYMBOL_GIT_PUSH="↑";
  local -r SYMBOL_GIT_PULL="↓";

  hash git 2>/dev/null || return 0; # git not found

  # current branch reference
  local ref=$(git symbolic-ref --short HEAD 2>/dev/null);

  # if it's not a normal branch name, get tag name or short unique hash
  [[ -z "$ref" ]] && ref=$(git describe --tags --always 2>/dev/null);
  [[ -n "$ref" ]] || return 0;  #not a git repo

  local following; # ahead/behind count
  local modified; # whether something has been modified locally
  local extras; # additional info
  local status; # status of the repo
  local untracked; # whether or not there are untracked files
  local staged; # whether or not there are staged files

  status=$(git status 2>&1 | tee);
  untracked=$(printf "%s" "$status" 2> /dev/null | grep -m 1 "Untracked files" &> /dev/null; printf "%s" "$?");
  staged=$(printf "%s" "$status" 2> /dev/null | grep -m 1 "Changes to be committed" &> /dev/null; printf "%s" "$?");

  [[ "${untracked}" == "0" ]] && extras+="?";
  [[ "${staged}" == "0" ]] && extras+="+";

  # scan first two lines of output from `git status`
  while IFS= read -r line; do
    if [[ $line =~ ^## ]]; then #header line
      [[ $line =~ ahead\ ([0-9]+) ]] && following+="$SYMBOL_GIT_PUSH${BASH_REMATCH[1]}"
      [[ $line =~ behind\ ([0-9]+) ]] && following+="$SYMBOL_GIT_PULL${BASH_REMATCH[1]}"
    else #branch is modified if output contains more lines after the header
      modified=" $SYMBOL_GIT_MODIFIED";
      break;
    fi;
  done < <(git status --porcelain --branch 2>/dev/null);

  # print the git branch segment without a trailing newline
  printf "%s" " [$SYMBOL_GIT_BRANCH$following $ref$modified$extras] ";
}

## Prompt customizations ##
function __host() {
  printf '\[\e[30;102m\] \h \[\e[0m\]';
}

function __dir() {
  printf '\[\e[1;97;44m\] \w \[\e[0m\]';
}

function __git_status() {
  printf "\[\e[30;43m\]\$(__git_info)\[\e[0m\]";
}

function __arrow() {
  printf '\[\e[1;97;44m\] ▸ \[\e[0m\]';
}

export PS1="$(__host)$(__dir)$(__git_status)$(__arrow) "

有人知道如何实现这一点吗?我尝试设置全局变量,但 PS1 使用的是子外壳,所以这不起作用。

【问题讨论】:

  • 你不能从__git_status() 的末尾删除\[\e[0m\] 和从__arrow() 的开头删除\[\e[1;97;44m\] 吗?

标签: bash prompt


【解决方案1】:

好吧,您的__git_info 函数返回一个状态,那么为什么不使用它呢? (当你是一个 git repo 时,确保它返回非零值。)不要重置函数中的颜色,而是让它们保持原样并在箭头之后重置它们:

function __dir() {
  printf '\[\e[1;97;44m\] \w ';
}

function __git_status() {
  local info=$(__git_info)
  [ $? -ne 0 ] && printf "\[\e[30;43m\]$info";
}

function __arrow() {
  printf ' ▸ \[\e[0m\]';
}

export PS1="$(__host)$(__dir)$(__git_status)$(__arrow) "

【讨论】:

  • return 1; 添加到__git_info 似乎不会返回非零值。
  • 这也会使 __git_status 文本加粗,我不想这样做。
  • \e[21m关闭__dir末尾的粗体。
  • 如何在不转义所有样式的情况下仅禁用粗体?我仍然无法将__gif_info 的退出状态变为0 以外的任何其他值。
  • local 掩盖了命令替换的返回值。你必须做local var; var=$(somecommand)(见github.com/koalaman/shellcheck/wiki/SC2155
猜你喜欢
  • 2014-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2011-08-01
  • 2016-07-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多