Replace search function with generic version

This commit is contained in:
The Magician 2024-09-07 19:07:48 +01:00
parent 1d9c614d16
commit 0d4d171edf
3 changed files with 49 additions and 43 deletions

View File

@ -14,13 +14,22 @@ var output string
func MainCliLoop(db *sql.DB) { func MainCliLoop(db *sql.DB) {
var command string var command string
var selectedStorageArea database.StorageArea var selectedStorageArea database.StorageArea
var cardLocation database.CardLocation var cardLocation database.CardLocation
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.Either,
Promo: logic.Either,
Language: "en",
}
var insertSearchOptions logic.InsertSearchOptions
for { for {
ShowSplashScreen() ShowSplashScreen()
showStorageInfo(os.Stdout, selectedStorageArea) showStorageInfo(os.Stdout, selectedStorageArea)
showSearchCriteria() showInsertSearchCriteria(insertSearchCriteria)
showSelectedCard() showSelectedCard()
showCopiesInserted() showCopiesInserted()
@ -45,14 +54,18 @@ func MainCliLoop(db *sql.DB) {
cardLocation.StorageAreaId = area.Id cardLocation.StorageAreaId = area.Id
break break
case "c", "criteria": case "c", "criteria":
getSearchCriteria() insertSearchCriteria = getSearchCriteria()
break break
case "s", "search": case "s", "search":
getSearchOptions(db) if shouldRefreshSearch {
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
}
var previousCardPrintingId = cardLocation.CardPrintingId var previousCardPrintingId = cardLocation.CardPrintingId
cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions) pk, err := logic.GenericSearch(insertSearchOptions)
cardLocation.CardPrintingId = pk
var exitError *exec.ExitError var exitError *exec.ExitError
if errors.As(err, &exitError) { if errors.As(err, &exitError) {
break break
@ -69,6 +82,7 @@ func MainCliLoop(db *sql.DB) {
break break
case "l", "locate": case "l", "locate":
cardName := GetStringResponse("Card name:") cardName := GetStringResponse("Card name:")
locations, err := logic.LocateCards(db, cardName) locations, err := logic.LocateCards(db, cardName)
logic.Check(err) logic.Check(err)

View File

@ -6,23 +6,18 @@ import (
"sevenkeys/logic" "sevenkeys/logic"
) )
var ( var shouldRefreshSearch bool = true
searchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "", func getSearchCriteria() logic.SearchCriteria {
Foil: logic.Either, var searchCriteria logic.SearchCriteria
Promo: logic.Either,
Language: "",
}
shouldRefreshSearch bool = true
searchOptions logic.SearchOptions
)
func getSearchCriteria() {
searchCriteria.SetCode = GetStringResponse("Set code:") searchCriteria.SetCode = GetStringResponse("Set code:")
searchCriteria.Foil = GetTriadicResponse("Foil (y/n/E):") searchCriteria.Foil = GetTriadicResponse("Foil (y/n/E):")
searchCriteria.Promo = GetTriadicResponse("Promo (y/n/E):") searchCriteria.Promo = GetTriadicResponse("Promo (y/n/E):")
searchCriteria.Language = GetStringResponse("Language:") searchCriteria.Language = GetStringResponse("Language:")
shouldRefreshSearch = true shouldRefreshSearch = true
return searchCriteria
} }
func getTriadicDisplay(triadic logic.Triadic) string { func getTriadicDisplay(triadic logic.Triadic) string {
@ -37,13 +32,13 @@ func getTriadicDisplay(triadic logic.Triadic) string {
return "Either" return "Either"
} }
func showSearchCriteria() { func showInsertSearchCriteria(insertSearchCriteria logic.SearchCriteria) {
fmt.Println("SEARCH CRITERIA") fmt.Println("SEARCH CRITERIA")
setCodeDisplay := getInfoDisplay(searchCriteria.SetCode) setCodeDisplay := getInfoDisplay(insertSearchCriteria.SetCode)
foilDisplay := getTriadicDisplay(searchCriteria.Foil) foilDisplay := getTriadicDisplay(insertSearchCriteria.Foil)
promoDisplay := getTriadicDisplay(searchCriteria.Promo) promoDisplay := getTriadicDisplay(insertSearchCriteria.Promo)
languageDisplay := getInfoDisplay(searchCriteria.Language) languageDisplay := getInfoDisplay(insertSearchCriteria.Language)
fmt.Println("Set code:", setCodeDisplay) fmt.Println("Set code:", setCodeDisplay)
fmt.Println("Foil:", foilDisplay) fmt.Println("Foil:", foilDisplay)
@ -58,14 +53,9 @@ func showSelectedCard() {
fmt.Println("Selected card:", selectedCardDisplay) fmt.Println("Selected card:", selectedCardDisplay)
} }
func getSearchOptions(db *sql.DB) { func getSearchOptions(db *sql.DB, insertSearchCriteria logic.SearchCriteria) logic.InsertSearchOptions {
if shouldRefreshSearch { options, err := logic.GetAllSearchOptions(db, insertSearchCriteria)
fmt.Println("LOADING") logic.Check(err)
options, err := logic.GetAllSearchOptions(db, searchCriteria) shouldRefreshSearch = false
logic.Check(err) return options
searchOptions = options
shouldRefreshSearch = false
fmt.Println("READY")
fmt.Println("RUN")
}
} }

View File

@ -25,9 +25,9 @@ type SearchCriteria struct {
Language string Language string
} }
// The SearchOptions type is a map of `string` keys (which can be searched using fzf) // The InsertSearchOptions type is a map of `string` keys (which can be searched using
// to `string` values (which represent a primary key in the CardPrintings table) // fzf) to `string` values (which represent a primary key in the CardPrintings table)
type SearchOptions map[string]string type InsertSearchOptions map[string]string
func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool { func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool {
if searchCriteria.SetCode != "" && printing.SetCode != searchCriteria.SetCode { if searchCriteria.SetCode != "" && printing.SetCode != searchCriteria.SetCode {
@ -55,8 +55,8 @@ func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteri
return false return false
} }
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptions, error) { func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearchOptions, error) {
var searchOptions SearchOptions = make(map[string]string) var searchOptions InsertSearchOptions = make(map[string]string)
cardPrintings, err := database.GetAllCardPrintings(db) cardPrintings, err := database.GetAllCardPrintings(db)
if err != nil { if err != nil {
@ -87,28 +87,30 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptio
return searchOptions, err return searchOptions, err
} }
func Search(searchOptions SearchOptions) (string, string, error) { func GenericSearch[pk string | int](options map[string]pk) (pk, error) {
var value pk
cmd := exec.Command("fzf") cmd := exec.Command("fzf")
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
fzfStdin, err := cmd.StdinPipe() fzfStdin, err := cmd.StdinPipe()
if err != nil { if err != nil {
return "", "", err return value, err
} }
go func() { go func() {
defer fzfStdin.Close() defer fzfStdin.Close()
for searchString, _ := range searchOptions { for option, _ := range options {
io.WriteString(fzfStdin, searchString+"\n") io.WriteString(fzfStdin, option+"\n")
} }
}() }()
fzfOutput, err := cmd.Output() fzfOutput, err := cmd.Output()
if err != nil { if err != nil {
return "", "", err return value, err
} }
key := strings.TrimSuffix(string(fzfOutput), "\n") searchResult := strings.TrimSuffix(string(fzfOutput), "\n")
value = options[searchResult]
return searchOptions[key], key, nil return value, nil
} }