all repos — min @ ea9b697606d17ed5c850e805be78e21362a157f7

A small but practical concatenative programming language.

Started implementing history support, fixed mdReplace behavior.
Fabio Cevasco h3rald@h3rald.com
Fri, 09 Sep 2016 09:24:25 +0200
commit

ea9b697606d17ed5c850e805be78e21362a157f7

parent

493da2b629b927cf87894904d6de0b09b5e2aa40

1 files changed, 34 insertions(+), 6 deletions(-)

jump to
M core/line.nimcore/line.nim

@@ -1,6 +1,7 @@

import critbits, - terminal + terminal, + queues # getch/putch implementations when defined(windows):

@@ -48,8 +49,12 @@ Line* = object

text*: string position*: int KeyCallback* = proc(ed: var LineEditor) + LineHistory* = object + position*: int + queue*: Queue[string] + max*: int LineEditor* = object - history: seq[string] + history: LineHistory line*: Line mode*: LineEditorMode

@@ -135,9 +140,29 @@ ed.line.position += 1

ed.back(rest.len) else: putchar(c.cint) - ed.line.text &= c.chr + ed.line.text[ed.line.position] = c.chr ed.line.position += 1 +proc initHistory(size = 256): LineHistory = + result.queue = initQueue[string](size) + result.position = 0 + result.max = size + +proc add(h: var LineHistory, s: string) = + if s == "": + return + if h.queue.len >= h.max: + discard h.queue.dequeue + h.queue.enqueue s + +proc addToHistory(ed: var LineEditor) = + ed.history.add ed.line.text + +proc initEditor*(mode = mdInsert, historySize = 256): LineEditor = + result.mode =mode + result.history = initHistory(historySize) + + # Character sets const CTRL* = {0 .. 31}

@@ -201,7 +226,7 @@ KEYSEQS["right"] = @[27, 91, 67]

KEYSEQS["left"] = @[27, 91, 68] KEYSEQS["insert"] = @[27, 91, 50, 126] KEYSEQS["delete"] = @[27, 91, 51, 126] - + proc readLine*(ed: var LineEditor, prompt=""): string = termSetup()

@@ -211,6 +236,7 @@ while true:

let c1 = getchar() if c1 in {10, 13}: termRestore(TERMSETTINGS) + ed.addToHistory() return ed.line.text elif c1 in {8, 127}: KEYMAP["backspace"](ed)

@@ -250,6 +276,8 @@ KEYMAP["delete"](ed)

elif KEYMAP.hasKey(KEYNAMES[c1]): KEYMAP[KEYNAMES[c1]](ed) + when isMainModule: - var ed = LineEditor(mode: mdInsert, history: newSeq[string](0)) - echo "\n---", ed.readLine("-> "), "---" + var ed = initEditor() + while true: + echo "\n---", ed.readLine("-> "), "---"