Compare commits
No commits in common. "4f14040747f3833e0129b5705b971802e1562e72" and "c2ca6d284d1d56daa5f6998bc878f904b2097413" have entirely different histories.
4f14040747
...
c2ca6d284d
|
@ -12,32 +12,21 @@ import (
|
|||
|
||||
var output string
|
||||
|
||||
func showOutput() {
|
||||
if output != "" {
|
||||
fmt.Println(output)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
// 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 {
|
||||
ShowSplashScreen()
|
||||
showStorageInfo(os.Stdout, selectedStorageArea)
|
||||
showInsertSearchCriteria(insertSearchCriteria)
|
||||
showSearchCriteria()
|
||||
showSelectedCard()
|
||||
showCopiesInserted()
|
||||
|
||||
|
@ -62,18 +51,14 @@ func MainCliLoop(db *sql.DB) {
|
|||
cardLocation.StorageAreaId = area.Id
|
||||
break
|
||||
case "c", "criteria":
|
||||
insertSearchCriteria = getSearchCriteria()
|
||||
getSearchCriteria()
|
||||
break
|
||||
case "s", "search":
|
||||
if shouldRefreshSearch {
|
||||
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
|
||||
}
|
||||
getSearchOptions(db)
|
||||
|
||||
var previousCardPrintingId = cardLocation.CardPrintingId
|
||||
|
||||
pk, err := logic.GenericSearch(insertSearchOptions)
|
||||
cardLocation.CardPrintingId = pk
|
||||
|
||||
cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
|
||||
var exitError *exec.ExitError
|
||||
if errors.As(err, &exitError) {
|
||||
break
|
||||
|
@ -89,15 +74,22 @@ func MainCliLoop(db *sql.DB) {
|
|||
insertSelectedCard(db, selectedStorageArea, cardLocation)
|
||||
break
|
||||
case "l", "locate":
|
||||
filename := GetStringResponse("Filename:")
|
||||
cardNames, err := logic.GetCardNamesFromFile(filename)
|
||||
logic.Check(err)
|
||||
|
||||
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
|
||||
cardName := GetStringResponse("Card name:")
|
||||
locations, err := logic.LocateCards(db, cardName)
|
||||
logic.Check(err)
|
||||
|
||||
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()
|
||||
|
||||
|
|
|
@ -6,18 +6,23 @@ import (
|
|||
"sevenkeys/logic"
|
||||
)
|
||||
|
||||
var shouldRefreshSearch bool = true
|
||||
|
||||
func getSearchCriteria() logic.SearchCriteria {
|
||||
var searchCriteria logic.SearchCriteria
|
||||
var (
|
||||
searchCriteria logic.SearchCriteria = logic.SearchCriteria{
|
||||
SetCode: "",
|
||||
Foil: logic.Either,
|
||||
Promo: logic.Either,
|
||||
Language: "",
|
||||
}
|
||||
shouldRefreshSearch bool = true
|
||||
searchOptions logic.SearchOptions
|
||||
)
|
||||
|
||||
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 {
|
||||
|
@ -32,13 +37,13 @@ func getTriadicDisplay(triadic logic.Triadic) string {
|
|||
return "Either"
|
||||
}
|
||||
|
||||
func showInsertSearchCriteria(insertSearchCriteria logic.SearchCriteria) {
|
||||
func showSearchCriteria() {
|
||||
fmt.Println("SEARCH CRITERIA")
|
||||
|
||||
setCodeDisplay := getInfoDisplay(insertSearchCriteria.SetCode)
|
||||
foilDisplay := getTriadicDisplay(insertSearchCriteria.Foil)
|
||||
promoDisplay := getTriadicDisplay(insertSearchCriteria.Promo)
|
||||
languageDisplay := getInfoDisplay(insertSearchCriteria.Language)
|
||||
setCodeDisplay := getInfoDisplay(searchCriteria.SetCode)
|
||||
foilDisplay := getTriadicDisplay(searchCriteria.Foil)
|
||||
promoDisplay := getTriadicDisplay(searchCriteria.Promo)
|
||||
languageDisplay := getInfoDisplay(searchCriteria.Language)
|
||||
|
||||
fmt.Println("Set code:", setCodeDisplay)
|
||||
fmt.Println("Foil:", foilDisplay)
|
||||
|
@ -53,9 +58,14 @@ func showSelectedCard() {
|
|||
fmt.Println("Selected card:", selectedCardDisplay)
|
||||
}
|
||||
|
||||
func getSearchOptions(db *sql.DB, insertSearchCriteria logic.SearchCriteria) logic.InsertSearchOptions {
|
||||
options, err := logic.GetAllSearchOptions(db, insertSearchCriteria)
|
||||
logic.Check(err)
|
||||
shouldRefreshSearch = false
|
||||
return options
|
||||
func getSearchOptions(db *sql.DB) {
|
||||
if shouldRefreshSearch {
|
||||
fmt.Println("LOADING")
|
||||
options, err := logic.GetAllSearchOptions(db, searchCriteria)
|
||||
logic.Check(err)
|
||||
searchOptions = options
|
||||
shouldRefreshSearch = false
|
||||
fmt.Println("READY")
|
||||
fmt.Println("RUN")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
import "database/sql"
|
||||
|
||||
type LocateCardResult struct {
|
||||
CardName string
|
||||
|
@ -18,16 +14,11 @@ type LocateCardResult struct {
|
|||
Position int
|
||||
}
|
||||
|
||||
func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) {
|
||||
func GetLocateCardResultsByCardName(db *sql.DB, cardName string) ([]LocateCardResult, error) {
|
||||
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, args, err := sqlx.In(query, cardNames)
|
||||
if err != nil {
|
||||
return results, err
|
||||
}
|
||||
|
||||
rows, err := db.Query(query, args...)
|
||||
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 = ?;"
|
||||
rows, err := db.Query(query, cardName)
|
||||
defer rows.Close()
|
||||
if err != nil {
|
||||
return results, err
|
||||
|
|
|
@ -17,7 +17,6 @@ require (
|
|||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // 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/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 // indirect
|
||||
github.com/mattn/go-isatty v0.0.18 // indirect
|
||||
|
|
|
@ -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/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/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/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||
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.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||
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/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
|
||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -2,67 +2,10 @@ package logic
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"sevenkeys/database"
|
||||
)
|
||||
|
||||
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable
|
||||
|
||||
func GetBinderLocationDescription(position int) string {
|
||||
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
|
||||
func LocateCards(db *sql.DB, cardName string) ([]database.LocateCardResult, error) {
|
||||
results, err := database.GetLocateCardResultsByCardName(db, cardName)
|
||||
return results, err
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -25,9 +25,9 @@ type SearchCriteria struct {
|
|||
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
|
||||
// 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
|
||||
|
||||
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) (InsertSearchOptions, error) {
|
||||
var searchOptions InsertSearchOptions = make(map[string]string)
|
||||
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptions, error) {
|
||||
var searchOptions SearchOptions = make(map[string]string)
|
||||
|
||||
cardPrintings, err := database.GetAllCardPrintings(db)
|
||||
if err != nil {
|
||||
|
@ -87,30 +87,28 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearc
|
|||
return searchOptions, err
|
||||
}
|
||||
|
||||
func GenericSearch[pk string | int](options map[string]pk) (pk, error) {
|
||||
var value pk
|
||||
|
||||
func Search(searchOptions SearchOptions) (string, string, error) {
|
||||
cmd := exec.Command("fzf")
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
fzfStdin, err := cmd.StdinPipe()
|
||||
if err != nil {
|
||||
return value, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer fzfStdin.Close()
|
||||
for option, _ := range options {
|
||||
io.WriteString(fzfStdin, option+"\n")
|
||||
for searchString, _ := range searchOptions {
|
||||
io.WriteString(fzfStdin, searchString+"\n")
|
||||
}
|
||||
}()
|
||||
|
||||
fzfOutput, err := cmd.Output()
|
||||
if err != nil {
|
||||
return value, err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
searchResult := strings.TrimSuffix(string(fzfOutput), "\n")
|
||||
value = options[searchResult]
|
||||
return value, nil
|
||||
key := strings.TrimSuffix(string(fzfOutput), "\n")
|
||||
|
||||
return searchOptions[key], key, nil
|
||||
}
|
||||
|
|
|
@ -8,8 +8,8 @@ import (
|
|||
|
||||
func main() {
|
||||
db := database.GetDatabaseFromConfig("config.json")
|
||||
|
||||
figlet.ReadFigletFonts()
|
||||
|
||||
cli.ShowSplashScreen()
|
||||
|
||||
cli.RunUpdateCheck(db)
|
||||
|
|
Loading…
Reference in New Issue