From 3ef49bc72c5da137e9b28311014d1576550ce56e Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 17 Sep 2024 11:25:51 +0100 Subject: [PATCH] Fix bug in reporting binder locations --- sevenkeys/cli/mainui.go | 18 ++++++++----- sevenkeys/cli/storage.go | 19 -------------- sevenkeys/logic/locate.go | 5 +++- sevenkeys/logic/locate_test.go | 46 ++++++++++++++++++++++++---------- sevenkeys/logic/storage.go | 40 ++++++----------------------- 5 files changed, 56 insertions(+), 72 deletions(-) diff --git a/sevenkeys/cli/mainui.go b/sevenkeys/cli/mainui.go index 05bd77d..aac287c 100644 --- a/sevenkeys/cli/mainui.go +++ b/sevenkeys/cli/mainui.go @@ -15,7 +15,7 @@ var output string func MainCliLoop(db *sql.DB) { var command string - var selectedStorageArea database.StorageArea + var selectedStorageAreaName string var cardLocation database.CardLocation var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{ @@ -36,7 +36,7 @@ func MainCliLoop(db *sql.DB) { for { ShowSplashScreen() - showStorageInfo(os.Stdout, selectedStorageArea) + showStorageInfo(os.Stdout, selectedStorageAreaName) showInsertSearchCriteria(insertSearchCriteria) showSelectedCard() showCopiesInserted() @@ -56,10 +56,14 @@ func MainCliLoop(db *sql.DB) { logic.Check(err) break case "a", "area": - area, err := logic.SelectStorageArea(db) + options, err := logic.GetStorageAreaSearchOptions(db) logic.Check(err) - selectedStorageArea = area - cardLocation.StorageAreaId = area.Id + + id, name, err := logic.GenericSearch(options) + logic.Check(err) + + selectedStorageAreaName = name + cardLocation.StorageAreaId = id break case "c", "criteria": insertSearchCriteria = getSearchCriteria() @@ -87,7 +91,9 @@ func MainCliLoop(db *sql.DB) { } break case "i", "insert": - insertSelectedCard(db, selectedStorageArea, cardLocation) + err := logic.StoreCard(db, cardLocation) + logic.Check(err) + copiesInserted++ break case "l", "locate": filename := GetStringResponse("Filename:") diff --git a/sevenkeys/cli/storage.go b/sevenkeys/cli/storage.go index 5893a91..0f86827 100644 --- a/sevenkeys/cli/storage.go +++ b/sevenkeys/cli/storage.go @@ -1,11 +1,9 @@ package cli import ( - "database/sql" "fmt" "io" "sevenkeys/database" - "sevenkeys/logic" ) var ( @@ -36,20 +34,3 @@ func showStorageInfo(w io.Writer, area database.StorageArea) { func showCopiesInserted() { fmt.Println("Copies inserted:", copiesInserted) } - -func insertSelectedCard(db *sql.DB, area database.StorageArea, location database.CardLocation) { - if area.Name == "" { - output = "No storage area selected." - return - } - - if location.CardPrintingId == "" { - output = "No card selected, please [search] for a card printing." - return - } - - err := logic.StoreCard(db, location) - logic.Check(err) - - copiesInserted++ -} diff --git a/sevenkeys/logic/locate.go b/sevenkeys/logic/locate.go index ce6008b..cf1a5fe 100644 --- a/sevenkeys/logic/locate.go +++ b/sevenkeys/logic/locate.go @@ -9,9 +9,12 @@ import ( 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 page int = ((position - 1) / SLOTS_PER_BINDER_PAGE) + 1 var pagePosition int = position % SLOTS_PER_BINDER_PAGE + if pagePosition == 0 { + pagePosition = SLOTS_PER_BINDER_PAGE + } var slot int var frontOrBack string diff --git a/sevenkeys/logic/locate_test.go b/sevenkeys/logic/locate_test.go index baf4ef5..eed9d2d 100644 --- a/sevenkeys/logic/locate_test.go +++ b/sevenkeys/logic/locate_test.go @@ -2,24 +2,44 @@ 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) - +func assert(t *testing.T, description string, expected string) { if description != expected { - t.Errorf("expected %s, got %s\n", expected, description) + 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" +func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForFirstSlotInPage(t *testing.T) { + var position int = 1 + var expected string = " on page 1 in front slot 1" description := GetBinderLocationDescription(position) - if description != expected { - t.Errorf("expected %s, got %s\n", expected, description) - } + assert(t, description, expected) +} + +func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForFirstSlotOnBackOfPage(t *testing.T) { + var position int = 10 + var expected string = " on page 1 in back slot 1" + + description := GetBinderLocationDescription(position) + + assert(t, description, expected) +} + +func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForLastSlotOnBackOfPage(t *testing.T) { + var position int = 18 + var expected string = " on page 1 in back slot 9" + + description := GetBinderLocationDescription(position) + + assert(t, description, expected) +} + +func Test_GetBinderLocationDescription_ReturnsCorrectSlotNumber_ForLastSlotOnBackOfSecondPage(t *testing.T) { + var position int = 36 + var expected string = " on page 2 in back slot 9" + + description := GetBinderLocationDescription(position) + + assert(t, description, expected) } diff --git a/sevenkeys/logic/storage.go b/sevenkeys/logic/storage.go index 20f0869..2f3b4e6 100644 --- a/sevenkeys/logic/storage.go +++ b/sevenkeys/logic/storage.go @@ -2,13 +2,11 @@ package logic import ( "database/sql" - "io" - "os" - "os/exec" "sevenkeys/database" - "strings" ) +type StorageAreaSearchOptions map[string]int + func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error { // TODO: Check if there's already a storage are with the same name // TODO: Check if the type entered is valid @@ -20,43 +18,19 @@ func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error { return nil } -func SelectStorageArea(db *sql.DB) (database.StorageArea, error) { - var selectedStorageArea database.StorageArea +func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) { + var options StorageAreaSearchOptions storageAreas, err := database.GetAllStorageAreas(db) if err != nil { - return selectedStorageArea, err + return options, err } - cmd := exec.Command("fzf") - cmd.Stderr = os.Stderr - - fzfStdin, err := cmd.StdinPipe() - if err != nil { - return selectedStorageArea, err - } - - go func() { - defer fzfStdin.Close() - for _, area := range storageAreas { - io.WriteString(fzfStdin, area.Name+"\n") - } - }() - - fzfOutput, err := cmd.Output() - if err != nil { - return selectedStorageArea, err - } - - key := strings.TrimSuffix(string(fzfOutput), "\n") for _, area := range storageAreas { - if area.Name == key { - selectedStorageArea = area - break - } + options[area.Name] = area.Id } - return selectedStorageArea, nil + return options, nil } func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {