From 3fd83a9043dfc928704104cae05e9f01376d6f90 Mon Sep 17 00:00:00 2001 From: Productive2 <48047721+Productive2@users.noreply.github.com> Date: Thu, 18 Feb 2021 21:51:44 +0100 Subject: [PATCH] some fixes --- editor.nim | 8 +++++--- examples/editor.nim | 16 ++++++++++++++-- examples/test.text | 5 +++++ multiline.nim | 7 +++++-- plugin/defaults.nim | 4 ++-- 5 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 examples/test.text diff --git a/editor.nim b/editor.nim index 772ba10..99d5181 100644 --- a/editor.nim +++ b/editor.nim @@ -39,6 +39,10 @@ proc finish*(le: LineEditor) = # can be overwritten to false, inside the event le.events.call(jeFinish) +proc quit*(le: LineEditor) = + le.finished = true + le.events.call(jeQuit) + proc forceRedraw*(le: LineEditor) = le.forceRedraw = true @@ -115,7 +119,6 @@ proc read*(editor: LineEditor): string = # starts at the top, full render moves it into the right y editor.fullRender() - while not editor.finished: # refresh current line every time @@ -143,6 +146,5 @@ proc read*(editor: LineEditor): string = # move cursor to end editor.moveCursorToEnd() + result = editor.content.getContent() editor.reset() - - return editor.content.getContent() diff --git a/examples/editor.nim b/examples/editor.nim index a4b54ba..1e739df 100644 --- a/examples/editor.nim +++ b/examples/editor.nim @@ -2,11 +2,23 @@ import ../plugin/defaults import ../editor import ../strutils import ../templates +import ../multiline import terminal +import os +eraseScreen() +setCursorPos(stdout, 0,0) let e = newLineEditor() +if paramCount() > 0: + let arg = paramStr(1) + if fileExists(arg): + e.content = readFile(arg).deserialize(replaceBS = false) +e.bindKey("ctrl+s"): + e.finish() e.prompt = "" -e.populateDefaults(enterSubmits = false) -let input = e.read() +e.populateDefaults(enterSubmits = false, shiftForVerticalMove = false) +let result = e.read() +if paramCount() > 0: + writeFile(paramStr(1), result) diff --git a/examples/test.text b/examples/test.text new file mode 100644 index 0000000..067e364 --- /dev/null +++ b/examples/test.text @@ -0,0 +1,5 @@ + +good +Hello there + +heee \ No newline at end of file diff --git a/multiline.nim b/multiline.nim index 0216ab4..40433f7 100644 --- a/multiline.nim +++ b/multiline.nim @@ -157,10 +157,13 @@ proc serialize*(ml: Multiline, sep: string = r"\n", replaceBS: bool = true): str result &= line.content & sep result[0..result.high() - sep.len()] -proc deserialize*(str: string, sep: string = r"\n"): Multiline = +proc deserialize*(str: string, sep: string = r"\n", replaceBS: bool = true): Multiline = result = newMultiline() for line in str.split(sep): - result.lines.add(newLine(line.replace(r"\\", r"\"))) + if replaceBS: + result.lines.add(newLine(line.replace(r"\\", r"\"))) + else: + result.lines.add(newLine(line)) result.y = result.high() result.x = result.lineLen() diff --git a/plugin/defaults.nim b/plugin/defaults.nim index 5fa15ea..5c69c7e 100644 --- a/plugin/defaults.nim +++ b/plugin/defaults.nim @@ -14,11 +14,11 @@ proc bindInput*(editor: LineEditor) = proc bindTerminate*(editor: LineEditor) = editor.bindKey("ctrl+c"): - editor.finish() + editor.quit() editor.bindKey("ctrl+d"): if editor.content.getContent() == "": - editor.finish() + editor.quit() proc populateDefaults*(editor: LineEditor, enterSubmits = true, shiftForVerticalMove = true) =