Skip to content

Commit 799eb10

Browse files
committed
Use a linenoise with win32 support
1 parent a450119 commit 799eb10

File tree

10 files changed

+1358
-325
lines changed

10 files changed

+1358
-325
lines changed

mk/rt.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ RUNTIME_CXXS_$(1) := \
7878
rt/arch/$$(HOST_$(1))/context.cpp \
7979
rt/arch/$$(HOST_$(1))/gpr.cpp
8080

81-
RUNTIME_CS_$(1) := rt/linenoise/linenoise.c
81+
RUNTIME_CS_$(1) := rt/linenoise/linenoise.c rt/linenoise/utf8.c
8282

8383
RUNTIME_S_$(1) := rt/arch/$$(HOST_$(1))/_context.S \
8484
rt/arch/$$(HOST_$(1))/ccall.S \

src/libstd/rl.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ extern mod rustrt {
99
fn linenoiseHistoryLoad(file: *c_char) -> c_int;
1010
fn linenoiseSetCompletionCallback(callback: *u8);
1111
fn linenoiseAddCompletion(completions: *(), line: *c_char);
12-
fn linenoiseClearScreen();
1312
}
1413

1514
/// Add a line to history
@@ -48,11 +47,6 @@ pub fn read(prompt: ~str) -> Option<~str> {
4847
}
4948
}
5049

51-
/// Clear the screen
52-
pub fn clear() {
53-
rustrt::linenoiseClearScreen();
54-
}
55-
5650
pub type CompletionCb = fn~(~str, fn(~str));
5751

5852
fn complete_key(_v: @CompletionCb) {}

src/rt/linenoise/README.markdown

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Linenoise
2+
3+
A minimal, zero-config, BSD licensed, readline replacement.
4+
5+
News: linenoise now includes minimal completion support, thanks to Pieter Noordhuis (@pnoordhuis).
6+
7+
News: linenoise is now part of [Android](http://android.git.kernel.org/?p=platform/system/core.git;a=tree;f=liblinenoise;h=56450eaed7f783760e5e6a5993ef75cde2e29dea;hb=HEAD Android)!
8+
9+
## Can a line editing library be 20k lines of code?
10+
11+
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?
12+
13+
So what usually happens is either:
14+
15+
* 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).
16+
* Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance).
17+
18+
The result is a pollution of binaries without line editing support.
19+
20+
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, supporing 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.
21+
22+
## Terminals, in 2010.
23+
24+
Apparently almost every terminal you can happen to use today has some kind of support for VT100 alike escape sequences. So I tried to write a lib using just very basic VT100 features. The resulting library appears to work everywhere I tried to use it.
25+
26+
Since it's so young I guess there are a few bugs, or the lib may not compile or work with some operating system, but it's a matter of a few weeks and eventually we'll get it right, and there will be no excuses for not shipping command line tools without built-in line editing support.
27+
28+
The library is currently less than 400 lines of code. In order to use it in your project just look at the *example.c* file in the source distribution, it is trivial. Linenoise is BSD code, so you can use both in free software and commercial software.
29+
30+
## Tested with...
31+
32+
* Linux text only console ($TERM = linux)
33+
* Linux KDE terminal application ($TERM = xterm)
34+
* Linux xterm ($TERM = xterm)
35+
* Mac OS X iTerm ($TERM = xterm)
36+
* Mac OS X default Terminal.app ($TERM = xterm)
37+
* OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)
38+
* IBM AIX 6.1
39+
* FreeBSD xterm ($TERM = xterm)
40+
41+
Please test it everywhere you can and report back!
42+
43+
## Let's push this forward!
44+
45+
Please fork it and add something interesting and send me a pull request. What's especially interesting are fixes, new key bindings, completion.
46+
47+
Send feedbacks to antirez at gmail

src/rt/linenoise/example.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "linenoise.h"
4+
5+
#ifndef NO_COMPLETION
6+
void completion(const char *buf, linenoiseCompletions *lc) {
7+
if (buf[0] == 'h') {
8+
linenoiseAddCompletion(lc,"hello");
9+
linenoiseAddCompletion(lc,"hello there");
10+
}
11+
}
12+
#endif
13+
14+
int main(void) {
15+
char *line;
16+
17+
#ifndef NO_COMPLETION
18+
linenoiseSetCompletionCallback(completion);
19+
#endif
20+
linenoiseHistoryLoad("history.txt"); /* Load the history at startup */
21+
while((line = linenoise("hello> ")) != NULL) {
22+
if (line[0] != '\0') {
23+
printf("echo: '%s'\n", line);
24+
linenoiseHistoryAdd(line);
25+
linenoiseHistorySave("history.txt"); /* Save every new entry */
26+
}
27+
free(line);
28+
}
29+
return 0;
30+
}

0 commit comments

Comments
 (0)