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)
\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 中的普通终止符。