如何着色editline应用程序的提示

时间:2014-01-20 17:16:41

标签: c++ c colors prompt editline

我正在尝试着色由libedit驱动的应用程序的提示,但我的颜色根本没有出现。我在这里做错了什么想法?

#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;
}

编译

clang++ editline.cc -ledit && ./a.out

并且不幸地显示了以下的无色提示:

:::     

3 个答案:

答案 0 :(得分:3)

Editline不支持颜色提示。有一个patch实现它们。

有趣的是,在屏幕更新期间,编辑行将图像首先渲染到内存缓冲区中,与前一帧进行差异,然后发出命令来修复差异。命令为moveto(x,y)delete(n)insert(text)

此设计允许更简单的代码。例如,编辑器中的insert命令可以实际重绘整个屏幕,但生成的终端绘制命令序列很少。

不幸的是,由于文本在到达终端之前经历了复杂的转换,因此一些信息在翻译时会丢失,就像颜色一样。

答案 1 :(得分:2)

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

\1\033[36m\1:::\1\033[0m\1
|          |   |         |
|          |   |_Start   |_Stop
|          |
|_Start    |_Stop

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

     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取消设置颜色,但有点不清楚它们的含义。

也许尝试这样的转义序列:

\1\033[36m:::\033[0m\1

由于\1可能会终止使用颜色,而\[ ... \]将成为bash中的正常终结符。

答案 2 :(得分:1)

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

http://www.termsys.demon.co.uk/vtansi.htm 获取更全面的属性列表