fixes to history
This commit is contained in:
parent
4b73239b55
commit
e49d4799f3
12
editor.nim
12
editor.nim
|
@ -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())
|
||||
|
|
@ -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())
|
||||
|
||||
|
Loading…
Reference in New Issue