破bash提示符包装线 [英] broken bash prompt wrap line

查看:180
本文介绍了破bash提示符包装线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定制在OSX我的bash提示符包括混帐分支加上分支状态的一些痕迹。这打破换行。

我知道我必须添加\\ [和\\]至prevent这个问题,但在功能,这样做显示\\ [和\\] litteraly。

我能做些什么来逃避这些功能,例如序列?

免责声明:这是我的第一次尝试在bash脚本

 功能parse_git_dirty {
  #TODO使git的状态响应变量
  #[支部+]:工作目录上演的变化
  如果[[$(git的状态2 - ;的/ dev / null的| grep的被提交)]
  那么S = $ S$(tput的setaf 2)+ $(tput的sgr0)
  科幻
  #[支部+]:工作目录不分级有变化
  如果[[$(git的状态2 - ;的/ dev / null的| grep的未上演提交)]
  那么S = $ S$(tput的setaf 1)+ $(tput的sgr0)
  科幻
  #[支部+]:工作目录有未跟踪文件
  如果[[$(git的状态2 - ;的/ dev / null的| grep的跟踪的文件)]
  那么S = $ S$(tput的setaf 1)+ $(tput的sgr0)
  科幻
  #[分公司与LT]:当地分公司是起源背后
  如果[[$(git的状态2 - ;的/ dev / null的| grep的你的分支是幕后黑手)]
  那么S = $ S$(tput的setaf 5)LT; $(tput的sgr0)
  科幻
  #[分支]的计算值:当地分支机构领先的起源
  如果[[$(git的状态2 - ;的/ dev / null的| grep的分支是超前)]
  那么S = $ S$(tput的setaf 5)> $(tput的sgr0)
  科幻
  #[部门和LT;>]:分支机构有分歧
  如果[[$(git的状态2 - ;的/ dev / null的| grep的分道扬镳)]
  那么S = $ S$(tput的setaf 5)LT;> $(tput的sgr0)
  科幻
  回声$ S
}
功能parse_git_branch {
  混帐分支--no色2 - ;的/ dev / null的| SED -e'/ ^ [^ *] / D'-e'S / * \\(。* \\)/ \\ 1 /'
}
功能show_git_branch {
  如果[[$(parse_git_branch)]
  然后回声$(tput的setaf 2)($(tput的sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput的setaf 2))$(tput的sgr0)
  科幻
}
出口PS1 =\\ U \\ [$(tput的setaf 2)\\] @ \\ [$(tput的sgr0)\\] \\ ^ h \\ [$(tput的setaf 2)\\]:\\ [$(tput的sgr0)\\] \\ W \\ [\\ $(show_git_branch)\\]


解决方案

我很高兴听到你已经解决了您的版本的问题,但我想这可能是值得指出的是混帐已经与一个有用的分布经过深思熟虑的名为 __ git_ps1 ,您可以在您的 PS1 bash函数。例如,你可以使用它是这样的:

 出口PS1 ='等等等等等等$(__ git_ps1(%S))'

如果你不是一个Git仓库,在 $(__ git_ps1(%S))会变成空字符串。如果是,然而,那么格式字符串将被使用。这通常会显示您当前分支,但如果你将要代替所示的合并或重订的中间是。

在默认情况下 __ git_ps1 将不会显示你的树是否较脏或有未跟踪的文件,因为在特定的仓库,这可能使你的bash提示不快慢出现。但是,如果你想看到这个信息,以及,它会告诉他们,如果你设置 GIT_PS1_SHOWDIRTYSTATE GIT_PS1_SHOWUNTRACKEDFILES 来一些非空。

您可以找到在更多信息git-completion.sh源文件

I'm customizing my bash prompt on OsX to include git branch plus some marks of the branch state. This breaks line wrap.

I know that I have to add \[ and \] to prevent this issue, but doing so in the functions does display \[ and \] litteraly.

What can I do to escape such sequences in those functions?

Disclaimer: those are my first attempts in bash scripting.

function parse_git_dirty {
  # TODO make git status response a variable
  # [branch+] : working dir has staged changes
  if [[ $(git status 2> /dev/null | grep "to be committed") ]]
  then S=$S"$(tput setaf 2)+$(tput sgr0)"
  fi
  # [branch+] : working dir has unstaged changes
  if [[ $(git status 2> /dev/null | grep "not staged for commit") ]]
  then S=$S"$(tput setaf 1)+$(tput sgr0)"
  fi
  # [branch+] : working dir has untracked files
  if [[ $(git status 2> /dev/null | grep "tracked files") ]]
  then S=$S"$(tput setaf 1)+$(tput sgr0)"
  fi
  # [branch<] : local branch is behind origin
  if [[ $(git status 2> /dev/null | grep "Your branch is behind") ]]
  then S=$S"$(tput setaf 5)<$(tput sgr0)"
  fi
  # [branch>] : local branch is ahead origin
  if [[ $(git status 2> /dev/null | grep "branch is ahead of") ]]
  then S=$S"$(tput setaf 5)>$(tput sgr0)"
  fi
  # [branch<>] : branches have diverged
  if [[ $(git status 2> /dev/null | grep "have diverged") ]]
  then S=$S"$(tput setaf 5)<>$(tput sgr0)"
  fi
  echo $S
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function show_git_branch {
  if [[ $(parse_git_branch) ]]
  then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)"
  fi
}
export PS1="\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[\$(show_git_branch)\] "

解决方案

I glad to hear that you've solved the problem with your version, but I thought it might be worth pointing out that git is already distributed with a helpful and carefully thought out bash function called __git_ps1 that you can include in your PS1. For example, you could use it like this:

 export PS1='blah blah blah$(__git_ps1 " (%s)") '

If you're not in a git repository, the $(__git_ps1 " (%s)") will turn into the empty string. If you are, however, then the format string will be used. That will usually show you your current branch, but if you're in the middle of a merge or a rebase that will be shown instead.

By default __git_ps1 won't show you whether the tree is dirty or there are untracked files, since in certain repositories this could make it irritatingly slow for your bash prompt to appear. However, if you want to see this information as well, it'll show them if you set GIT_PS1_SHOWDIRTYSTATE or GIT_PS1_SHOWUNTRACKEDFILES to something non-empty.

You can find more information at the top of the git-completion.sh source file.

这篇关于破bash提示符包装线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆