有没有上色在bash提示文本没有两个电话一个有效的方法? [英] Is there an efficient way to colorize text in a bash prompt without two calls?

查看:104
本文介绍了有没有上色在bash提示文本没有两个电话一个有效的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上色在bash提示文本没有两个电话有效的方法是什么?

Efficient way to colorize text in a bash prompt without two calls?

有大量的资源,各地有关自定义PS1的​​地方。这里的关键点是:

There are plenty of resources around the place about customizing PS1. The critical points here are:


  • 这是可以调用自定义函数生成文本,导致自定义文本

  • 这是可能的这样一个函数发出自定义颜色codeS

  • 非打印文本(如颜色codeS)需要被标记,以使自动换行的工作

  • 这是不可能的自定义函数做标记。

我可能是错的最后一点,如果有办法,这将完全解决这个问题。

I may be wrong on the last point, and if there's a way, that would perfectly solve this.

下面是一个简单的,有点做作的例子是拓扑类似,如果是有道理的,到一个我摆弄。我有一个外部命令(称之为 generate_text ),它产生在标准输出上不是OK或一些字的消息。它也可以在所有发送任何东西。这三个国家,应在提示符下图所示:如果它发出什么,留下的提示,正是因为它是(用户名@主机名:路径$ );如果它发出OK,把一个绿色的OK的提示之前;如果别的,就把该文本为红色。

Here's a simplified and somewhat contrived example that's topologically similar, if that makes sense, to the one I'm tinkering with. I have an external command (call it generate_text) which produces on stdout either "OK" or some one-word message. It may also emit nothing at all. Those three states should be shown in the prompt: if it emits nothing, leave the prompt exactly as it is (user@hostname:path$); if it emits OK, put a green "OK " before the prompt; if anything else, put that text in red.

我目前的解决方案是让调用generate_text一个自定义函数,要么发出它的文本或发出基于它的颜色code,然后调用该函数两次:

My current solution is to have a custom function that invokes generate_text and either emits its text or emits a color code based on it, and then call that function twice:

generate_prompt()
{
    txt=`generate_text`
    [ -z "$txt" ] && exit # The empty case. Produce nothing.
    $1 && echo "##$msg## " || case $msg in
        'OK') echo -e '\e[1;32m'; ;; # Green for OK
        *) echo -e "\e[1;31m"; ;; # Red for anything else
    esac
}
PS1='\[$(generate_prompt false)\]$(generate_prompt true)\[\e[0m\]'$PS1

这意味着我必须调用两次generate_text并假定他们会返回相同的字符串(通常是这样,但它是理论上可能是国家能够在两个调用之间变化)。这令我有点浪费。有没有既发出非印刷色code和来自同一个函数内的一段文字,一个便捷的方式?

This means I have to call generate_text twice and assume that they'll return the same string (which will normally be the case, but it's theoretically possible that state could change between the two invocations). This strikes me as somewhat wasteful. Is there a convenient way to emit both a non-printing color code and a piece of text from within the same function?

推荐答案

您可以使用PROMPT_COMMAND计算所有需要的值,然后在提示符下使用它们:

You can use PROMPT_COMMAND to calculate all required values, and then using them in your prompt:

generate_prompt() {
  color="" message=""
  txt=$(generate_text)
  [[ -z $txt ]] && return

  message="##$txt##"
  [[ $txt == OK ]] && color=$'\e[1;32m' || color=$'\e[1;31m'
}
PROMPT_COMMAND=generate_prompt
PS1='\[$color\]$message\[\e[0m\]'$PS1

注意PROMPT_COMMAND有时已经配置来设置xterm的标题,在这种情况下,你可以追加到它,而不是。

Note that PROMPT_COMMAND is sometimes already configured to set the xterm title, in which case you can append to it instead.

这篇关于有没有上色在bash提示文本没有两个电话一个有效的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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