commit b3380f04cb5ee8a0bbe81fb4915ccceabd6caa92
parent bb25e47cb3f791e6bf14199fbcdd3b1a18cf0997
Author: Skylar Hill <stellarskylark@posteo.net>
Date: Sat, 4 Jun 2022 23:59:27 -0500
Add write command and simple value increment/decrement
Diffstat:
4 files changed, 56 insertions(+), 9 deletions(-)
diff --git a/rpgsheet.nimble b/rpgsheet.nimble
@@ -1,6 +1,6 @@
# Package
-version = "0.2.1"
+version = "0.2.2"
author = "Skylar Hill"
description = "CLI/TUI application for TTRPG character sheets"
license = "GPL-3.0-only"
diff --git a/src/rpgsheet.nim b/src/rpgsheet.nim
@@ -55,7 +55,7 @@ proc main(): void =
let sheet = loadSheet(sheetLocation)
if verb == Tui:
- runTui(sheet)
+ runTui(sheet, sheetLocation)
return
echo doVerb(action, verb, sheet)
diff --git a/src/tui.nim b/src/tui.nim
@@ -104,19 +104,26 @@ proc exitProc(message = "") {.noconv.} =
showCursor()
quit(message, 0)
-proc command(cmdline: string, sheet: YamlDocument): string =
+proc command(cmdline: string, sheet: YamlDocument, file: string): string =
if cmdline == "":
return ""
let tokens = cmdline.split(" ")
var cmd = tokens[0]
case cmd
- of "roll":
+ of "r", "roll":
let action = tokens[1..tokens.high].join(" ").deFancy
result = action.fancyDisplay(-1) & ": " & action.doVerb(Roll, sheet)
+ of "w", "write":
+ if tokens.len == 2:
+ sheet.write(tokens[2])
+ else:
+ sheet.write(file)
+ of "q", "quit":
+ exitProc()
else:
result = "command not recognized"
-proc runTui*(sheet: YamlDocument): void =
+proc runTui*(sheet: YamlDocument, file: string): void =
let tabs = sheet.root["tabs"]
var tabIndex = 0
@@ -181,7 +188,7 @@ proc runTui*(sheet: YamlDocument): void =
statusText = ""
of Key.Enter:
mode = Normal
- statusText = command(commandText, sheet)
+ statusText = command(commandText, sheet, file)
of Key.Backspace:
if commandText.len > 0:
commandText = commandText[0..^2]
@@ -200,10 +207,10 @@ proc runTui*(sheet: YamlDocument): void =
tabIndex = tabIndex.loopDec(tabs.elems)
selWinIndex = 0
of Key.L, Key.Right:
- selWinIndex = selwinIndex.loopInc(tabWindows)
+ selWinIndex = selWinIndex.loopInc(tabWindows)
descScroll = 0
of Key.H, Key.Left:
- selWinIndex = selwinIndex.loopDec(tabWindows)
+ selWinIndex = selWinIndex.loopDec(tabWindows)
descScroll = 0
of Key.J, Key.Down:
selectedByWindow[win] = selectedByWindow[win].loopInc(itemsByWindow[win])
@@ -225,6 +232,46 @@ proc runTui*(sheet: YamlDocument): void =
mode = Command
statusText = ":"
commandText = ""
+ of Key.Equals:
+ let sel = itemsByWindow[win][selectedByWindow[win]]
+ let node = sheet.getExpression(sel)
+ try:
+ let val = node.getContent("modifier").parseInt
+ for key, content in node.fields.pairs:
+ if key.content == "modifier":
+ node[key] = newYamlNode($(val + 1))
+ except ValueError:
+ statusText = sel & " is not an integer value."
+ of Key.Plus:
+ let sel = itemsByWindow[win][selectedByWindow[win]]
+ let node = sheet.getExpression(sel)
+ try:
+ let val = node.getContent("modifier").parseInt
+ for key, content in node.fields.pairs:
+ if key.content == "modifier":
+ node[key] = newYamlNode($(val + 10))
+ except ValueError:
+ statusText = sel & " is not an integer value."
+ of Key.Minus:
+ let sel = itemsByWindow[win][selectedByWindow[win]]
+ let node = sheet.getExpression(sel)
+ try:
+ let val = node.getContent("modifier").parseInt
+ for key, content in node.fields.pairs:
+ if key.content == "modifier":
+ node[key] = newYamlNode($(val - 1))
+ except ValueError:
+ statusText = sel & " is not an integer value."
+ of Key.Underscore:
+ let sel = itemsByWindow[win][selectedByWindow[win]]
+ let node = sheet.getExpression(sel)
+ try:
+ let val = node.getContent("modifier").parseInt
+ for key, content in node.fields.pairs:
+ if key.content == "modifier":
+ node[key] = newYamlNode($(val - 10))
+ except ValueError:
+ statusText = sel & " is not an integer value."
else: discard
# Write title
diff --git a/src/verb.nim b/src/verb.nim
@@ -84,7 +84,7 @@ proc view(action: string, sheet: YamlDocument): string =
echo action & ":"
echo expressionAsString(node)
-proc write(doc: YamlDocument, file: string) =
+proc write*(doc: YamlDocument, file: string) =
let s = newfileStream(file, fmWrite)
dumpDom(doc, s, options = defineOptions(style = psBlockOnly))
s.close()