TheMathemagicians/sevenkeys/cli/mainui.go

111 lines
2.5 KiB
Go
Raw Normal View History

2024-06-10 15:34:16 +00:00
package cli
import (
2024-06-11 09:43:30 +00:00
"database/sql"
2024-06-12 20:33:50 +00:00
"errors"
2024-06-10 15:34:16 +00:00
"fmt"
2024-08-20 14:28:49 +00:00
"os"
2024-06-12 20:33:50 +00:00
"os/exec"
2024-08-20 11:57:36 +00:00
"sevenkeys/database"
2024-06-12 20:33:50 +00:00
"sevenkeys/logic"
2024-06-10 15:34:16 +00:00
)
var output string
2024-06-11 09:57:02 +00:00
func MainCliLoop(db *sql.DB) {
2024-06-10 15:34:16 +00:00
var command string
2024-08-20 14:28:49 +00:00
var selectedStorageArea database.StorageArea
var cardLocation database.CardLocation
2024-06-10 15:34:16 +00:00
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.Either,
Promo: logic.Either,
Language: "en",
}
var insertSearchOptions logic.InsertSearchOptions
2024-09-07 23:24:24 +00:00
// TODO: Make these do something and add the ability to modify them
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.Either,
2024-09-07 23:24:24 +00:00
Promo: logic.Either,
Language: "en",
}
2024-06-10 15:34:16 +00:00
for {
2024-06-12 20:33:50 +00:00
ShowSplashScreen()
2024-08-20 14:28:49 +00:00
showStorageInfo(os.Stdout, selectedStorageArea)
showInsertSearchCriteria(insertSearchCriteria)
showSelectedCard()
showCopiesInserted()
2024-06-12 20:33:50 +00:00
2024-06-10 15:34:16 +00:00
command = GetStringResponse("SEVENKEYS $")
2024-08-20 11:57:36 +00:00
var err error
2024-06-10 15:34:16 +00:00
switch command {
case "q", "quit":
2024-06-10 15:34:16 +00:00
return
2024-08-20 11:57:36 +00:00
case "n", "newstorage":
var storageArea database.StorageArea
storageArea.Name = GetStringResponse("Storage area name:")
storageArea.StorageType = GetStringResponse("Storage area type (Binder/Box):")
2024-08-20 11:57:36 +00:00
err = logic.CreateStorageArea(db, storageArea)
logic.Check(err)
break
2024-08-20 17:00:23 +00:00
case "a", "area":
area, err := logic.SelectStorageArea(db)
logic.Check(err)
selectedStorageArea = area
cardLocation.StorageAreaId = area.Id
break
case "c", "criteria":
insertSearchCriteria = getSearchCriteria()
2024-06-12 20:33:50 +00:00
break
case "s", "search":
if shouldRefreshSearch {
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
}
var previousCardPrintingId = cardLocation.CardPrintingId
pk, err := logic.GenericSearch(insertSearchOptions)
cardLocation.CardPrintingId = pk
2024-06-12 20:33:50 +00:00
var exitError *exec.ExitError
if errors.As(err, &exitError) {
break
}
logic.Check(err)
output = ""
if cardLocation.CardPrintingId != previousCardPrintingId {
copiesInserted = 0
}
2024-06-10 15:34:16 +00:00
break
case "i", "insert":
insertSelectedCard(db, selectedStorageArea, cardLocation)
break
case "l", "locate":
2024-09-07 23:24:24 +00:00
filename := GetStringResponse("Filename:")
cardNames, err := logic.GetCardNamesFromFile(filename)
logic.Check(err)
2024-09-07 23:24:24 +00:00
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
logic.Check(err)
for _, location := range locations {
2024-09-07 23:24:24 +00:00
fmt.Println(location)
}
fmt.Scanln()
2024-06-10 15:34:16 +00:00
break
default:
fmt.Println("Unrecognized command:", command)
break
}
}
}