Looking into linenoise
- 2 minutes read - 313 wordsMotivation
Linenoise is an editing library for command line input. It is written in C and aims to be minimal, permissively licensed and used as GNU readline replacement. After GNU readline switched to GPLv3 or later as its license, looking at alternative editing libraries became a necessity for some project. It is time to explore Linenoise as an alternative.
Introduction
The project lists following features:
- Single and multi line editing mode with the usual key bindings
- History handling
- Completion
- Hints (suggestions at the right of the prompt as you type)
- Multiplexing mode, with prompt hiding/restoring for asynchronous output
- About ~850 lines (comments and spaces excluded) of BSD license source code
- Only uses a subset of VT100 escapes (ANSI.SYS compatible)
From the feature list, this looks awesome and the small code size is a huge benefit. In comparison GNU readline brings in around 30k lines of code and libedit requires around 20k lines of code. Linenoise with its <1k lines of code make it easy to embed in existing and new projects and also makes it rather easy to audit and maintain. However Linenoise is not API compatible with GNU readline. If you are currently using GNU readline, then there is no easy switch to use Linenoise if you are using asynchronous input via an event loop or external sources (compared to blocking input).
Terminal control
The usage of VT100 escape is crucial for any command line input and editing. Linenoise tries to use a little control sequences as possible. This increases the compatibility and keeps the code simple.
Sample application
In case of simple command line programs that can use blocking command line input, the usage is simple.
#include <stdio.h>
#include <stdlib.h>
#include "linenoise.h"
int main(int argc, char **argv)
{
char *line;
line = linenoise("hello> ");
printf("%s\n", line);
free(line);
}
References
All sample code can be found on GitHub in the linenoise-examples repository.