I am using the (excellent) readline
(version 6.3, default [non-vi] mode) library from within my own program, running in a Terminal window (on a PC). There is a problem when there is previous output not terminated by newline when readline()
is called.
#include <stdio.h>
#include <readline/readline.h>
void main(void)
{
// Previous output from some other part of application
// which *may* have output stuff *not* terminated with a '\n'
printf("Hello ");
fflush(stdout);
char *in = readline("OK> ");
}
So the line looks like:
Hello OK> <caret here>
If you type a small number of characters (up to 5?) and then, say, Ctrl+U
(may be others) to delete your input so far it all seems well --- readline()
moves the caret back to just after its own prompt. However, try typing, say:
123456 <Ctrl+U>
Now it deletes back into the Hello
, leaving just Hell
on the line, followed by the caret.
I need one of two possible solutions:
This does look like a bug, now that I have realised it depends on how many characters are typed on the line where it goes wrong. Any fix/workaround?
Alternatively, is there a
readline
library call I could make which would tell me what position/column the caret is at before I invokereadline()
? Then at least I could recognise the fact that that I am at the end of an existing line and output a\n
so as to position myself at the start of a new line first.
P.S.
Is this an OK place to ask about readline
programming under Ubuntu or should I be posting at stackoverflow.com?
readline
can do the erase. (But your #2 is not enough: there is no erasing there, only cursor repositioning, there must be an additional erase-to-EOL output?) If I could force it to always choose backspacing that would solve: is there a way I can tell it to choose that, or that my terminal cannot do your #2? – JonBrave Dec 17 '16 at 16:21readline
to work as desired if there is previous output. – JonBrave Dec 20 '16 at 16:20\n
or\r
in the prompt)? It kind of work. You can even prompt with\r<lot of spaces>\r<your prompt>
so that any previous characters are erased (and as a welcome side-effect, your prompt will be long enough to disable the undesirable behaviour of Ctrl-U). – xhienne Dec 20 '16 at 17:08\r
I will lose it, and with\n
I will get a blank line for every usual case where there was nothing. Neither of those is "acceptable"! – JonBrave Dec 21 '16 at 16:01bash
suffers the same problem. Try:echo -n abcdefghi<RETURN>
. Then type a lot of characters (30?123456789012345678901234567890
and thenCtrl+U
. Your prompt gets overwritten. – JonBrave Dec 21 '16 at 16:05