rpgsheet

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit aa428175a7d41c688ede7a1151228b6b54e7d0d0
parent 2bf8d856d1a4e35be7501b68a0c71521f30b64ba
Author: Skylar Hill <stellarskylark@posteo.net>
Date:   Sun,  5 Jun 2022 02:41:08 -0500

Add help menu, fix write command

Diffstat:
Msrc/tui.nim | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------
1 file changed, 59 insertions(+), 18 deletions(-)

diff --git a/src/tui.nim b/src/tui.nim @@ -13,7 +13,8 @@ import utils, verb type Mode = enum Normal, - Command + Command, + Help proc toAscii(key: Key): string = result = @@ -81,14 +82,43 @@ proc toAscii(key: Key): string = of Key.Eight: "8" of Key.Nine: "9" of Key.Zero: "0" - of Key.Plus: "+" - of Key.Minus: "-" - of Key.Asterisk: "*" + of Key.Plus: "+" + of Key.Minus: "-" + of Key.Asterisk: "*" of Key.Underscore: "_" of Key.Tilde: "~" of Key.SingleQuote: "'" + of Key.Dot: "." else: "" +proc drawHelp(): void = + var tb = newTerminalBuffer(terminalWidth(), terminalHeight()) + var bb = newBoxBuffer(tb.width, tb.height) + bb.drawRect(1, 0, tb.width - 1, tb.height - 1) + tb.write(bb) + tb.write(2, 1, "Press Q to exit help.") + tb.write(2, 2, "") + tb.write(2, 3, "h Select window to the left") + tb.write(2, 4, "l Select window to the right") + tb.write(2, 5, "j Select next item in window") + tb.write(2, 6, "k Select previous item in window") + tb.write(2, 7, "Enter Roll selected item") + tb.write(2, 8, "] Scroll description forward") + tb.write(2, 9, "[ Scroll description backward") + tb.write(2, 10, "= Increase item value by 1 (if hardcoded integer)") + tb.write(2, 11, "- Decrease item value by 1 (if hardcoded integer)") + tb.write(2, 12, "+ Increase item value by 10 (if hardcoded integer)") + tb.write(2, 13, "_ Decrease item value by 10 (if hardcoded integer)") + tb.write(2, 14, "J/Tab Switch to next tab") + tb.write(2, 15, "K Switch to previous tab") + tb.write(2, 16, ": Run a command") + tb.write(2, 17, " :r[oll] <expression>") + tb.write(2, 18, " Roll a dice expression or item in your character sheet.") + tb.write(2, 19, " :w[rite] [optional-path]") + tb.write(2, 20, " Overwrite the current character sheet, or write to the given path.") + tb.display() + sleep(20) + proc placeWindows(tb: TerminalBuffer, windows: seq[YamlNode], descY: int): Table[string, (int, int, int, int)] = result = initTable[string, (int, int, int, int)]() let windowWidth = @@ -118,7 +148,7 @@ proc command(cmdline: string, sheet: YamlDocument, file: string): string = result = action.fancyDisplay(-1) & ": " & action.doVerb(Roll, sheet) of "w", "write": if tokens.len == 2: - sheet.write(tokens[2]) + sheet.write(tokens[1]) else: sheet.write(file) of "q", "quit": @@ -141,6 +171,7 @@ proc runTui*(sheet: YamlDocument, file: string): void = var commandText = "" var descScroll = 0 var mode = Normal + var sleepTime = 20 # Initialize tabs, windows, and expressions for tab in tabs: @@ -166,12 +197,6 @@ proc runTui*(sheet: YamlDocument, file: string): void = itemsbyWindow[win] = itemsByWindow[win].sorted while true: - var tb = newTerminalBuffer(terminalWidth(), terminalHeight()) - var bb = newBoxBuffer(tb.width, tb.height) - var sbb = newBoxBuffer(tb.width, tb.height) - - let descY = tb.height - 10 - var tabName: string var tabWindows: seq[YamlNode] # There should only be one item, but because @@ -180,12 +205,15 @@ proc runTui*(sheet: YamlDocument, file: string): void = for n, w in tabs[tabIndex].fields.pairs: tabName = n.content tabWindows = w.elems - - let windowPositions = placeWindows(tb, tabWindows, descY) let win = tabWindows[selWinIndex].content var key = getKey() case mode + of Help: + drawHelp() + if key in [Key.Q, Key.Escape]: + mode = Normal + continue of Command: case key of Key.Escape: @@ -194,6 +222,7 @@ proc runTui*(sheet: YamlDocument, file: string): void = of Key.Enter: mode = Normal statusText = command(commandText, sheet, file) + sleepTime = 20 # better performance of Key.Backspace: if commandText.len > 0: commandText = commandText[0..^2] @@ -242,10 +271,6 @@ proc runTui*(sheet: YamlDocument, file: string): void = descScroll = if descScroll == 0: 0 else: descScroll - 1 - of Key.Colon: - mode = Command - statusText = ":" - commandText = "" of Key.Equals: let sel = itemsByWindow[win][selectedByWindow[win]] let node = sheet.getExpression(sel) @@ -286,8 +311,24 @@ proc runTui*(sheet: YamlDocument, file: string): void = node[key] = newYamlNode($(val - 10)) except ValueError: statusText = sel & " is not an integer value." + of Key.Colon: + mode = Command + statusText = ":" + commandText = "" + sleepTime = 0 # smoother typing + of Key.QuestionMark: + mode = Help else: discard + # Prepare for drawing + var tb = newTerminalBuffer(terminalWidth(), terminalHeight()) + var bb = newBoxBuffer(tb.width, tb.height) + var sbb = newBoxBuffer(tb.width, tb.height) + + let descY = tb.height - 10 + let windowPositions = placeWindows(tb, tabWindows, descY) + + # Write title let title = fmt"""{sheet.root.getContent("name")}, {sheet.root.getContent("class")} {sheet.root.getContent("level")}""" let titleWidth = title.len + 3 @@ -390,4 +431,4 @@ proc runTui*(sheet: YamlDocument, file: string): void = tb.write(sbb) tb.display() - sleep(20) + sleep(sleepTime)