package logic import ( "database/sql" "fmt" "io" "os" "os/exec" "sevenkeys/database" "strings" ) type Triadic int const ( True Triadic = iota False Either ) type SearchCriteria struct { SetCode string Foil Triadic Promo Triadic Language 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 GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearchOptions, error) { var searchOptions InsertSearchOptions = make(InsertSearchOptions) cardPrintings, err := database.GetAllCardPrintings(db) if err != nil { return searchOptions, err } for _, printing := range cardPrintings { // Filter based on search criteria filter := filterPrinting(printing, searchCriteria) if filter { continue } // Construct search option string searchString := fmt.Sprintf("%s (%s %s) [%s]", printing.Name, printing.SetCode, printing.CollectorNumber, printing.Language) if printing.IsFoil { searchString += " FOIL" } if printing.IsPromo { searchString += " PROMO" } searchOptions[searchString] = printing.Id } return searchOptions, err } type StorageSearchOptions map[string]int func GetAllStorageSearchOptions(db *sql.DB) (StorageSearchOptions, error) { var searchOptions StorageSearchOptions = make(StorageSearchOptions) storedCards, err := database.GetAllStoredCards(db) if err != nil { return searchOptions, nil } for _, storedCard := range storedCards { searchString := fmt.Sprintf("%s (%s %s) [%s]", storedCard.CardName, storedCard.SetCode, storedCard.CollectorNumber, storedCard.Language) if storedCard.IsFoil { searchString += " FOIL" } if storedCard.IsPromo { searchString += " PROMO" } searchOptions[searchString] = storedCard.CardLocationId } return searchOptions, nil } func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) { var value pk cmd := exec.Command("fzf") cmd.Stderr = os.Stderr fzfStdin, err := cmd.StdinPipe() if err != nil { return value, "", err } go func() { defer fzfStdin.Close() for option, _ := range options { io.WriteString(fzfStdin, option+"\n") } }() fzfOutput, err := cmd.Output() if err != nil { return value, "", err } searchResult := strings.TrimSuffix(string(fzfOutput), "\n") value = options[searchResult] return value, searchResult, nil }