commit 43b2530e497cc5fe277b51113a925821d0ef349f
parent 958ad6d6e33d46e73606104346d9552e1cd6c26f
Author: Skylar Hill <stellarskylark@posteo.net>
Date: Fri, 3 Jun 2022 19:30:31 -0500
Add roll command, extend capability of roll verb
Diffstat:
4 files changed, 40 insertions(+), 7 deletions(-)
diff --git a/rpgsheet.nimble b/rpgsheet.nimble
@@ -1,6 +1,6 @@
# Package
-version = "0.1.2"
+version = "0.1.3"
author = "Skylar Hill"
description = "CLI/TUI application for TTRPG character sheets"
license = "GPL-3.0-only"
diff --git a/src/tui.nim b/src/tui.nim
@@ -71,6 +71,19 @@ proc toAscii(key: Key): string =
of Key.ShiftY: "Y"
of Key.ShiftZ: "Z"
of Key.Space: " "
+ of Key.One: "1"
+ of Key.Two: "2"
+ of Key.Three: "3"
+ of Key.Four: "4"
+ of Key.Five: "5"
+ of Key.Six: "6"
+ of Key.Seven: "7"
+ of Key.Eight: "8"
+ of Key.Nine: "9"
+ of Key.Zero: "0"
+ of Key.Plus: "+"
+ of Key.Minus: "-"
+ of Key.Asterisk: "*"
else: ""
proc placeWindows(tb: TerminalBuffer, windows: seq[YamlNode], descY: int): Table[string, (int, int, int, int)] =
@@ -91,6 +104,16 @@ proc exitProc() {.noconv.} =
showCursor()
quit(0)
+proc command(cmdline: string, sheet: YamlDocument): string =
+ let tokens = cmdline.split(" ")
+ var cmd = tokens[0]
+ case cmd
+ of "roll":
+ let action = tokens[1..tokens.high].join(" ").deFancy
+ result = action.fancyDisplay(-1) & ": " & action.doVerb(Roll, sheet)
+ else:
+ result = "command not recognized"
+
proc runTui*(sheet: YamlDocument): void =
let tabs = sheet.root["tabs"]
var tabIndex = 0
@@ -103,6 +126,7 @@ proc runTui*(sheet: YamlDocument): void =
var selectedByWindow = initTable[string, int]()
var itemsByWindow = initTable[string, seq[string]]()
var statusText = ""
+ var commandText = ""
var descScroll = 0
var mode = Normal
@@ -142,16 +166,21 @@ proc runTui*(sheet: YamlDocument): void =
let win = tabWindows[selWinIndex].content
var key = getKey()
- if mode == Command:
+ case mode
+ of Command:
case key
of Key.Escape:
mode = Normal
statusText = ""
of Key.Enter:
mode = Normal
- of Key.Backspace: statusText = statusText[0..^2]
+ statusText = command(commandText, sheet)
+ of Key.Backspace:
+ if commandText.len > 0:
+ commandText = commandText[0..^2]
else:
- statusText &= key.toAscii()
+ commandText &= key.toAscii()
+ statusText = ":" & commandText
else:
case key
of Key.Q, Key.Escape: exitProc()
@@ -176,7 +205,7 @@ proc runTui*(sheet: YamlDocument): void =
of Key.Enter:
let itemList = itemsByWindow[win]
let action = itemList[selectedByWindow[win]]
- statusText = fmt"{fancyDisplay(action, 99)}: {doVerb(action, Roll, sheet)}"
+ statusText = fmt"{fancyDisplay(action, -1)}: {doVerb(action, Roll, sheet)}"
of Key.RightBracket, Key.PageDown:
descScroll += 1
of Key.LeftBracket, Key.PageUp:
@@ -186,6 +215,7 @@ proc runTui*(sheet: YamlDocument): void =
of Key.Colon:
mode = Command
statusText = ":"
+ commandText = ""
else: discard
# Write title
@@ -206,7 +236,7 @@ proc runTui*(sheet: YamlDocument): void =
tabX + tabWidth, 2,
doubleStyle = true
)
- tb.write(tabX + 2, 1, tabName.fancyDisplay(99))
+ tb.write(tabX + 2, 1, tabName.fancyDisplay(-1))
# Write status line
let statusX = tabX + tabWidth + 1
diff --git a/src/utils.nim b/src/utils.nim
@@ -32,6 +32,9 @@ proc fancyDisplay*(exp: string, maxLen: int): string =
for word in result.split(" "):
if word notin noCap:
result = result.replace(word, word.capitalizeAscii)
+
+proc deFancy*(display: string): string =
+ result = display.replace(" ", "-").toLowerAscii()
proc contains*(node: YamlNode, search: string): bool =
for key in keys(node.fields):
diff --git a/src/verb.nim b/src/verb.nim
@@ -10,7 +10,7 @@ import utils
let reRawExp = re"[a-zA-Z\-']+[a-zA-Z']"
let reProcessedExp = re"^[\d*+\-/()]*$"
-let reDice = re"^\d*d\d+$"
+let reDice = re"(\d+x)?(\d*d[\d%]+)(\*\d+)?([+-]\d+)?(s\d+)?"
type
Verb* = enum