diff --git a/editor.nim b/editor.nim index 2b3aa71..a87f9aa 100644 --- a/editor.nim +++ b/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()) \ No newline at end of file diff --git a/textBuffer.nim b/textBuffer.nim index 1f4b3ef..ecd1cc7 100644 --- a/textBuffer.nim +++ b/textBuffer.nim @@ -125,4 +125,23 @@ proc isFistLine*(buf: TextBuffer): bool = buf.cursorY == 0 proc isLineEmpty*(buf: TextBuffer): bool = - buf.cline().len() == 0 \ No newline at end of file + 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()) + + \ No newline at end of file