如何为编辑线应用程序的提示着色

How to colorize the prompt of an editline application

本文关键字:提示 应用程序 编辑      更新时间:2023-10-16

我正在尝试使libedit供电的应用程序提示着色,但是我根本不会出现颜色。有什么想法我在这里做错了什么?

#include <iostream>
#include <histedit.h>
char* prompt(EditLine *e)
{
  static char p[] = "133[36m1:::133[0m1 ";
  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

不幸的是仅显示:

的未颜色提示
:::     

编辑线不支持颜色提示。有一个补丁实现它们。

有趣的是,在屏幕更新期间,Editline在内存缓冲区中首先呈现图像,并在上一个帧中差异,然后发出命令以修复差异。命令是moveto(x,y)delete(n)insert(text)

此设计允许更简单的代码。例如,在编辑器中插入命令可以并且实际上对整个屏幕进行重新绘制,但是终端绘制命令的结果序列很小。

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

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

133[36m1:::133[0m1
|          |   |         |
|          |   |_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来阐明颜色,但尚不清楚它们的含义。

也许尝试这样的逃生序列:

133[36m:::33[0m1

由于1可能从使用中终止颜色,而[ ... ]将是bash中的普通终结者。

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

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