简体   繁体   English

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

[英]How to colorize the prompt of an editline application

I am trying to colorize the prompt of an application powered by libedit , but I the color simply does not show up. 我试图着色由libedit驱动的应用程序的提示,但我的颜色根本没有显示。 Any ideas what I'm doing wrong here? 我在这里做错了什么想法?

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

Compiled with 编译

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

and shows unfortunately just the uncolored prompt of: 并且显示不幸的是只有未着色的提示:

:::     

Editline doesn't support colour prompts. Editline不支持颜色提示。 There is a patch implementing them. 有一个补丁实现它们。

Interestingly, during screen update editline renders the image first in a memory buffer, diffs with the previous frame and then emits commands to fix the difference. 有趣的是,在屏幕更新期间,编辑行将图像首先渲染到内存缓冲区中,与前一帧进行差异,然后发出命令来修复差异。 The commands are moveto(x,y) , delete(n) , insert(text) . 命令是moveto(x,y)delete(n)insert(text)

This design allows for a simpler code. 这种设计允许更简单的代码。 For instance, insert command in the editor can and actually does redraw the entire screen but the resulting sequence of terminal draw commands is minimal. 例如,编辑器中的insert命令可以实际重绘整个屏幕,但最终的终端绘制命令序列是最小的。

Unfortunately, since a text undergoes complex transformations before reaching the terminal, some information is lost in translation, like the colour. 不幸的是,由于文本在到达终端之前经历了复杂的变换,因此一些信息在翻译中会丢失,就像颜色一样。

\\1 is used as a stop/start literal character, so that seems to be the correct behavior. \\ 1用作停止/启动文字字符,因此这似乎是正确的行为。

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

EL_PROMPT_ESC, char *(*f)(EditLine *), char c Same as EL_PROMPT, but the c argument indicates the start/stop literal prompt character. 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.

The man page states using 0 to unset the color, but it's a little unclear what they mean. 手册页声明使用0来取消设置颜色,但有点不清楚它们的含义。

Maybe try the escape sequence like this: 也许尝试这样的转义序列:

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

Since the \\1 is possibly terminating the color from being used, whereas \\[ ... \\] would be the normal terminators in bash. 由于\\1可能终止使用的颜色,而\\[ ... \\]将是bash中的正常终止符。

the 'esc[0m' resets ALL the attributes, so a displayed color will immediately disappear, better to set the attribute to a different color For instance white 'esc[47m' 'esc [0m'重置所有属性,因此显示的颜色会立即消失,最好将属性设置为不同的颜色例如white'esc [47m'

see http://www.termsys.demon.co.uk/vtansi.htm for a more comprehensive list of attributes 请参阅http://www.termsys.demon.co.uk/vtansi.htm以获取更全面的属性列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM