代码之家  ›  专栏  ›  技术社区  ›  Nxt3

确定PS1中以前使用的颜色

  •  1
  • Nxt3  · 技术社区  · 6 年前

    现在我的PS1看起来像这样 enter image description here

    我希望“–184”背景颜色根据最后一部分的颜色而改变。所以,如果我不在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使用的是subshell,所以这不起作用。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Jack    6 年前

    好吧,你的 __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) "