Miniedit is a command line editor library providing generic text entry capabilities. Specifically, miniedit implements this function:
wchar_t *editline(const wchar_t *prompt);
editline() will print prompt, read one line of text from user and return it, without the final newline, in a string allocated by malloc().
You can brose miniedit sources at [1] or grab them via Mercurial like this:
hg clone http://hg.tx97.net/miniedit
single source file, under 1000 lines of code
works under both Unix-like system and Windows
good Unicode support on unices (including 2-column characters)
some Unicode support on Windows (no 2-column characters)
Here’s the simplest example:
/* example.c */
#include "miniedit.h"
#include <locale.h>
#include <stdio.h>
int main() {
setlocale(LC_CTYPE, "");
wchar_t *name = editline(L"Type in your name, friend: ");
if (name) {
wprintf(L"Hello and welcome, %ls!\n", name);
free(name);
} else {
wprintf(L"¯\_(ツ)_/¯\n");
}
return 0;
}
Note several things:
You need to initialize your locale using setlocale(). Strictly speaking, this is only needed under unices, but it won’t hurt on Windows as well.
editline() will return NULL on end of file, if the user pressed Ctrl-D while input string was empty, or if the user pressed Ctrl-C.
You are responsible for freeing the returned string.
To compile the example above for a Unix, you’ll need a C99 compiler and termcap library. On *BSD all the needed termcap files come with the system; on a Debian-based system you’ll need to install libncurses5-dev package first. Once you’ve done that, you can build the above example like this:
cc -std=c99 -o example example.c miniedit.c -ltermcap
To compile it for Windows, you’ll only need a C99 compiler. For example, if you have a MinGW toolset installed, you could do it this way:
i686-pc-mingw32-gcc -std=c99 -o example.exe example.c miniedit.c
There are multiple libraries that cover the functionality of miniedit. GNU readline [2] and libedit [3] are the most widely used of them. Miniedit was developed as a response to those two libraries being way too big and lacking Windows support.
You might also find the linenoise library [4] useful.