all repos — minline @ 67322609a54274a0028de0e38cb368f0579dfe54

A minimalist but highly-customizable line editing library.

Experimenting on overflow
h3rald h3rald@h3rald.com
Mon, 15 Feb 2021 01:57:35 +0000
commit

67322609a54274a0028de0e38cb368f0579dfe54

parent

0dca43255ab520a485a054b8c37ad1ce795252bf

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

jump to
M nimline.nimnimline.nim

@@ -33,6 +33,7 @@ critbits,

terminal, deques, sequtils, + math, strutils, std/exitprocs, os

@@ -115,6 +116,24 @@

proc line*(ed: var LineEditor): var Line = # TODO: Docs return ed.lines[ed.currentLine] + +proc overflow(ed: var LineEditor, l: int): int = + let pparts = ed.prompt.split("\n") + let lprompt = pparts[pparts.len-1] + return terminalWidth() - ed.lines[l].text.len - lprompt.len + +proc overlines(ed: var LineEditor, l: int, down = false): int = + let pparts = ed.prompt.split("\n") + let lprompt = pparts[pparts.len-1] + var pos: int + if down: + pos = lprompt.len + ed.lines[l].position + else: + pos = ed.lines[l].text.len + lprompt.len - ed.lines[l].position + return (pos / terminalWidth()).ceil.int + +proc overflowing(ed: var LineEditor): bool = + return ed.line.position + ed.prompt.len > terminalWidth() proc goToStart*(ed: var LineEditor) = ## Move the cursor to the beginning of the line.

@@ -151,16 +170,17 @@ ed.back(max(0, nn-2))

stdout.cursorBackward(nn) ed.line.position = ed.line.position - nn -proc up*(ed: var LineEditor, n=1) = +proc up*(ed: var LineEditor) = ## TODO docs let maxLines = ed.currentLine if maxLines <= 0: return - var nn = min(n, maxLines) + let nn = min(1, maxLines) + let nl = 1 + ed.overlines(ed.currentLine, false) let pos = ed.line.position ed.currentLine = ed.currentLine - nn ed.line.position = pos - stdout.cursorUp(nn) + stdout.cursorUp(nl) let pdiff = pos - ed.line.text.len; if pdiff > 0: ed.back(pdiff)

@@ -182,16 +202,19 @@ ed.forward(max(0, nn-2))

stdout.cursorForward(nn) ed.line.position += nn -proc down*(ed: var LineEditor, n=1) = +proc down*(ed: var LineEditor) = ## TODO docs let maxLines = ed.lines.len - 1 - ed.currentLine if maxLines <= 0: return - var nn = min(n, maxLines) + let nn = min(1, maxLines) + let nl = 1 + ed.overlines(ed.currentLine, true) + if nn > maxLines: + return let pos = ed.line.position ed.currentLine = ed.currentLine + nn ed.line.position = pos - stdout.cursorDown(nn) + stdout.cursorDown(nl) let pdiff = pos - ed.line.text.len; if pdiff > 0: ed.back(pdiff)

@@ -810,5 +833,5 @@ let text = ed.text

ed.lines = newSeq[Line](0) return text while true: - echo ed.readLine("-> ") + echo ed.readLine("---> ")