Create printing search and pipe to fzf for selection

This commit is contained in:
The Magician 2024-05-27 17:45:22 +01:00
parent 2fd868ee9c
commit 33001f7421
8 changed files with 83 additions and 75 deletions

View File

@ -2,19 +2,40 @@ package main
import ( import (
"fmt" "fmt"
"io"
"log" "log"
"os"
"os/exec"
"sevenkeys/database" "sevenkeys/database"
"sevenkeys/database/operations" "sevenkeys/database/operations"
) )
func main() { func check(err error) {
db := database.GetDatabaseFromConfig("config.json")
allCardNames, err := operations.GetAllCardNames(db)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
}
for _, name := range allCardNames {
fmt.Println(name) func main() {
} db := database.GetDatabaseFromConfig("config.json")
cardSearchOptions, err := operations.GetCardSearchOptions(db)
check(err)
cmd := exec.Command("fzf")
cmd.Stderr = os.Stderr
fzfStdin, err := cmd.StdinPipe()
check(err)
go func() {
defer fzfStdin.Close()
for _, option := range cardSearchOptions {
io.WriteString(fzfStdin, option+"\n")
}
}()
fzfOutput, err := cmd.Output()
check(err)
fmt.Println("Output:", string(fzfOutput))
} }

View File

@ -1,8 +1,14 @@
package entities package entities
type CardPrinting struct { type CardPrinting struct {
Id int Id string
GamepieceId int Name string
SetId string SetCode string
ImageUrl string HasFoil bool
HasNonFoil bool
IsReserved bool
IsRacist bool
IsPromo bool
CollectorNumber string
Language string
} }

View File

@ -3,6 +3,7 @@ package entities
type CardStorageLocation struct { type CardStorageLocation struct {
Id int Id int
CardPrintingId string CardPrintingId string
IsFoil bool
StorageBox string StorageBox string
Source string Source string
Position int Position int

View File

@ -66,7 +66,7 @@ func InsertCard(db *sql.DB, card types.Card) error {
return nil return nil
} }
func InsertCardStorageLocation(db *sql.DB, cardPrintingId string, storageBox string, source string) error { func InsertCardStorageLocation(db *sql.DB, cardPrintingId string, isFoil bool, storageBox string, source string) error {
var lastPosition int var lastPosition int
getLastPositionQuery := `SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;` getLastPositionQuery := `SELECT Position FROM CardStorageLocation WHERE StorageBox = ? ORDER BY Position DESC LIMIT 1;`
err := db.QueryRow(getLastPositionQuery, storageBox).Scan(&lastPosition) err := db.QueryRow(getLastPositionQuery, storageBox).Scan(&lastPosition)
@ -79,14 +79,14 @@ func InsertCardStorageLocation(db *sql.DB, cardPrintingId string, storageBox str
} }
insertQuery := `INSERT INTO CardStorageLocation insertQuery := `INSERT INTO CardStorageLocation
(CardPrintingId, StorageBox, Source, Position) (CardPrintingId, IsFoil, StorageBox, Source, Position)
VALUES (?, ?, ?, ?);` VALUES (?, ?, ?, ?, ?);`
insert, err := db.Prepare(insertQuery) insert, err := db.Prepare(insertQuery)
if err != nil { if err != nil {
return err return err
} }
_, err = insert.Exec(cardPrintingId, storageBox, source, nextPosition) _, err = insert.Exec(cardPrintingId, isFoil, storageBox, source, nextPosition)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,7 +2,9 @@ package operations
import ( import (
"database/sql" "database/sql"
"fmt"
"sevenkeys/database/entities" "sevenkeys/database/entities"
"strings"
"time" "time"
) )
@ -33,24 +35,52 @@ func GetCacheTimestampByType(db *sql.DB, cacheType string) (time.Time, error) {
return stamp, err return stamp, err
} }
func GetAllCardNames(db *sql.DB) ([]string, error) { func GetCardSearchOptions(db *sql.DB) ([]string, error) {
var cardNames []string var searchOptions []string
query := "SELECT Name FROM CardPrinting;"
query := "SELECT Id, Name, SetCode, HasFoil, HasNonFoil, CollectorNumber, Language FROM CardPrinting;"
rows, err := db.Query(query) rows, err := db.Query(query)
defer rows.Close() defer rows.Close()
if err != nil { if err != nil {
return cardNames, err return searchOptions, err
} }
var cardName string var printing entities.CardPrinting
for rows.Next() { for rows.Next() {
err := rows.Scan(&cardName) err := rows.Scan(&printing.Id,
&printing.Name,
&printing.SetCode,
&printing.HasFoil,
&printing.HasNonFoil,
&printing.CollectorNumber,
&printing.Language)
if err != nil { if err != nil {
return cardNames, err return searchOptions, err
} }
cardNames = append(cardNames, cardName) // TODO: Make this configurable to be able to handle non-English cards
if printing.Language != "en" {
continue
}
if printing.HasNonFoil {
searchOption := fmt.Sprintf("%s | %s (%s %s)",
printing.Id,
printing.Name,
strings.ToUpper(printing.SetCode),
printing.CollectorNumber)
searchOptions = append(searchOptions, searchOption)
}
if printing.HasFoil {
searchOption := fmt.Sprintf("%s | %s (%s %s) FOIL",
printing.Id,
printing.Name,
strings.ToUpper(printing.SetCode),
printing.CollectorNumber)
searchOptions = append(searchOptions, searchOption)
}
} }
return cardNames, nil return searchOptions, nil
} }

Binary file not shown.

View File

@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS CardStorageLocation (
Id INT AUTO_INCREMENT PRIMARY KEY, Id INT AUTO_INCREMENT PRIMARY KEY,
CardPrintingId VARCHAR(36) NOT NULL, CardPrintingId VARCHAR(36) NOT NULL,
FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id), FOREIGN KEY (CardPrintingId) REFERENCES CardPrinting(Id),
IsFoil BOOLEAN NOT NULL,
StorageBox VARCHAR(20) NOT NULL, StorageBox VARCHAR(20) NOT NULL,
Source VARCHAR(100) NULL, Source VARCHAR(100) NULL,
Position INT NOT NULL Position INT NOT NULL

View File

@ -1,51 +0,0 @@
#!/bin/bash
printLastInsertedCard() {
printf "Last inserted card: "
if test -n "$1"; then
printf "$1"
else
printf "None"
fi
printf "\n"
}
searchForCard() {
# TODO: Add keybinding to show card image with feh
./printinglist | fzf
}
declare g_lastInsertedCardId
declare g_lastInsertedCardName
while true; do
clear
printLastInsertedCard "$g_lastInsertedCardName"
printf "search? (y/N)"
read response
if [[ "$response" == "y" ]]; then
selection="$(searchForCard)"
case "$?" in
0)
# Insert the card
printf "$selection\n"
# TODO: set last inserted card name and id
;;
1)
# Print "no match" error
;;
2)
# Print error
;;
130)
# User cancelled search
;;
esac
else
break
fi
done