From f4441b77b410062d4b43360c3b486a5a2567f47f Mon Sep 17 00:00:00 2001 From: Productive2 <48047721+Productive2@users.noreply.github.com> Date: Thu, 18 Feb 2021 22:25:13 +0100 Subject: [PATCH] bugfixes to make editor work now --- examples/editor.nim | 2 +- examples/test.text | 5 ----- multiline.nim | 14 +++++++++++--- 3 files changed, 12 insertions(+), 9 deletions(-) delete mode 100644 examples/test.text diff --git a/examples/editor.nim b/examples/editor.nim index 1e739df..2846532 100644 --- a/examples/editor.nim +++ b/examples/editor.nim @@ -13,7 +13,7 @@ let e = newLineEditor() if paramCount() > 0: let arg = paramStr(1) if fileExists(arg): - e.content = readFile(arg).deserialize(replaceBS = false) + e.content = readFile(arg).fromString() e.bindKey("ctrl+s"): e.finish() e.prompt = "" diff --git a/examples/test.text b/examples/test.text deleted file mode 100644 index 067e364..0000000 --- a/examples/test.text +++ /dev/null @@ -1,5 +0,0 @@ - -good -Hello there - -heee \ No newline at end of file diff --git a/multiline.nim b/multiline.nim index 40433f7..6906f6a 100644 --- a/multiline.nim +++ b/multiline.nim @@ -19,10 +19,11 @@ proc Y*(ml: Multiline): int = # constructor -proc newMultiline*: Multiline = +proc newMultiline*(initEmpty: bool = true): Multiline = new(result) result.lines = @[] - result.lines.add(newLine()) + if initEmpty: + result.lines.add(newLine()) result.x = 0 result.y = 0 @@ -148,6 +149,8 @@ proc getLine*(ml: Multiline, line: int = -1): string = else: "" +# without the extra args these work together to convert a multiline to a single +# line string. With extra args customizable proc serialize*(ml: Multiline, sep: string = r"\n", replaceBS: bool = true): string = # replaceBS = replace backslash for line in ml.lines: @@ -158,7 +161,7 @@ proc serialize*(ml: Multiline, sep: string = r"\n", replaceBS: bool = true): str result[0..result.high() - sep.len()] proc deserialize*(str: string, sep: string = r"\n", replaceBS: bool = true): Multiline = - result = newMultiline() + result = newMultiline(initEmpty = false) for line in str.split(sep): if replaceBS: result.lines.add(newLine(line.replace(r"\\", r"\"))) @@ -168,6 +171,11 @@ proc deserialize*(str: string, sep: string = r"\n", replaceBS: bool = true): Mul result.y = result.high() result.x = result.lineLen() +proc fromString*(str: string): Multiline = + # simple load of string to multiline + deserialize(str, sep = "\n", replaceBS = false) + proc getContent*(ml: Multiline): string = + # simple convert of multiline to string ml.serialize(sep = "\n", replaceBS = false)