minsi: Minimum viable ANSI terminal interface
Lassi Kortela 17 Oct 2020 11:55 UTC
As mentioned quite a while back on the SRFI lists, I once tried to
figure out a minimal API for Unix terminal control.
The point of this API is:
* To protect us normal plebs from obscure pitfalls like accidentally
writing terminal control codes when we intend to write plain text.
* To handle the lowest-level arcana: entering and exiting raw mode,
polling with a timeout, and figuring out where control codes begin and
end, separating the input into control codes and plain text strings.
Here it is, somewhat polished: https://github.com/lassik/minsi
./build.sh should compile it on Linux/*BSD/Mac.
./example is a program that reads terminal events (keystrokes and window
resize) and describes them on the screen as they come in.
Unix does window resize notifications via SIGWINCH. Since signal
handling is obscure and error-prone, minsi doesn't do it; example.c has
to install its own SIGWINCH handler. The library gives only a
minsiSetResizeFlag() function, which example.c calls to notify the
library of a window size change.
Here's the full API from minsi.h:
// Event types:
//
// '^' -- control character "@" "A" ... "?"
// 'c' -- character "ö"
// 'e' -- escape sequence "[1;10C"
// 'r' -- resize ""
struct minsi;
struct minsi *minsiFromFd(int fd);
struct minsi *minsiFromStdin(void);
struct minsi *minsiFromStdout(void);
int minsiSwitchToRawMode(struct minsi *minsi);
int minsiSwitchToOrigMode(struct minsi *minsi);
int minsiGetSize(struct minsi *minsi, int *out_x, int *out_y);
const char *minsiReadEvent(struct minsi *minsi);
void minsiWriteString(struct minsi *minsi, const char *string);
void minsiWriteEscape(struct minsi *minsi, const char *string);
void minsiWriteFlush(struct minsi *minsi);
void minsiSetResizeFlag(struct minsi *minsi);
Mouse events are not yet supported, but should be added.
- Thoughts in general?
- Is this level of abstraction a reasonable fit for SRFI 205 in particular?