6

我正在尝试为由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

不幸的是,只显示了以下无色提示:

:::     
4

3 回答 3

3

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

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

这种设计允许更简单的代码。例如,编辑器中的插入命令可以并且实际上确实会重绘整个屏幕,但最终的终端绘图命令序列是最少的。

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

于 2016-03-27T20:12:36.763 回答
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 中的正常终止符。

于 2014-01-20T17:57:07.333 回答
1

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

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

于 2015-02-21T16:09:52.317 回答