use linenoise instead of GNU readline
This commit is contained in:
parent
87f65f5e72
commit
aa8b470cf6
10 changed files with 1351 additions and 66 deletions
|
|
@ -17,7 +17,7 @@ template bootSwitch(name, expr, userString: expr): expr =
|
||||||
const name = if expr: " " & userString else: ""
|
const name = if expr: " " & userString else: ""
|
||||||
|
|
||||||
bootSwitch(usedRelease, defined(release), "-d:release")
|
bootSwitch(usedRelease, defined(release), "-d:release")
|
||||||
bootSwitch(usedGnuReadline, defined(useGnuReadline), "-d:useGnuReadline")
|
bootSwitch(usedGnuReadline, defined(useLinenoise), "-d:useLinenoise")
|
||||||
bootSwitch(usedNoCaas, defined(noCaas), "-d:noCaas")
|
bootSwitch(usedNoCaas, defined(noCaas), "-d:noCaas")
|
||||||
bootSwitch(usedBoehm, defined(boehmgc), "--gc:boehm")
|
bootSwitch(usedBoehm, defined(boehmgc), "--gc:boehm")
|
||||||
bootSwitch(usedMarkAndSweep, defined(gcmarkandsweep), "--gc:markAndSweep")
|
bootSwitch(usedMarkAndSweep, defined(gcmarkandsweep), "--gc:markAndSweep")
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,8 @@
|
||||||
import
|
import
|
||||||
strutils
|
strutils
|
||||||
|
|
||||||
when not defined(windows) and defined(useGnuReadline):
|
# support '-d:useGnuReadline' for backwards compatibility:
|
||||||
|
when not defined(windows) and (defined(useGnuReadline) or defined(useLinenoise)):
|
||||||
import rdstdin
|
import rdstdin
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,7 @@ path="$lib/wrappers"
|
||||||
# path="$lib/wrappers/lua"
|
# path="$lib/wrappers/lua"
|
||||||
# path="$lib/wrappers/opengl"
|
# path="$lib/wrappers/opengl"
|
||||||
path="$lib/wrappers/pcre"
|
path="$lib/wrappers/pcre"
|
||||||
path="$lib/wrappers/readline"
|
path="$lib/wrappers/linenoise"
|
||||||
path="$lib/wrappers/sdl"
|
path="$lib/wrappers/sdl"
|
||||||
# path="$lib/wrappers/x11"
|
# path="$lib/wrappers/x11"
|
||||||
path="$lib/wrappers/zip"
|
path="$lib/wrappers/zip"
|
||||||
|
|
|
||||||
2
koch.nim
2
koch.nim
|
|
@ -57,7 +57,7 @@ Possible Commands:
|
||||||
Boot options:
|
Boot options:
|
||||||
-d:release produce a release version of the compiler
|
-d:release produce a release version of the compiler
|
||||||
-d:tinyc include the Tiny C backend (not supported on Windows)
|
-d:tinyc include the Tiny C backend (not supported on Windows)
|
||||||
-d:useGnuReadline use the GNU readline library for interactive mode
|
-d:useLinenoise use the linenoise library for interactive mode
|
||||||
(not needed on Windows)
|
(not needed on Windows)
|
||||||
-d:nativeStacktrace use native stack traces (only for Mac OS X or Linux)
|
-d:nativeStacktrace use native stack traces (only for Mac OS X or Linux)
|
||||||
-d:noCaas build Nim without CAAS support
|
-d:noCaas build Nim without CAAS support
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,8 @@
|
||||||
# distribution, for details about the copyright.
|
# distribution, for details about the copyright.
|
||||||
#
|
#
|
||||||
|
|
||||||
## This module contains code for reading from `stdin`:idx:. On UNIX the GNU
|
## This module contains code for reading from `stdin`:idx:. On UNIX the
|
||||||
## readline library is wrapped and set up to provide default key bindings
|
## linenoise library is wrapped and set up to provide default key bindings
|
||||||
## (e.g. you can navigate with the arrow keys). On Windows ``system.readLine``
|
## (e.g. you can navigate with the arrow keys). On Windows ``system.readLine``
|
||||||
## is used. This suffices because Windows' console already provides the
|
## is used. This suffices because Windows' console already provides the
|
||||||
## wanted functionality.
|
## wanted functionality.
|
||||||
|
|
@ -100,34 +100,28 @@ when defined(Windows):
|
||||||
stdout.write "\n"
|
stdout.write "\n"
|
||||||
|
|
||||||
else:
|
else:
|
||||||
import readline, history, termios, unsigned
|
import linenoise, termios, unsigned
|
||||||
|
|
||||||
proc readLineFromStdin*(prompt: string): TaintedString {.
|
proc readLineFromStdin*(prompt: string): TaintedString {.
|
||||||
tags: [ReadIOEffect, WriteIOEffect].} =
|
tags: [ReadIOEffect, WriteIOEffect].} =
|
||||||
var buffer = readline.readLine(prompt)
|
var buffer = linenoise.readLine(prompt)
|
||||||
if isNil(buffer): quit(0)
|
if isNil(buffer): quit(0)
|
||||||
result = TaintedString($buffer)
|
result = TaintedString($buffer)
|
||||||
if result.string.len > 0:
|
if result.string.len > 0:
|
||||||
add_history(buffer)
|
historyAdd(buffer)
|
||||||
readline.free(buffer)
|
linenoise.free(buffer)
|
||||||
|
|
||||||
proc readLineFromStdin*(prompt: string, line: var TaintedString): bool {.
|
proc readLineFromStdin*(prompt: string, line: var TaintedString): bool {.
|
||||||
tags: [ReadIOEffect, WriteIOEffect].} =
|
tags: [ReadIOEffect, WriteIOEffect].} =
|
||||||
var buffer = readline.readLine(prompt)
|
var buffer = linenoise.readLine(prompt)
|
||||||
if isNil(buffer): quit(0)
|
if isNil(buffer): quit(0)
|
||||||
line = TaintedString($buffer)
|
line = TaintedString($buffer)
|
||||||
if line.string.len > 0:
|
if line.string.len > 0:
|
||||||
add_history(buffer)
|
historyAdd(buffer)
|
||||||
readline.free(buffer)
|
linenoise.free(buffer)
|
||||||
# XXX how to determine CTRL+D?
|
# XXX how to determine CTRL+D?
|
||||||
result = true
|
result = true
|
||||||
|
|
||||||
# initialization:
|
|
||||||
# disable auto-complete:
|
|
||||||
proc doNothing(a, b: cint): cint {.cdecl, procvar.} = discard
|
|
||||||
|
|
||||||
discard readline.bind_key('\t'.ord, doNothing)
|
|
||||||
|
|
||||||
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
|
proc readPasswordFromStdin*(prompt: string, password: var TaintedString):
|
||||||
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
|
bool {.tags: [ReadIOEffect, WriteIOEffect].} =
|
||||||
password.setLen(0)
|
password.setLen(0)
|
||||||
|
|
|
||||||
25
lib/wrappers/linenoise/LICENSE.txt
Normal file
25
lib/wrappers/linenoise/LICENSE.txt
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||||
|
Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||||
|
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||||
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
52
lib/wrappers/linenoise/README.markdown
Normal file
52
lib/wrappers/linenoise/README.markdown
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
# Linenoise
|
||||||
|
|
||||||
|
A minimal, zero-config, BSD licensed, readline replacement used in Redis,
|
||||||
|
MongoDB, and Android.
|
||||||
|
|
||||||
|
* Single and multi line editing mode with the usual key bindings implemented.
|
||||||
|
* History handling.
|
||||||
|
* Completion.
|
||||||
|
* About 1,100 lines of BSD license source code.
|
||||||
|
* Only uses a subset of VT100 escapes (ANSI.SYS compatible).
|
||||||
|
|
||||||
|
## Can a line editing library be 20k lines of code?
|
||||||
|
|
||||||
|
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?
|
||||||
|
|
||||||
|
So what usually happens is either:
|
||||||
|
|
||||||
|
* 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).
|
||||||
|
* Smaller programs not using a configure script not supporting line editing at all (A problem we had with Redis-cli for instance).
|
||||||
|
|
||||||
|
The result is a pollution of binaries without line editing support.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Terminals, in 2010.
|
||||||
|
|
||||||
|
Apparently almost every terminal you can happen to use today has some kind of support for basic VT100 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, and now can work even on ANSI.SYS compatible terminals, since no
|
||||||
|
VT220 specific sequences are used anymore.
|
||||||
|
|
||||||
|
The library is currently about 1100 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.
|
||||||
|
|
||||||
|
## Tested with...
|
||||||
|
|
||||||
|
* Linux text only console ($TERM = linux)
|
||||||
|
* Linux KDE terminal application ($TERM = xterm)
|
||||||
|
* Linux xterm ($TERM = xterm)
|
||||||
|
* Linux Buildroot ($TERM = vt100)
|
||||||
|
* Mac OS X iTerm ($TERM = xterm)
|
||||||
|
* Mac OS X default Terminal.app ($TERM = xterm)
|
||||||
|
* OpenBSD 4.5 through an OSX Terminal.app ($TERM = screen)
|
||||||
|
* IBM AIX 6.1
|
||||||
|
* FreeBSD xterm ($TERM = xterm)
|
||||||
|
* ANSI.SYS
|
||||||
|
|
||||||
|
Please test it everywhere you can and report back!
|
||||||
|
|
||||||
|
## Let's push this forward!
|
||||||
|
|
||||||
|
Patches should be provided in the respect of linenoise sensibility for small
|
||||||
|
easy to understand code.
|
||||||
|
|
||||||
|
Send feedbacks to antirez at gmail
|
||||||
1107
lib/wrappers/linenoise/linenoise.c
Normal file
1107
lib/wrappers/linenoise/linenoise.c
Normal file
File diff suppressed because it is too large
Load diff
68
lib/wrappers/linenoise/linenoise.h
Normal file
68
lib/wrappers/linenoise/linenoise.h
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
/* linenoise.h -- VERSION 1.0
|
||||||
|
*
|
||||||
|
* Guerrilla line editing library against the idea that a line editing lib
|
||||||
|
* needs to be 20,000 lines of C code.
|
||||||
|
*
|
||||||
|
* See linenoise.c for more information.
|
||||||
|
*
|
||||||
|
* ------------------------------------------------------------------------
|
||||||
|
*
|
||||||
|
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||||
|
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __LINENOISE_H
|
||||||
|
#define __LINENOISE_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct linenoiseCompletions {
|
||||||
|
size_t len;
|
||||||
|
char **cvec;
|
||||||
|
} linenoiseCompletions;
|
||||||
|
|
||||||
|
typedef void(*linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
|
||||||
|
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
|
||||||
|
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
|
||||||
|
|
||||||
|
char *linenoise(const char *prompt);
|
||||||
|
int linenoiseHistoryAdd(const char *line);
|
||||||
|
int linenoiseHistorySetMaxLen(int len);
|
||||||
|
int linenoiseHistorySave(const char *filename);
|
||||||
|
int linenoiseHistoryLoad(const char *filename);
|
||||||
|
void linenoiseClearScreen(void);
|
||||||
|
void linenoiseSetMultiLine(int ml);
|
||||||
|
void linenoisePrintKeyCodes(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __LINENOISE_H */
|
||||||
38
lib/wrappers/linenoise/linenoise.nim
Normal file
38
lib/wrappers/linenoise/linenoise.nim
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# Nim's Runtime Library
|
||||||
|
# (c) Copyright 2010 Andreas Rumpf
|
||||||
|
#
|
||||||
|
# See the file "copying.txt", included in this
|
||||||
|
# distribution, for details about the copyright.
|
||||||
|
#
|
||||||
|
|
||||||
|
type
|
||||||
|
Completions* {.importc: "linenoiseCompletions", header: "linenoise.h".} = object
|
||||||
|
len* {.importc: "len".}: csize
|
||||||
|
cvec* {.importc: "cvec".}: cstringArray
|
||||||
|
|
||||||
|
CompletionCallback* = proc (a2: cstring; a3: ptr Completions) {.cdecl.}
|
||||||
|
|
||||||
|
{.compile: "linenoise.c".}
|
||||||
|
|
||||||
|
proc setCompletionCallback*(a2: ptr CompletionCallback) {.
|
||||||
|
importc: "linenoiseSetCompletionCallback", header: "linenoise.h".}
|
||||||
|
proc addCompletion*(a2: ptr Completions; a3: cstring) {.
|
||||||
|
importc: "linenoiseAddCompletion", header: "linenoise.h".}
|
||||||
|
proc readLine*(prompt: cstring): cstring {.importc: "linenoise", header: "linenoise.h".}
|
||||||
|
proc historyAdd*(line: cstring): cint {.importc: "linenoiseHistoryAdd",
|
||||||
|
header: "linenoise.h", discardable.}
|
||||||
|
proc historySetMaxLen*(len: cint): cint {.importc: "linenoiseHistorySetMaxLen",
|
||||||
|
header: "linenoise.h".}
|
||||||
|
proc historySave*(filename: cstring): cint {.importc: "linenoiseHistorySave",
|
||||||
|
header: "linenoise.h".}
|
||||||
|
proc historyLoad*(filename: cstring): cint {.importc: "linenoiseHistoryLoad",
|
||||||
|
header: "linenoise.h".}
|
||||||
|
proc clearScreen*() {.importc: "linenoiseClearScreen", header: "linenoise.h".}
|
||||||
|
proc setMultiLine*(ml: cint) {.importc: "linenoiseSetMultiLine",
|
||||||
|
header: "linenoise.h".}
|
||||||
|
proc printKeyCodes*() {.importc: "linenoisePrintKeyCodes",
|
||||||
|
header: "linenoise.h".}
|
||||||
|
|
||||||
|
proc free*(s: cstring) {.importc: "free", header: "<stdlib.h>".}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue