获得一个动态的bash提示符PS1权 [英] Getting a dynamic bash prompt PS1 right

查看:152
本文介绍了获得一个动态的bash提示符PS1权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,我想报其中一个配置文件的版本在本地文件系统启用PS1动态bash提示符。这是我想要做的,简化的一个人为的例子。那些出错的东西:坏的包装和/或逃避出现支架。任何人都可以发现我在做什么错了?

I'm working on a dynamic bash prompt where I want reported in PS1 which version of a config file is enabled on the local filesystem. This is a contrived example of what I'm trying to do, simplified. The things that are going wrong: bad wrapping and/or escape brackets appearing. Can anyone spot what I'm doing wrong?

如果做作配置V2匹配,我想看看在PS1该版本为黄色。如果是V1,在提示为绿色。设置:

If the contrived config matches "v2", I want to see that version in PS1 as YELLOW. If it's "v1", in the prompt as GREEN. The setup:

$ grep FOOVER foo-*.conf
foo-v1.conf:# FOOVER xyz-v1
foo-v2.conf:# FOOVER zet-v2

我倒是然后符号链接foo.conf富-v1.conf。我的.bashrc:

I'd then symlink foo.conf foo-v1.conf. My bashrc:

 0 GREEN=$(tput setaf 034)
 1 YELLOW=$(tput setaf 3)
 2 BLUE=$(tput setaf 4)
 3 CYAN=$(tput setaf 6)
 4 BOLD=$(tput bold)
 5 RESET=$(tput sgr0)
 6 CONF=$HOME/foo.conf
 7
 8 function __get_foo_version () {
 9   FOOVER=$(grep FOOVER $CONF | awk '{print $3}')
10   if [[ "$FOOVER" =~ v2$ ]]; then
11     style_foover="${BOLD}${YELLOW}$FOOVER"
12     #style_foover="\[${BOLD}${YELLOW}\]$FOOVER"
13   elif [[ "$FOOVER" =~ v1$ ]]; then
14     style_foover="${BOLD}${GREEN}$FOOVER"
15     #style_foover="\[${BOLD}${GREEN}\]$FOOVER"
16   fi
17   echo $style_foover
18 }
19
20 style_host="\[${RESET}${BLUE}\]"
21 style_path="\[${RESET}${CYAN}\]"
22 style_reset="\[${RESET}\]"
23
24 PS1='user@'
25 PS1+="${style_host}host"
26 PS1+="${style_reset} "
27 PS1+="\$(__get_foo_version) "
28 PS1+="${style_reset}"
29 PS1+="${style_path}\W"
30 PS1+="${style_reset} $ "

当我运行上面,我得到这个行为,这看起来不错,在第一次: http://imgur.com/HIR3SoA

When I run the above, I get this behavior, which looks good at first: http://imgur.com/HIR3SoA

但是,当我向上箭头为长命令,坏的包装发生了: http://imgur.com/qLfdUor

But when I up-arrow to a long command, the bad wrapping happens: http://imgur.com/qLfdUor

当我禁用线11,14,使线12,15,我得到的是为了处理非打印字符显示PS1了括号:

When I disable lines 11, 14 and enable lines 12, 15, I get the brackets that are intended to handle non-printing chars showing up in PS1:

(没有足够的信誉分后超过2链接:( imgur.com斜线nkXFDyJ)

(not enough reputation points to post more than 2 links :( imgur.com slash nkXFDyJ)

user@host \[\]xyz-v1 ~ $

..和我仍然得到不好的包装在这种情况下。

.. and I still get the bad wrapping in this case.

推荐答案

使用\\ X01(或\\ 001)和\\ X02(或\\ 002),然后用评估他们的回声-e:

Use \x01 (or \001) and \x02 (or \002) and then evaluate them with echo -e:

    function __get_foo_version () {
      FOOVER=$(grep FOOVER $CONF | awk '{print $3}')
      if [[ "$FOOVER" =~ v2$ ]]; then
        #style_foover="${BOLD}${YELLOW}$FOOVER"
>>>     style_foover="\x01${BOLD}${YELLOW}\x02$FOOVER"
      elif [[ "$FOOVER" =~ v1$ ]]; then
        #style_foover="${BOLD}${GREEN}$FOOVER"
>>>     style_foover="\x01${BOLD}${GREEN}\x02$FOOVER"
      fi
>>>   echo -e "$style_foover"
    }

一个更完整的答案,为什么这修复它是在这里:
<一href=\"http://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt\">http://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt

A more complete answer as to why this fixes it is here: http://superuser.com/questions/301353/escape-non-printing-characters-in-a-function-for-a-bash-prompt

这篇关于获得一个动态的bash提示符PS1权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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