Skip to content

Commit

Permalink
Add basic control of cursor position in text input mode
Browse files Browse the repository at this point in the history
Using the arrow keys, the cursor can be moved left or right.
  • Loading branch information
joachimschmidt557 committed Mar 25, 2024
1 parent 442d8e4 commit 4162cdc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
5 changes: 4 additions & 1 deletion src/core.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ type

ModeInfo* = object
case mode*: Mode
of MdNormal, MdSearch: discard
of MdNormal: discard
of MdSearch:
searchCursorPos*: int = 0
of MdInputText:
promptText*: string
input*: string = ""
textCursorPos*: int = 0
callbackText*: proc (input: string)
of MdInputBool:
promptBool*: string
Expand Down
16 changes: 10 additions & 6 deletions src/draw.nim
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ proc drawFooter(index: int, lenEntries: int, lenSelected: int, hidden: bool,
nb.print(offsetErrMsg, y, " " & errMsg, c8(clrRed), c8(clrBlack))
nb.cursor = (TB_HIDE_CURSOR, TB_HIDE_CURSOR)

proc drawInputFooter(prompt: string, query: string, nb: var Nimbox) =
proc drawInputFooter(prompt: string, query: string, cursorPos: int,
nb: var Nimbox) =
let
y = nb.height() - 1
offset = prompt.len + 1
offset = prompt.wcswidth + 1
cursorPos = offset + query[0..cursorPos - 1].wcswidth
nb.print(0, y, prompt, c8(clrYellow), c8(clrBlack))
nb.print(offset, y, query, c8(clrYellow), c8(clrBlack))
nb.cursor = (offset + query.wcswidth, y)
nb.cursor = (cursorPos, y)

proc errMsg(err: ErrorKind): string =
case err
Expand Down Expand Up @@ -215,10 +217,12 @@ proc redraw*(s: State, nb: var Nimbox) =
s.showHidden, s.currentSearchQuery != "",
errMsg, nb)
of MdSearch:
drawInputFooter("search:", s.currentSearchQuery, nb)
drawInputFooter("search:", s.currentSearchQuery,
s.modeInfo.searchCursorPos, nb)
of MdInputText:
drawInputFooter(s.modeInfo.promptText, s.modeInfo.input, nb)
drawInputFooter(s.modeInfo.promptText, s.modeInfo.input,
s.modeInfo.textCursorPos, nb)
of MdInputBool:
drawInputFooter(s.modeInfo.promptBool, "", nb)
drawInputFooter(s.modeInfo.promptBool, "", 0, nb)

nb.present()
31 changes: 24 additions & 7 deletions src/nimmm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,37 @@ type
PrComplete,

proc processInputTextMode(event: nimbox.Event,
input: var string): ProcessInputTextModeResult =
input: var string,
cursorPos: var int): ProcessInputTextModeResult =
## common input processing for MdInputText and MdSearch
case event.kind
of EventType.Key:
case event.sym
of Symbol.Escape:
return PrCanceled
of Symbol.Backspace:
if input.len > 0:
input.setLen(input.high)
if cursorPos > 0:
let (_, runeLen) = lastRune(input, cursorPos - 1)
input = input[0..cursorPos - 1 - runeLen] & input.substr(cursorPos)
cursorPos -= runeLen
of Symbol.Enter:
return PrComplete
of Symbol.Space:
input.add(" ")
let inserted = " "
input.insert(inserted, cursorPos)
cursorPos += inserted.len
of Symbol.Character:
input.add($event.ch.Rune)
let inserted = $event.ch.Rune
input.insert(inserted, cursorPos)
cursorPos += inserted.len
of Symbol.Left:
if cursorPos > 0:
let (_, runeLen) = lastRune(input, cursorPos - 1)
cursorPos -= runeLen
of Symbol.Right:
if cursorPos < input.len:
let runeLen = runeLenAt(input, cursorPos)
cursorPos += runeLen
else:
discard
of EventType.Mouse, EventType.Resize, EventType.None:
Expand Down Expand Up @@ -208,7 +223,8 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
discard
# Input text mode: Ignore keymap
of MdInputText:
case processInputTextMode(event, s.modeInfo.input)
case processInputTextMode(event, s.modeInfo.input,
s.modeInfo.textCursorPos)
of PrCanceled:
s.modeInfo = ModeInfo(mode: MdNormal)
of PrComplete:
Expand All @@ -220,7 +236,8 @@ proc mainLoop(nb: var Nimbox, enable256Colors: bool) =
discard
# Incremental search mode: Ignore keymap
of MdSearch:
case processInputTextMode(event, s.currentSearchQuery)
case processInputTextMode(event, s.currentSearchQuery,
s.modeInfo.searchCursorPos)
of PrCanceled:
s.resetTab()
of PrComplete:
Expand Down

0 comments on commit 4162cdc

Please sign in to comment.