社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

从bash将parse git_分支函数转换为zsh(提示)

Sam Coles • 6 年前 • 1588 次点击  

我在bash中使用这个函数

function parse_git_branch {
  git_status="$(git status 2> /dev/null)"
  pattern="^# On branch ([^${IFS}]*)"
  if [[ ! ${git_status}} =~ "working directory clean" ]]; then
    state="*"
  fi
  # add an else if or two here if you want to get more specific

  if [[ ${git_status} =~ ${pattern} ]]; then
    branch=${BASH_REMATCH[1]}
    echo "(${branch}${state})"
  fi
}

但我决定使用zsh。虽然我可以在.zshrc中将它完美地用作shell脚本(即使没有shebang),但该行中的错误是解析错误。 if [[ ! ${git_status}}

我需要做什么来为zshell做好准备?

编辑: 我得到的“实际错误”是 " parse error near } 它指的是带有奇怪双曲线的线 }} 在bash上工作。

编辑: 这是最后的代码,只是为了好玩:

parse_git_branch() {
    git_status="$(git status 2> /dev/null)"
pattern="^# On branch ([^[:space:]]*)"
    if [[ ! ${git_status} =~ "working directory clean" ]]; then
        state="*"
    fi
    if [[ ${git_status} =~ ${pattern} ]]; then
      branch=${match[1]}
      echo "(${branch}${state})"
    fi
}

setopt PROMPT_SUBST
PROMPT='$PR_GREEN%n@$PR_GREEN%m%u$PR_NO_COLOR:$PR_BLUE%2c$PR_NO_COLOR%(!.#.$)'
RPROMPT='$PR_GREEN$(parse_git_branch)$PR_NO_COLOR'

感谢大家的耐心和帮助。

编辑: 最好的答案让我们都明白了: git status 是瓷器(UI)。好的脚本不利于Git管道。最后一个功能是:

# The latest version of Chris' function below

PROMPT='$PR_GREEN%n@$PR_GREEN%m%u$PR_NO_COLOR:$PR_BLUE%2c$PR_NO_COLOR%(!.#.$)'
RPROMPT='$PR_GREEN$(parse_git_branch)$PR_NO_COLOR'

注意,只有提示是zsh特定的。在bash中,这将是您的提示加上 "\$(parse_git_branch)" .

这可能会慢一点(更多地调用Git,但这是一个经验问题),但它不会被Git的变化破坏(它们不会改变管道)。这对于一个好的剧本来说是非常重要的。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/31492
 
1588 次点击  
文章 [ 7 ]  |  最新文章 6 年前