Note that there are some explanatory texts on larger screens.

plurals
  1. POGNU Readline: how to clear the input line?
    primarykey
    data
    text
    <p>I use GNU Readline in the "select" fashion, by registering a callback function like so:</p> <pre><code>rl_callback_handler_install("", on_readline_input); </code></pre> <p>And then hooking up <code>rl_callback_read_char</code> as the callback for my <code>select()</code> loop for <code>STDIN_FILENO</code>. That's all pretty standard stuff, and works fine.</p> <p>Now, my program asynchronously prints messages to the screen, sometimes interleaved with input from the user. A "clean" session would look like this:</p> <pre><code>user input SERVER OUTPUT SERVER OUTPUT user input SERVER OUTPUT </code></pre> <p>But what if the user is midway through a line when the server response arrives? Then it gets ugly:</p> <pre><code>user input SERVER OUTPUT user inSERVER OUTPUT put SERVER OUTPUT </code></pre> <p>I fixed this simply by printing a newline before the server output if the user had typed anything (this is easy to tell by checking <code>rl_line_buffer</code>), and then doing <code>rl_forced_update_display()</code> after printing the server output. Now it looks like this:</p> <pre><code>user input SERVER OUTPUT user in SERVER OUTPUT user input SERVER OUTPUT </code></pre> <p>This is better, but still not perfect. The problem comes when the user typed an entire line but didn't yet press Enter--then it looks like this:</p> <pre><code>user input SERVER OUTPUT user input SERVER OUTPUT user input SERVER OUTPUT </code></pre> <p>This is bad because it appears to the user that they typed three commands (three responses for three inputs is just as possible as three responses for two inputs, which is what actually happened).</p> <p>A nasty hack (which works) is to do this:</p> <pre><code>user input SERVER OUTPUT user input - INCOMPLETE SERVER OUTPUT user input SERVER OUTPUT </code></pre> <p>I figured I could improve this by printing backspace ('\b') characters instead of <code>" - INCOMPLETE"</code>, but that doesn't seem to do anything at all on my terminal (gnome-terminal on Ubuntu Hardy). <code>printf("ABC\b");</code> just prints <code>ABC</code>, for whatever reason.</p> <p>So how can I erase the incomplete input line? Either by printing backspaces somehow (I can figure out how many to print--it's <code>strlen(rl_line_buffer)</code>), or by using some Readline facility I don't yet know about?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload