all repos — minline @ fef6c6ba9fa7b37580bb2f171aea49372aa6262b

A minimalist but highly-customizable line editing library.

Implementing new procs.
h3rald h3rald@h3rald.com
Sun, 23 Jul 2023 16:37:35 +0200
commit

fef6c6ba9fa7b37580bb2f171aea49372aa6262b

parent

5c15fc6ecd6b36f3cd031be3bb3734e52da6b51c

1 files changed, 18 insertions(+), 7 deletions(-)

jump to
M nimline.nimnimline.nim

@@ -93,6 +93,7 @@ modeInsert

modeReplace Entry* = object ## An object representing text entered in a prompt, potentially including multiple lines. text: string + offset: int position: int wx: int wy: int

@@ -111,21 +112,21 @@ max: int

# Internal Methods -proc bol*(ed: Editor): int = +proc bol*(ed: var Editor): int = ## Returns the beginning of line index. return 0 -proc eol*(ed: Editor): int = +proc eol*(ed: var Editor): int = ## Returns the end of line index based on terminal width. - return terminalWidth - ed.prompt.len + return terminalWidth() - ed.prompt.len -proc boh*(ed: Editor): int = +proc boh*(ed: var Editor): int = ## Return the beginning of history index. return 0 -proc eoh*(ed: Editor): int = +proc eoh*(ed: var Editor): int = ## Returns the end of history index. - return ed.history.len-1 + return ed.history.queue.len-1 proc lines*(en: Entry): seq[string] =

@@ -134,8 +135,18 @@ return en.text.split("\n")

proc wlines*(en: Entry): seq[string] = ## Returns a sequence of all wrapped lines in an entry, taking into account terminal width. + let wlineLen = terminalWidth() - en.offset result = newSeq[string](0) - # TODO + for line in en.lines(): + if line.len <= wlineLen: + result.add(line) + else: + var rest = line + while (rest.len > wlineLen): + result.add(rest[0..wlineLen-1]) + rest = rest[wlineLen..rest.len-1] + if (rest.len > 0): + result.add(rest) # TO REVIEW: