首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对编辑行应用程序的提示符着色

如何对编辑行应用程序的提示符着色
EN

Stack Overflow用户
提问于 2014-01-20 17:16:40
回答 2查看 736关注 0票数 6

我试图给一个由李贝迪驱动的应用程序的提示符着色,但是我的颜色根本没有显示出来。知道我在这里做错什么了吗?

代码语言:javascript
复制
#include <iostream>
#include <histedit.h>

char* prompt(EditLine *e)
{
  static char p[] = "\1\033[36m\1:::\1\033[0m\1 ";
  return p;
}

int main(int argc, char* argv[])
{
  EditLine* el = el_init(argv[0], stdin, stdout, stderr);
  el_set(el, EL_PROMPT_ESC, &prompt, '\1');
  el_set(el, EL_EDITOR, "vi");

  while (1)
  {
    int count;
    char const* line = el_gets(el, &count);

    if (count > 0)
      std::cout << line;
  }

  el_end(el);

  return 0;
}

代码语言:javascript
复制
clang++ editline.cc -ledit && ./a.out

不幸的是,只显示了以下未着色的提示符:

代码语言:javascript
复制
:::     
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-20 17:57:07

\1用作停止/启动文字字符,因此这似乎是正确的行为。

代码语言:javascript
复制
\1\033[36m\1:::\1\033[0m\1
|          |   |         |
|          |   |_Start   |_Stop
|          |
|_Start    |_Stop

EL_PROMPT_ESC,char *(*f)(EditLine *),char c与EL_PROMPT相同,但c参数指示开始/停止文字提示字符。

代码语言:javascript
复制
     If a start/stop literal character is found in the prompt, the
     character itself is not printed, but characters after it are
     printed directly to the terminal without affecting the state
     of the current line.  A subsequent second start/stop literal
     character ends this behavior.  This is typically used to
     embed literal escape sequences that change the color/style of
     the terminal in the prompt.  0 unsets it.

手册页声明使用0取消颜色,但不太清楚它们的含义。

也许可以试试这样的逃逸顺序:

代码语言:javascript
复制
\1\033[36m:::\033[0m\1

因为\1可能会终止颜色的使用,而\[ ... \]则是bash中的正常终止符。

票数 2
EN

Stack Overflow用户

发布于 2015-02-21 16:09:52

'esc[0m‘重置所有属性,因此显示的颜色将立即消失,最好将属性设置为不同的颜色,例如白色的'esc[47m’

有关更全面的属性列表,请参见http://www.termsys.demon.co.uk/vtansi.htm

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21240181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档