ctrl+c, ctrl+d support

This commit is contained in:
Art 2022-12-28 18:12:29 +01:00
parent 26bb28ac5a
commit ddb5f375d7
Signed by: prod2
GPG Key ID: F3BB5A97A70A8DDE
2 changed files with 20 additions and 4 deletions

View File

@ -26,6 +26,9 @@ type
cols: int # number of columns in the terminal
maxRows: int # max number of rows allowed
EditorResult* = enum
erEnter, erCtrlC, erCtrlD, erError
# for editors
var editors: seq[EditorState]
@ -56,12 +59,18 @@ proc render(ed: EditorState) =
template cline(ed: EditorState): var string =
ed.buffer[ed.y]
proc read*(ed: EditorState): string =
proc read*(ed: EditorState): (EditorResult, string) =
var editorResult = erError
ed.render()
while true:
let key = getKey()
case key:
of jkEnter.int:
editorResult = erEnter
break
of jkBackspace.int:
if ed.x == 0:
@ -75,6 +84,13 @@ proc read*(ed: EditorState): string =
ed.cline() = ed.cline[0..ed.x-1] & ed.cline[ed.x+1..ed.cline().high()]
dec ed.x
dec ed.bx
of 3: # ctrl+c
editorResult = erCtrlC
break
of 4: # ctrl+d
if ed.buffer.len() <= 1 and ed.cline().len() == 0:
editorResult = erCtrlD
break
else:
if key > 31 and key < 127:
# ascii char
@ -91,7 +107,7 @@ proc read*(ed: EditorState): string =
ed.render()
# return val
result = ed.buffer.join("\n")
result = (editorResult, ed.buffer.join("\n"))
# cleanup
write(stdout, "\n")

View File

@ -3,7 +3,7 @@ import editor
let e = newEditor("> ", false)
while true:
let text = e.read()
if text == "quit":
let (res, text) = e.read()
if res in {erCtrlC, erCtrlD} or text == "quit":
break
echo text