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

mav*_*vam 6 c c++ prompt colors 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;
}
Run Code Online (Sandbox Code Playgroud)

编译

clang++ editline.cc -ledit && ./a.out
Run Code Online (Sandbox Code Playgroud)

并且显示不幸的是只有未着色的提示:

:::     
Run Code Online (Sandbox Code Playgroud)

l'L*_*L'l 2

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

\1\033[36m\1:::\1\033[0m\1
|          |   |         |
|          |   |_Start   |_Stop
|          |
|_Start    |_Stop
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

手册页指出使用0来取消设置颜色,但有点不清楚它们的含义。

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

\1\033[36m:::\033[0m\1
Run Code Online (Sandbox Code Playgroud)

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

  • 我相信这整个问题是 libedit 中的一个错误。我已经联系了克里斯托·祖拉斯(Christos Zoulas)。如果我得到答案,我会在这里发帖。 (3认同)
  • 抱歉,我最终确实得到了回复,但忘记了这一切。“这是一个已知的错误(对于颜色提示)。它需要一些认真的工作才能修复,因为现在提示功能没有办法在正确的时间处理打印属性。我会研究它。” 不幸的是,它看起来好像还没有修复。 (2认同)