linenoise.c 1.2025.9 → 2.2026.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{README.markdown → README.md} +81 -27
- package/{linenoise.c → linenoise/linenoise.c} +1762 -1349
- package/linenoise/linenoise.h +114 -0
- package/linenoise.h +4 -112
- package/package.json +23 -7
- package/Makefile +0 -7
|
@@ -1,38 +1,17 @@
|
|
|
1
1
|
# Linenoise
|
|
2
2
|
|
|
3
3
|
A minimal, zero-config, BSD licensed, readline replacement used in Redis,
|
|
4
|
-
MongoDB, Android and many other projects.
|
|
4
|
+
MongoDB, Android and many other projects.
|
|
5
5
|
|
|
6
6
|
* Single and multi line editing mode with the usual key bindings implemented.
|
|
7
7
|
* History handling.
|
|
8
8
|
* Completion.
|
|
9
9
|
* Hints (suggestions at the right of the prompt as you type).
|
|
10
10
|
* Multiplexing mode, with prompt hiding/restoring for asynchronous output.
|
|
11
|
-
*
|
|
11
|
+
* UTF-8 support for multi-byte characters and emoji.
|
|
12
|
+
* About ~1100 lines (comments and spaces excluded) of BSD license source code.
|
|
12
13
|
* Only uses a subset of VT100 escapes (ANSI.SYS compatible).
|
|
13
14
|
|
|
14
|
-
## Installation
|
|
15
|
-
|
|
16
|
-
Run:
|
|
17
|
-
```bash
|
|
18
|
-
$ npm i linenoise.c
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
And then include `linenoise.h` as follows:
|
|
22
|
-
```c
|
|
23
|
-
#include "node_modules/linenoise.c/linenoise.h"
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
You may also want to include `linenoise.c` as follows:
|
|
27
|
-
```c
|
|
28
|
-
#ifndef __LINENOISE_C__
|
|
29
|
-
#define __LINENOISE_C__
|
|
30
|
-
#include "node_modules/linenoise.c/linenoise.c"
|
|
31
|
-
#endif
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
This will include both the function declaration and their definitions into a single file.
|
|
35
|
-
|
|
36
15
|
## Can a line editing library be 20k lines of code?
|
|
37
16
|
|
|
38
17
|
Line editing with some support for history is a really important feature for command line utilities. Instead of retyping almost the same stuff again and again it's just much better to hit the up arrow and edit on syntax errors, or in order to try a slightly different command. But apparently code dealing with terminals is some sort of Black Magic: readline is 30k lines of code, libedit 20k. Is it reasonable to link small utilities to huge libraries just to get a minimal support for line editing?
|
|
@@ -41,7 +20,7 @@ So what usually happens is either:
|
|
|
41
20
|
|
|
42
21
|
* Large programs with configure scripts disabling line editing if readline is not present in the system, or not supporting it at all since readline is GPL licensed and libedit (the BSD clone) is not as known and available as readline is (real world example of this problem: Tclsh).
|
|
43
22
|
* Smaller programs not using a configure script not supporting line editing at all (A problem we had with `redis-cli`, for instance).
|
|
44
|
-
|
|
23
|
+
|
|
45
24
|
The result is a pollution of binaries without line editing support.
|
|
46
25
|
|
|
47
26
|
So I spent more or less two hours doing a reality check resulting in this little library: is it *really* needed for a line editing library to be 20k lines of code? Apparently not, it is possibe to get a very small, zero configuration, trivial to embed library, that solves the problem. Smaller programs will just include this, supporting line editing out of the box. Larger programs may use this little library or just checking with configure if readline/libedit is available and resorting to Linenoise if not.
|
|
@@ -78,6 +57,48 @@ easy to understand code.
|
|
|
78
57
|
|
|
79
58
|
Send feedbacks to antirez at gmail
|
|
80
59
|
|
|
60
|
+
|
|
61
|
+
# Installation
|
|
62
|
+
|
|
63
|
+
Run:
|
|
64
|
+
|
|
65
|
+
```sh
|
|
66
|
+
$ npm i linenoise.c
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
And then include `linenoise.h` as follows:
|
|
70
|
+
|
|
71
|
+
```c
|
|
72
|
+
// main.c
|
|
73
|
+
#include "node_modules/linenoise.c/linenoise.h"
|
|
74
|
+
|
|
75
|
+
int main() { /* ... */ }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
And then compile with `clang` or `gcc` as usual.
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
$ clang main.c # or, use gcc
|
|
82
|
+
$ gcc main.c
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
You may also use a simpler approach:
|
|
86
|
+
|
|
87
|
+
```c
|
|
88
|
+
// main.c
|
|
89
|
+
#include <linenoise.h>
|
|
90
|
+
|
|
91
|
+
int main() { /* ... */ }
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
If you add the path `node_modules/linenoise.c` to your compiler's include paths.
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
$ clang -I./node_modules/linenoise.c main.c # or, use gcc
|
|
98
|
+
$ gcc -I./node_modules/linenoise.c main.c
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
|
|
81
102
|
# The API
|
|
82
103
|
|
|
83
104
|
Linenoise is very easy to use, and reading the example shipped with the
|
|
@@ -155,7 +176,7 @@ just that. Both functions return -1 on error and 0 on success.
|
|
|
155
176
|
|
|
156
177
|
Sometimes it is useful to allow the user to type passwords or other
|
|
157
178
|
secrets that should not be displayed. For such situations linenoise supports
|
|
158
|
-
a "mask mode" that will just replace the characters the user is typing
|
|
179
|
+
a "mask mode" that will just replace the characters the user is typing
|
|
159
180
|
with `*` characters, like in the following example:
|
|
160
181
|
|
|
161
182
|
$ ./linenoise_example
|
|
@@ -363,14 +384,47 @@ example using select(2) and the asynchronous API:
|
|
|
363
384
|
|
|
364
385
|
You can test the example by running the example program with the `--async` option.
|
|
365
386
|
|
|
387
|
+
## Running the tests
|
|
388
|
+
|
|
389
|
+
To run the test suite:
|
|
390
|
+
|
|
391
|
+
make test
|
|
392
|
+
|
|
393
|
+
The tests will display a virtual terminal showing linenoise output in real-time, making it easy to see what's being tested and debug any failures.
|
|
394
|
+
|
|
395
|
+
### What the tests cover
|
|
396
|
+
|
|
397
|
+
The test suite verifies:
|
|
398
|
+
|
|
399
|
+
* Basic typing and cursor movement (left, right, home, end)
|
|
400
|
+
* Backspace and delete operations
|
|
401
|
+
* UTF-8 multi-byte characters (accented letters, CJK)
|
|
402
|
+
* Emoji and grapheme clusters (skin tones, ZWJ sequences like flags)
|
|
403
|
+
* Horizontal scrolling for long lines
|
|
404
|
+
* Multiline mode editing and navigation
|
|
405
|
+
* History navigation in multiline mode
|
|
406
|
+
* Word and line deletion (Ctrl-W, Ctrl-U)
|
|
407
|
+
|
|
408
|
+
### How the test harness works
|
|
409
|
+
|
|
410
|
+
The test program (`linenoise-test.c`) implements a VT100 terminal emulator that captures and verifies linenoise output:
|
|
411
|
+
|
|
412
|
+
1. **Fork and pipes**: The test harness forks `linenoise-example`, connecting to it via pipes. The child process sees `LINENOISE_ASSUME_TTY=1` to enable terminal mode despite not having a real TTY.
|
|
413
|
+
2. **VT100 emulator**: A minimal VT100 emulator parses escape sequences (cursor movement, screen clearing, etc.) and maintains a virtual screen buffer. Each cell stores a complete UTF-8 grapheme cluster and its display width.
|
|
414
|
+
3. **Visual rendering**: After each operation, the virtual screen is rendered to your real terminal with a border, so you can watch the test execute and see exactly what linenoise is displaying.
|
|
415
|
+
4. **Assertions**: Tests verify screen contents and cursor position against expected values.
|
|
416
|
+
|
|
417
|
+
This approach tests linenoise as users actually experience it, catching rendering bugs that unit tests would miss.
|
|
418
|
+
|
|
366
419
|
## Related projects
|
|
367
420
|
|
|
368
|
-
* [Linenoise NG](https://github.com/arangodb/linenoise-ng) is a fork of Linenoise that aims to add more advanced features like
|
|
421
|
+
* [Linenoise NG](https://github.com/arangodb/linenoise-ng) is a fork of Linenoise that aims to add more advanced features like Windows support and other features. Uses C++ instead of C as development language.
|
|
369
422
|
* [Linenoise-swift](https://github.com/andybest/linenoise-swift) is a reimplementation of Linenoise written in Swift.
|
|
370
423
|
|
|
371
424
|
<br>
|
|
372
425
|
<br>
|
|
373
426
|
|
|
374
427
|
|
|
428
|
+
[](https://github.com/antirez/linenoise)
|
|
375
429
|
[](https://nodef.github.io)
|
|
376
430
|

|