gpt4 book ai didi

git - 使用 'vcs_info' 根据状态着色 git 分支名称

转载 作者:行者123 更新时间:2023-12-04 14:30:53 25 4
gpt4 key购买 nike

我是 的新手(我是很长一段时间的 用户)我有一个自定义提示(由不同的来源混合而成),其中有一个特定的 git 部分。: enter image description here

我想要: - 如果 git status 显示“clean”,则 (master) 为绿色 - 如果 git status 说“不干净”

(master) 为红色

我不知道我该怎么做,如果有人知道,我将不胜感激。

我的.zshrc:

# (...)
# 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%# '

最佳答案

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

我建议您采用 vcs_info-examples第三集 中解释的方法你可以找到文件 here .

precmd() {
# As always first run the system so everything is setup correctly.
vcs_info
# And then just set PS1, RPS1 and whatever you want to. This $PS1
# is (as with the other examples above too) just an example of a very
# basic single-line prompt. See "man zshmisc" for details on how to
# make this less readable. :-)
if [[ -n ${vcs_info_msg_0_} ]]; then
# Oh hey, nothing from vcs_info, so we got more space.
# Let's print a longer part of $PWD...
PS1="%5~%# "
else
# vcs_info found something, that needs space. So a shorter $PWD
# makes sense.
PS1="%3~${vcs_info_msg_0_}%# "
fi
}

从那开始(以及您提供的代码片段)我会这样做:

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>] 这样的标志来部分对于 git-status

希望对你有帮助

关于git - 使用 'vcs_info' 根据状态着色 git 分支名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34602033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com