all repos — minline @ 1b74ebe4775acccc41a9506cfd4727ff85a863ed

A minimalist but highly-customizable line editing library.

Experimenting with multiline support.
Cevasco, Fabio fabio.cevasco@siemens.com
Fri, 31 Jan 2020 17:21:15 +0100
commit

1b74ebe4775acccc41a9506cfd4727ff85a863ed

parent

1868fcf71d0bac973a0dd7fa9537088fd6793871

2 files changed, 23 insertions(+), 4 deletions(-)

jump to
M .gitignore.gitignore

@@ -1,3 +1,4 @@

nimcache/ nimline nimline.html +nimline.exe
M nimline.nimnimline.nim

@@ -74,6 +74,7 @@ queue: Deque[string]

max: int LineEditor* = object ## An object representing a line editor, used to process text typed in the terminal. completionCallback*: proc(ed: LineEditor): seq[string] + newLineCallback*: proc(ed: var LineEditor, prompt: string, c: int): string history: LineHistory line: Line mode: LineEditorMode

@@ -591,10 +592,15 @@ if esc:

esc = false continue elif c1 in {10, 13}: - stdout.write("\n") - ed.historyAdd() - ed.historyFlush() - return ed.line.text + if not ed.newLineCallback.isNil: + let line = ed.newLineCallback(ed, prompt, c1) + if line != "": + return line + else: + stdout.write("\n") + ed.historyAdd() + ed.historyFlush() + return ed.line.text elif c1 in {8, 127}: KEYMAP["backspace"](ed) elif c1 in PRINTABLE:

@@ -675,6 +681,18 @@ #

#testChar() proc testLineEditor() = var ed = initEditor(historyFile = "") + ed.newLineCallback = proc(ed: var LineEditor, prompt: string, c: int): string = + let lpar = ed.line.text.count("(") + let rpar = ed.line.text.count(")") + if (lpar != rpar): + #ed.line.text &= c.chr + stdout.write("\n" & prompt & " ... ") + return "" + else: + stdout.write("\n") + ed.historyAdd() + ed.historyFlush() + return ed.line.text while true: echo "---", ed.readLine("-> "), "---"