Compare commits

..

No commits in common. "4f14040747f3833e0129b5705b971802e1562e72" and "c2ca6d284d1d56daa5f6998bc878f904b2097413" have entirely different histories.

10 changed files with 70 additions and 191 deletions

View File

@ -12,32 +12,21 @@ import (
var output string var output string
func showOutput() {
if output != "" {
fmt.Println(output)
}
}
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
// TODO: Make these do something and add the ability to modify them
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.True,
Promo: logic.Either,
Language: "en",
}
for { for {
ShowSplashScreen() ShowSplashScreen()
showStorageInfo(os.Stdout, selectedStorageArea) showStorageInfo(os.Stdout, selectedStorageArea)
showInsertSearchCriteria(insertSearchCriteria) showSearchCriteria()
showSelectedCard() showSelectedCard()
showCopiesInserted() showCopiesInserted()
@ -62,18 +51,14 @@ func MainCliLoop(db *sql.DB) {
cardLocation.StorageAreaId = area.Id cardLocation.StorageAreaId = area.Id
break break
case "c", "criteria": case "c", "criteria":
insertSearchCriteria = getSearchCriteria() getSearchCriteria()
break break
case "s", "search": case "s", "search":
if shouldRefreshSearch { getSearchOptions(db)
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
}
var previousCardPrintingId = cardLocation.CardPrintingId var previousCardPrintingId = cardLocation.CardPrintingId
pk, err := logic.GenericSearch(insertSearchOptions) cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
cardLocation.CardPrintingId = pk
var exitError *exec.ExitError var exitError *exec.ExitError
if errors.As(err, &exitError) { if errors.As(err, &exitError) {
break break
@ -89,15 +74,22 @@ func MainCliLoop(db *sql.DB) {
insertSelectedCard(db, selectedStorageArea, cardLocation) insertSelectedCard(db, selectedStorageArea, cardLocation)
break break
case "l", "locate": case "l", "locate":
filename := GetStringResponse("Filename:") cardName := GetStringResponse("Card name:")
cardNames, err := logic.GetCardNamesFromFile(filename) locations, err := logic.LocateCards(db, cardName)
logic.Check(err)
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
logic.Check(err) logic.Check(err)
for _, location := range locations { for _, location := range locations {
fmt.Println(location) 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() fmt.Scanln()

View File

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

View File

@ -1,10 +1,6 @@
package database package database
import ( import "database/sql"
"database/sql"
"github.com/jmoiron/sqlx"
)
type LocateCardResult struct { type LocateCardResult struct {
CardName string CardName string
@ -18,16 +14,11 @@ type LocateCardResult struct {
Position int Position int
} }
func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) { func GetLocateCardResultsByCardName(db *sql.DB, cardName string) ([]LocateCardResult, error) {
var results []LocateCardResult var results []LocateCardResult
query := "SELECT CardPrinting.Name, CardPrinting.SetCode, CardPrinting.IsFoil, CardPrinting.IsPromo, CardPrinting.CollectorNumber, CardPrinting.Language, StorageArea.StorageType, StorageArea.Name, CardLocation.Position FROM CardLocation JOIN CardPrinting ON CardLocation.CardPrintingId = CardPrinting.Id JOIN StorageArea ON CardLocation.StorageAreaId = StorageArea.Id WHERE CardPrinting.Name IN (?);" query := "SELECT CardPrinting.Name, CardPrinting.SetCode, CardPrinting.IsFoil, CardPrinting.IsPromo, CardPrinting.CollectorNumber, CardPrinting.Language, StorageArea.StorageType, StorageArea.Name, CardLocation.Position FROM CardLocation JOIN CardPrinting ON CardLocation.CardPrintingId = CardPrinting.Id JOIN StorageArea ON CardLocation.StorageAreaId = StorageArea.Id WHERE CardPrinting.Name = ?;"
query, args, err := sqlx.In(query, cardNames) rows, err := db.Query(query, cardName)
if err != nil {
return results, err
}
rows, err := db.Query(query, args...)
defer rows.Close() defer rows.Close()
if err != nil { if err != nil {
return results, err return results, err

View File

@ -17,7 +17,6 @@ require (
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 // indirect github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 // indirect github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect github.com/mattn/go-isatty v0.0.18 // indirect

View File

@ -20,9 +20,6 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFYky7l1pDP5Dr0BTCRkaQJUG6Jm5ehsk= github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFYky7l1pDP5Dr0BTCRkaQJUG6Jm5ehsk=
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM= github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 h1:UtyD+eBVdLYSj5/pjfSR6mtnzMgIiOVcFT024G2l4CY= github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 h1:UtyD+eBVdLYSj5/pjfSR6mtnzMgIiOVcFT024G2l4CY=
@ -34,7 +31,6 @@ github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+Ei
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=

View File

@ -1,25 +0,0 @@
package logic
import (
"bufio"
"os"
)
func GetCardNamesFromFile(filename string) ([]string, error) {
var cardNames []string
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return cardNames, err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
cardNames = append(cardNames, scanner.Text())
}
return cardNames, nil
}

View File

@ -2,67 +2,10 @@ package logic
import ( import (
"database/sql" "database/sql"
"fmt"
"sevenkeys/database" "sevenkeys/database"
) )
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable func LocateCards(db *sql.DB, cardName string) ([]database.LocateCardResult, error) {
results, err := database.GetLocateCardResultsByCardName(db, cardName)
func GetBinderLocationDescription(position int) string { return results, err
var page int = (position / SLOTS_PER_BINDER_PAGE) + 1
var pagePosition int = position % SLOTS_PER_BINDER_PAGE
var slot int
var frontOrBack string
if pagePosition <= (SLOTS_PER_BINDER_PAGE / 2) {
slot = pagePosition
frontOrBack = "front"
} else {
slot = pagePosition - (SLOTS_PER_BINDER_PAGE / 2)
frontOrBack = "back"
}
return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot)
}
func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]string, error) {
var locations []string
results, err := database.GetLocateResults(db, cardNames)
if err != nil {
return locations, err
}
// TODO: Filter by search criteria
var location string
for _, result := range results {
location = fmt.Sprintf("%s (%s %s) [%s]",
result.CardName,
result.SetCode,
result.CollectorNumber,
result.Language)
if result.IsFoil {
location += " FOIL"
}
if result.IsPromo {
location += " PROMO"
}
location += fmt.Sprintf(" in %s \"%s\"",
result.StorageAreaType,
result.StorageAreaName)
if result.StorageAreaType == "Binder" {
location += GetBinderLocationDescription(result.Position)
} else if result.StorageAreaType == "Box" {
location += fmt.Sprintf(" at position %d", result.Position)
}
locations = append(locations, location)
}
return locations, nil
} }

View File

@ -1,25 +0,0 @@
package logic
import "testing"
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForFrontSlots(t *testing.T) {
var position int = 24
var expected string = "on page 2 in front slot 6"
description := GetBinderLocationDescription(position)
if description != expected {
t.Errorf("expected %s, got %s\n", expected, description)
}
}
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForBackSlots(t *testing.T) {
var position int = 17
var expected string = "on page 1 in back slot 8"
description := GetBinderLocationDescription(position)
if description != expected {
t.Errorf("expected %s, got %s\n", expected, description)
}
}

View File

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

View File

@ -8,8 +8,8 @@ import (
func main() { func main() {
db := database.GetDatabaseFromConfig("config.json") db := database.GetDatabaseFromConfig("config.json")
figlet.ReadFigletFonts() figlet.ReadFigletFonts()
cli.ShowSplashScreen() cli.ShowSplashScreen()
cli.RunUpdateCheck(db) cli.RunUpdateCheck(db)