首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用“vcs_info”根据状态对git分支名称进行着色

使用“vcs_info”根据状态对git分支名称进行着色
EN

Stack Overflow用户
提问于 2016-01-04 23:57:54
回答 1查看 3K关注 0票数 3

我是zsh的新手(我是一个很长时间的bash用户),我有一个自定义提示符(由不同的来源混合而成),并为git提供了一个特定的部分。

我想要:-绿色的(master)如果git status说‘干净’,用红色的(master)如果git status说‘不干净’

我不知道该怎么做,如果有人知道我会非常感激的。

我的.zshrc

代码语言:javascript
复制
# (...)
# Easy colors in ZSH scripting
autoload -U colors && colors

setopt prompt_subst
autoload -Uz vcs_info

function +vi-git-untracked() {
  if [[ $(git rev-parse --is-inside-work-tree 2> /dev/null) == 'true' ]] && \
  [[ $(git ls-files --other --directory --exclude-standard | sed q | wc -l | tr -d ' ') == 1 ]] ; then
  hook_com[unstaged]+='%F{red}??%f'
fi
}

# Show remote ref name and number of commits ahead-of or behind
function +vi-git-st() {
  local ahead behind remote
  local -a gitstatus

  # Are we on a remote-tracking branch?
  remote=${$(git rev-parse --verify ${hook_com[branch]}@{upstream} \
    --symbolic-full-name 2>/dev/null)/refs\/remotes\/}

  if [[ -n ${remote} ]] ; then
    ahead=$(git rev-list ${hook_com[branch]}@{upstream}..HEAD 2>/dev/null | wc -l)
    (( $ahead )) && gitstatus+=( "${c3}+${ahead}${c2}" )

    behind=$(git rev-list HEAD..${hook_com[branch]}@{upstream} 2>/dev/null | wc -l)
    (( $behind )) && gitstatus+=( "${c4}-${behind}${c2}" )

    hook_com[branch]="${hook_com[branch]}${(j:/:)gitstatus}"
  fi
}

# Show count of stashed changes
function +vi-git-stash() {
  local -a stashes

  if [[ -s ${hook_com[base]}/.git/refs/stash ]] ; then
    stashes=$(git stash list 2>/dev/null | wc -l)
    hook_com[misc]+="%f (%F{1}STASH=${stashes}%f)"
  fi
}

zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:git*:*' get-revision true
zstyle ':vcs_info:git*:*' check-for-changes true

#zstyle ':vcs_info:*' stagedstr '%F{3}A%f' 
zstyle ':vcs_info:*' stagedstr '%F{3}A%f' 
zstyle ':vcs_info:*' unstagedstr 'M' 
zstyle ':vcs_info:*' actionformats '%f(%F{2}%b%F{3}|%F{1}%a%f)  '
# format the git part
zstyle ':vcs_info:*' formats '%f(%b) %F{2}%c%F{3}%u%m%f'
zstyle ':vcs_info:git*+set-message:*' hooks git-untracked git-stash git-st
zstyle ':vcs_info:*' enable git 
#zstyle ':vcs_info:*+*:*' debug true

# same as PROMPT_COMMAND in bash
precmd () { vcs_info }
# improve by putting branch name is red if branch is not clean
# conditional on exit code: %(?..%F) on affiche le code de retour uniquement si il est > 0
RPROMPT='%(?..[%F{red}ERROR%F{white}:%F{red}%?%f])'
PROMPT='%F{green}%n%F{orange}@%F{yellow}%m:%F{7}%3~%f ${vcs_info_msg_0_} %f%# '
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-20 07:17:18

我的答案将基于我的个人偏好和zsh-user中关于如何使用vcs_info的示例目录。

我建议您采用在第三集中解释的方法,您可以在vcs_info-examples文件中找到这里

{#与往常一样,首先运行系统,以便正确设置所有内容。vcs_info #,然后设置PS1、RPS1和你想要的任何东西。这个$PS1 #(和上面的其他例子一样)只是一个非常#基本的单行提示符的例子。请参阅"man“,以获得关于如何降低此文件可读性的详细信息。-)如果[ -n ${vcs_info_msg__} ];那么#噢,嘿,没有从vcs_info得到,所以我们有更多的空间。#让我们打印一个更长的部分的$PWD.PS1="%5~%#“space # vcs_info找到了一些需要空间的东西。因此,更短的$PWD #是有意义的。PS1="%3~${vcs_info_msg__}%#“fi }

接下来(以及您提供的代码片段),我会这样做:

代码语言:javascript
复制
precmd() {
    vcs_info
    if [[ -n ${vcs_info_msg_0_} ]]; then
        # vcs_info found something (the documentation got that backwards
        # STATUS line taken from https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/git.zsh
        STATUS=$(command git status --porcelain 2> /dev/null | tail -n1)
        if [[ -n $STATUS ]]; then
            PROMPT='%F{green}%n%F{orange}@%F{yellow}%m:%F{7}%3~%f %F{red}${vcs_info_msg_0_} %f%# '
        else
            PROMPT='%F{green}%n%F{orange}@%F{yellow}%m:%F{7}%3~%f %F{green}${vcs_info_msg_0_} %f%# '
        fi
    else
        # nothing from vcs_info
        PROMPT='%F{green}%n%F{orange}@%F{yellow}%m:%F{7}%3~%f %# '
    fi
}

几个注意事项:

  • 第一个if在示例文件中出错
  • STATUS定义取自oh-my-zsh
  • 如果zstyle是脏的,则可以更改STATUS
  • 您可以使用git status部件的标志(如--untracked-files[=<mode>] --ignore-submodules[=<when>] for git-状态 )来提高git-状态部件的性能。

我希望这能帮上忙

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34602033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档