TheMathemagicians/sevenkeys/cli/mainui.go

103 lines
2.2 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
func showOutput() {
if output != "" {
fmt.Println(output)
}
}
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
for {
2024-06-12 20:33:50 +00:00
ShowSplashScreen()
2024-08-20 14:28:49 +00:00
showStorageInfo(os.Stdout, selectedStorageArea)
showSearchCriteria()
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":
2024-06-12 20:33:50 +00:00
getSearchCriteria()
break
case "s", "search":
2024-06-12 20:33:50 +00:00
getSearchOptions(db)
var previousCardPrintingId = cardLocation.CardPrintingId
cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
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":
cardName := GetStringResponse("Card name:")
locations, err := logic.LocateCards(db, cardName)
logic.Check(err)
for _, location := range locations {
fmt.Printf("%s (%s %s) [%s]", location.CardName, location.SetCode, location.CollectorNumber, location.Language)
if location.IsFoil {
fmt.Printf(" FOIL")
}
if location.IsPromo {
fmt.Printf(" PROMO")
}
fmt.Printf(" in %s \"%s\" at position %d\n", location.StorageAreaType, location.StorageAreaName, location.Position)
}
fmt.Scanln()
2024-06-10 15:34:16 +00:00
break
default:
fmt.Println("Unrecognized command:", command)
break
}
}
}