Replace search function with generic version
This commit is contained in:
parent
1d9c614d16
commit
0d4d171edf
|
@ -14,13 +14,22 @@ var output string
|
|||
|
||||
func MainCliLoop(db *sql.DB) {
|
||||
var command string
|
||||
|
||||
var selectedStorageArea database.StorageArea
|
||||
var cardLocation database.CardLocation
|
||||
|
||||
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
||||
SetCode: "",
|
||||
Foil: logic.Either,
|
||||
Promo: logic.Either,
|
||||
Language: "en",
|
||||
}
|
||||
var insertSearchOptions logic.InsertSearchOptions
|
||||
|
||||
for {
|
||||
ShowSplashScreen()
|
||||
showStorageInfo(os.Stdout, selectedStorageArea)
|
||||
showSearchCriteria()
|
||||
showInsertSearchCriteria(insertSearchCriteria)
|
||||
showSelectedCard()
|
||||
showCopiesInserted()
|
||||
|
||||
|
@ -45,14 +54,18 @@ func MainCliLoop(db *sql.DB) {
|
|||
cardLocation.StorageAreaId = area.Id
|
||||
break
|
||||
case "c", "criteria":
|
||||
getSearchCriteria()
|
||||
insertSearchCriteria = getSearchCriteria()
|
||||
break
|
||||
case "s", "search":
|
||||
getSearchOptions(db)
|
||||
if shouldRefreshSearch {
|
||||
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
|
||||
}
|
||||
|
||||
var previousCardPrintingId = cardLocation.CardPrintingId
|
||||
|
||||
cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
|
||||
pk, err := logic.GenericSearch(insertSearchOptions)
|
||||
cardLocation.CardPrintingId = pk
|
||||
|
||||
var exitError *exec.ExitError
|
||||
if errors.As(err, &exitError) {
|
||||
break
|
||||
|
@ -69,6 +82,7 @@ func MainCliLoop(db *sql.DB) {
|
|||
break
|
||||
case "l", "locate":
|
||||
cardName := GetStringResponse("Card name:")
|
||||
|
||||
locations, err := logic.LocateCards(db, cardName)
|
||||
logic.Check(err)
|
||||
|
||||
|
|
|
@ -6,23 +6,18 @@ import (
|
|||
"sevenkeys/logic"
|
||||
)
|
||||
|
||||
var (
|
||||
searchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
||||
SetCode: "",
|
||||
Foil: logic.Either,
|
||||
Promo: logic.Either,
|
||||
Language: "",
|
||||
}
|
||||
shouldRefreshSearch bool = true
|
||||
searchOptions logic.SearchOptions
|
||||
)
|
||||
var shouldRefreshSearch bool = true
|
||||
|
||||
func getSearchCriteria() logic.SearchCriteria {
|
||||
var searchCriteria logic.SearchCriteria
|
||||
|
||||
func getSearchCriteria() {
|
||||
searchCriteria.SetCode = GetStringResponse("Set code:")
|
||||
searchCriteria.Foil = GetTriadicResponse("Foil (y/n/E):")
|
||||
searchCriteria.Promo = GetTriadicResponse("Promo (y/n/E):")
|
||||
searchCriteria.Language = GetStringResponse("Language:")
|
||||
shouldRefreshSearch = true
|
||||
|
||||
return searchCriteria
|
||||
}
|
||||
|
||||
func getTriadicDisplay(triadic logic.Triadic) string {
|
||||
|
@ -37,13 +32,13 @@ func getTriadicDisplay(triadic logic.Triadic) string {
|
|||
return "Either"
|
||||
}
|
||||
|
||||
func showSearchCriteria() {
|
||||
func showInsertSearchCriteria(insertSearchCriteria logic.SearchCriteria) {
|
||||
fmt.Println("SEARCH CRITERIA")
|
||||
|
||||
setCodeDisplay := getInfoDisplay(searchCriteria.SetCode)
|
||||
foilDisplay := getTriadicDisplay(searchCriteria.Foil)
|
||||
promoDisplay := getTriadicDisplay(searchCriteria.Promo)
|
||||
languageDisplay := getInfoDisplay(searchCriteria.Language)
|
||||
setCodeDisplay := getInfoDisplay(insertSearchCriteria.SetCode)
|
||||
foilDisplay := getTriadicDisplay(insertSearchCriteria.Foil)
|
||||
promoDisplay := getTriadicDisplay(insertSearchCriteria.Promo)
|
||||
languageDisplay := getInfoDisplay(insertSearchCriteria.Language)
|
||||
|
||||
fmt.Println("Set code:", setCodeDisplay)
|
||||
fmt.Println("Foil:", foilDisplay)
|
||||
|
@ -58,14 +53,9 @@ func showSelectedCard() {
|
|||
fmt.Println("Selected card:", selectedCardDisplay)
|
||||
}
|
||||
|
||||
func getSearchOptions(db *sql.DB) {
|
||||
if shouldRefreshSearch {
|
||||
fmt.Println("LOADING")
|
||||
options, err := logic.GetAllSearchOptions(db, searchCriteria)
|
||||
func getSearchOptions(db *sql.DB, insertSearchCriteria logic.SearchCriteria) logic.InsertSearchOptions {
|
||||
options, err := logic.GetAllSearchOptions(db, insertSearchCriteria)
|
||||
logic.Check(err)
|
||||
searchOptions = options
|
||||
shouldRefreshSearch = false
|
||||
fmt.Println("READY")
|
||||
fmt.Println("RUN")
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
|
|
@ -25,9 +25,9 @@ type SearchCriteria struct {
|
|||
Language string
|
||||
}
|
||||
|
||||
// The SearchOptions type is a map of `string` keys (which can be searched using fzf)
|
||||
// to `string` values (which represent a primary key in the CardPrintings table)
|
||||
type SearchOptions map[string]string
|
||||
// The InsertSearchOptions type is a map of `string` keys (which can be searched using
|
||||
// fzf) to `string` values (which represent a primary key in the CardPrintings table)
|
||||
type InsertSearchOptions map[string]string
|
||||
|
||||
func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool {
|
||||
if searchCriteria.SetCode != "" && printing.SetCode != searchCriteria.SetCode {
|
||||
|
@ -55,8 +55,8 @@ func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteri
|
|||
return false
|
||||
}
|
||||
|
||||
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptions, error) {
|
||||
var searchOptions SearchOptions = make(map[string]string)
|
||||
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearchOptions, error) {
|
||||
var searchOptions InsertSearchOptions = make(map[string]string)
|
||||
|
||||
cardPrintings, err := database.GetAllCardPrintings(db)
|
||||
if err != nil {
|
||||
|
@ -87,28 +87,30 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptio
|
|||
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.Stderr = os.Stderr
|
||||
|
||||
fzfStdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
return value, err
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer fzfStdin.Close()
|
||||
for searchString, _ := range searchOptions {
|
||||
io.WriteString(fzfStdin, searchString+"\n")
|
||||
for option, _ := range options {
|
||||
io.WriteString(fzfStdin, option+"\n")
|
||||
}
|
||||
}()
|
||||
|
||||
fzfOutput, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
return value, err
|
||||
}
|
||||
|
||||
key := strings.TrimSuffix(string(fzfOutput), "\n")
|
||||
|
||||
return searchOptions[key], key, nil
|
||||
searchResult := strings.TrimSuffix(string(fzfOutput), "\n")
|
||||
value = options[searchResult]
|
||||
return value, nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue