Dawid Ciężarkiewicz (dpc)

Mostly coding and rants

github twitter linkedin stackoverflow gitter pinboard email rss
Asynchronous GNU Readline printing
Sep 15, 2014
2 minutes read

Some while ago I’ve spend my time developing a XMPP command line client which is using strophe XMPP library to handle XMPP and GNU Readline for I/O.

The idea was to have a readline prompt at the bottom and yet be able to asynchronously print incoming messages above it - in the “log window”.

It seems that many people were looking for solution already:

I haven’t found any satisfying answer on the web so I’d like to present my own solution.

Basic idea is to use alternate (asynchronous) GNU Readline interface and on each new asynchronous print:

  • save a copy of current line state
  • clear both prompt and current line (content + position)
  • force screen update
  • print asynchronous event (followed by a newline)
  • restore prompt and current line state
  • force screen update

Simple it is, indeed and you can see a working code if you don’t believe.

The only thing that I was unable to get is preventing putting the original confirmed readline buffer in “log window”. As this is not a big deal for my requirements the complete and universal solution would be able to change what the user typed in the readline buffer just before it’s getting scrolled up and becoming part of the “log window”.

I hope someone will fine it useful, like I do.

Update:

Below is a patch by Denis Linvinus:

http://pastebin.com/SA87Lxqq

that he uses to get more “unrestricted log window with a prompt” for his project and wanted me to share with you too. Generally, I think it’s a bit unclean because of using vt100 escape sequences so I’m not going to merge it, but if anyone finds it useful, it’s good for everyone.


Back to posts