6

I am trying to colorize the prompt of an application powered by libedit, but I the color simply does not show up. 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:

:::     
mavam
  • 12,242
  • 10
  • 53
  • 87

3 Answers3

3

Editline doesn't support colour prompts. 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).

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.

Unfortunately, since a text undergoes complex transformations before reaching the terminal, some information is lost in translation, like the colour.

Nick Zavaritsky
  • 1,429
  • 8
  • 19
  • It would be great if these changes make it up into the upstream master repository eventually. – mavam Mar 29 '16 at 18:22
2

\1 is used as a stop/start literal character, so that seems to be the correct behavior.

\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.

     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.

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.

l'L'l
  • 44,951
  • 10
  • 95
  • 146
  • Interestingly `\1\[\033[36m\]\1:::\1\[\033[0m\]\1` gave me a prompt of `[][]:::` with the inner `][` being colorized. It seemed that `\1` terminates the coloring. Indeed, using `"\1\033[36m:::\033[0m\1 "`, without the escape characters in the middle, yields a colored prompt! – mavam Jan 20 '14 at 18:22
  • When I'm using a colored prompt, the backspace key doesn't work anymore, it's printing just escaped characters to the TTY. Any ideas what may be the issue here? – mavam Feb 01 '14 at 02:05
  • 3
    I believe this whole issue is a bug in libedit. I have contacted Christos Zoulas about it. I'll post here if I get an answer. – Chewi Feb 28 '14 at 16:09
  • @Chewi: thanks for contacting the author, I'd be thrilled if this gets fixed! – mavam Mar 11 '14 at 05:58
  • @Chewi: I have the same issue, although in a more complicated setup. I am interested in the result as well.' – Stefano Borini Feb 20 '15 at 13:52
  • 2
    Sorry, I did eventually get a reply but forgot all about this. "This is a known bug (for the color prompt). It needs some serious work to get fixed because right now the prompt function does not have a way to deal with printing attributes at the right time. I will look into it." Unfortunately it looks as though it still hasn't been fixed. – Chewi Feb 21 '15 at 15:28
1

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'

see http://www.termsys.demon.co.uk/vtansi.htm for a more comprehensive list of attributes

user3629249
  • 16,402
  • 1
  • 16
  • 17