fixes to history
This commit is contained in:
parent
4b73239b55
commit
e49d4799f3
10
editor.nim
10
editor.nim
|
@ -52,7 +52,7 @@ proc read*(ed: EditorState): (EditorResult, string) =
|
||||||
var scroll: Scroll = new(Scroll)
|
var scroll: Scroll = new(Scroll)
|
||||||
|
|
||||||
template moveInHistory(delta: int) =
|
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:
|
if newHistoryIndex != ed.historyIndex:
|
||||||
ed.textBuffer = ed.history[newHistoryIndex]
|
ed.textBuffer = ed.history[newHistoryIndex]
|
||||||
scroll.reset()
|
scroll.reset()
|
||||||
|
@ -69,6 +69,7 @@ proc read*(ed: EditorState): (EditorResult, string) =
|
||||||
case control:
|
case control:
|
||||||
of jkEnter:
|
of jkEnter:
|
||||||
if ed.textBuffer.isLineEmpty() and ed.textBuffer.isLastLine():
|
if ed.textBuffer.isLineEmpty() and ed.textBuffer.isLastLine():
|
||||||
|
ed.textBuffer.stripFinalNewline()
|
||||||
editorResult = erEnter
|
editorResult = erEnter
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
|
@ -115,9 +116,12 @@ proc read*(ed: EditorState): (EditorResult, string) =
|
||||||
|
|
||||||
# return val, strip final newline (due to how double enter is how you enter)
|
# return val, strip final newline (due to how double enter is how you enter)
|
||||||
let cont = ed.textBuffer.getContent()
|
let cont = ed.textBuffer.getContent()
|
||||||
result = (editorResult, if cont.len() == 0: "" else: cont[0..^2])
|
result = (editorResult, cont)
|
||||||
|
|
||||||
# cleanup
|
# cleanup
|
||||||
stdout.write("\n")
|
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())
|
||||||
|
|
|
@ -126,3 +126,22 @@ proc isFistLine*(buf: TextBuffer): bool =
|
||||||
|
|
||||||
proc isLineEmpty*(buf: TextBuffer): bool =
|
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