Supports seamless and efficient terminal resizing and multibyte/combining/wide characters. (Wide characters are those that use more than one terminal column.)
One of the trickier aspects is that readline only exports the cursor position as a byte offset (rl_point
), which won't correspond to the correct terminal cursor column for many strings with special characters. We therefore have to calculate the terminal cursor column ourselves. readline itself does it as part of the default rl_redisplay()
function.
-
Some invalid strings and meta characters could still cause the cursor position to be off (though it's only a visual annoyance).
-
Entering multibyte characters during search (e.g., Ctrl-R) does not work, due to a readline issue. The only workaround I've found is to let readline directly from stdin instead (e.g., via a
select(2)
loop: The readline manual has an example program). Buffering bytes until they form a complete multibyte character (e.g., viaget_wch()
+wcrtomb(3)
) doesn't help either.A neat setup on Linux is to use
signalfd(2)
(libevent might be a portable option) to catch theSIGWINCH
generated by the terminal resize in theselect(2)
(orepoll(7)
) loop and then callingresizeterm()
and updating the windows (the manualresizeterm()
is needed since we override ncurses' defaultSIGWINCH
handler). I have a WIP that uses that approach in a branch for another project. I could push it on request. -
To keep things simple, the readline (bottom) window does not scroll horizontally. It would require some care to get special characters right.
The contents of the top window is set to whatever is entered. To exit, press Ctrl-D on an empty line.
See LICENSE.txt. SPDX license identifiers are used in the source code.