fixes to history

This commit is contained in:
Art 2022-12-30 00:26:43 +01:00
parent 4b73239b55
commit e49d4799f3
Signed by: prod2
GPG Key ID: F3BB5A97A70A8DDE
2 changed files with 28 additions and 5 deletions

View File

@ -52,7 +52,7 @@ proc read*(ed: EditorState): (EditorResult, string) =
var scroll: Scroll = new(Scroll)
template moveInHistory(delta: int) =
let newHistoryIndex = min(max(ed.historyIndex, 0), ed.history.high())
let newHistoryIndex = min(max(ed.historyIndex + delta, 0), ed.history.high())
if newHistoryIndex != ed.historyIndex:
ed.textBuffer = ed.history[newHistoryIndex]
scroll.reset()
@ -69,6 +69,7 @@ proc read*(ed: EditorState): (EditorResult, string) =
case control:
of jkEnter:
if ed.textBuffer.isLineEmpty() and ed.textBuffer.isLastLine():
ed.textBuffer.stripFinalNewline()
editorResult = erEnter
break
else:
@ -112,12 +113,15 @@ proc read*(ed: EditorState): (EditorResult, string) =
ed.textBuffer.newLine()
else:
discard # not implemented
# return val, strip final newline (due to how double enter is how you enter)
let cont = ed.textBuffer.getContent()
result = (editorResult, if cont.len() == 0: "" else: cont[0..^2])
result = (editorResult, cont)
# cleanup
stdout.write("\n")
ed.termBuffer = nil
# don't add empty lines to history
if ed.history[ed.history.high()].getContent() == "":
ed.history.del(ed.history.high())

View File

@ -125,4 +125,23 @@ proc isFistLine*(buf: TextBuffer): bool =
buf.cursorY == 0
proc isLineEmpty*(buf: TextBuffer): bool =
buf.cline().len() == 0
buf.cline().len() == 0
proc stripFinalNewline*(buf: TextBuffer) =
# nothing if only 1 line
if buf.content.len() <= 1:
return
# nothing if last line not empty
if buf.content[buf.content.high()].len() > 0:
return
# adjust cursor
if buf.cursorY == buf.content.high():
dec buf.cursorY
buf.cursorX = buf.cline().len()
# pop final element
buf.content.del(buf.content.high())