gamelog/bin/cli.sh

209 lines
3.8 KiB
Bash
Raw Normal View History

2024-02-01 22:57:46 +00:00
#!/bin/bash
#
# cli.sh
# ------
#
# === Menu Driven ===
# Begin: Update or New Game or Exit
# Update: Select from recent or Quick Search
# New: Enter New Game. Return to Menu
# === CLI Driven ===
# -n New Game
# -u Update Game
db="db/games.rec"
function main_menu {
cat<<EOF
2024-02-03 17:04:05 +00:00
== Main Menu ==
2024-02-01 22:57:46 +00:00
1. [U]pdate
2. [N]ew Game
3. [E]dit Game
4. [Q]uit
EOF
read -p "> " selection;
}
function new_game () {
read -p "System: " system
read -p "Module: " module
cat<<EOF
Role:
1. [D]M
2. [P]layer
EOF
read -p "> " roleinput
cat<<EOF
Length:
1. [C]ampaign
2. [A]dventure
3. [O]neshot
EOF
read -p "> " lengthinput
read -p "Format: " format
case "$roleinput" in
"D" | "d" | 1) role="DM";;
"P" | "p" | 2) role="Player";;
"*") echo "Unknown Role!"; exit 1;;
esac
case "$lengthinput" in
"C" | "c" | 1) length="Campaign";;
"A" | "a" | 2) length="Adventure";;
"O" | "o" | 3) length="Oneshot";;
"*") echo "Unknown Length!"; exit 1;;
esac
recins $db --verbose -t Game \
-f "System" -v "$system" \
-f "Module" -v "$module" \
-f "Role" -v "$role" \
-f "Length" -v "$length" \
-f "Format" -v "$format" \
-f "Status" -v "Ongoing"
}
gamemenutmpl="{{Id}}. {{Module}} ({{System}})
"
function edit_game () {
id="$1"
shift
recsel $db -t Game -e "Id = $id" \
| awk 'BEGIN { RS="\n" } { print NR ". " $0 }'
read -p "Number/[A]bort> " selection
case $selection in
"a" | "A" | "x" | "X" | "")
edit_menu;;
*)
fieldvalue=(`recsel $db -t Game -e "Id = $id" \
| awk 'BEGIN { RS="\n"; FS=": " } NR == s { print $1 " " $2 }' s="$selection"`)
echo "${fieldvalue[0]}: ${fieldvalue[1]}"
read -p "New value: " value
recset --verbose $db -t Game -e "Id = $id" -f "${fieldvalue[0]}" -s "$value";
edit_game $id;;
esac
}
function update () {
id="$1"
shift
tmp=$(mktemp)
$EDITOR "$tmp"
Text=$(< "$tmp")
recins --verbose $db -t Update -f Game -v "$id" -f Text -v "$Text"
recsel $db -t Update -q "$Text"
main_menu
}
function recent_games_update () {
count=`recsel -t Game $db -c`
recent="$((count - 7))"
recsel $db -t Game -e "Id > ${recent}" | recfmt "${gamemenutmpl}"
read -p "Number> " id
case $id in
"" | "b" | "B" | "x" | "X" | "q" | "Q")
update_menu;;
*)
update $id;;
esac
}
function search_games_update () {
read -p "Query> " q
recsel $db -t Game -i -q "${q}" | recfmt "${gamemenutmpl}"
read -p "Number> " id
case id in
"" | "b" | "B" | "x" | "X" | "q" | "Q")
update_menu;;
*)
update $id;;
esac
}
function recent_games () {
count=`recsel -t Game $db -c`
recent="$((count - 7))"
recsel $db -t Game -e "Id > ${recent}" | recfmt "${gamemenutmpl}"
read -p "Number> " id
case $id in
"" | "b" | "B" | "x" | "X" | "q" | "Q")
edit_menu;;
*)
edit_game $id;;
esac
}
function search_games () {
read -p "Query> " q
recsel $db -t Game -i -q "${q}" | recfmt "${gamemenutmpl}"
read -p "Number> " id
case id in
"" | "b" | "B" | "x" | "X" | "q" | "Q")
edit_menu;;
*)
edit_game $id;;
esac
}
function update_menu () {
cat<<EOF
2024-02-03 17:04:05 +00:00
== Find Game To Update ==
2024-02-01 22:57:46 +00:00
1. [R]ecent Games
2. [S]earch
3. [M]ain menu
EOF
read -p "> " menu
case "$menu" in
1 | "r" | "R")
recent_games_update;;
2 | "s" | "S")
search_games_update;;
3 | "m" | "M" | "q" | "Q" | "b" | "B")
main_menu;;
*)
echo "Select a number or letter from the menu";edit_menu;;
esac
}
function edit_menu () {
cat<<EOF
2024-02-03 17:04:05 +00:00
== Edit Existing Game ==
2024-02-01 22:57:46 +00:00
1. [R]ecent Games
2. [S]earch
3. [M]ain menu
EOF
read -p "> " menu
case "$menu" in
1 | "r" | "R")
recent_games;;
2 | "s" | "S")
search_games;;
3 | "m" | "M" | "q" | "Q" | "b" | "B")
main_menu;;
*)
echo "Select a number or letter from the menu";edit_menu;;
esac
}
main_menu
case "$selection" in
1 | "u" | "U")
update_menu;;
2 | "n" | "N")
new_game;;
3 | "e" | "E")
edit_menu;;
4 | "x" | "X" | "q" | "Q")
echo "Byeeee";;
*)
echo "Select a letter or number from the menu";;
esac