Implement cli interface

This commit is contained in:
The Magician 2024-06-10 16:34:16 +01:00
parent 2885ec32bc
commit aad3ba9a57
10 changed files with 216 additions and 67 deletions

28
sevenkeys/cli/mainui.go Normal file
View File

@ -0,0 +1,28 @@
package cli
import (
"fmt"
"sevenkeys/logic"
)
func MainCliLoop(searchOptions logic.SearchOptions) {
var command string
for {
command = GetStringResponse("SEVENKEYS $")
switch command {
case "quit":
return
case "splash":
ShowSplashScreen()
break
case "stash":
StashCliLoop(searchOptions)
break
default:
fmt.Println("Unrecognized command:", command)
break
}
}
}

20
sevenkeys/cli/response.go Normal file
View File

@ -0,0 +1,20 @@
package cli
import (
"fmt"
"strings"
)
func GetStringResponse(prompt string) string {
fmt.Print(prompt + " ")
var response string
fmt.Scan(&response)
return response
}
func GetYesNoResponse(prompt string) bool {
response := GetStringResponse(prompt)
return strings.ToUpper(response) == "Y"
}

View File

@ -0,0 +1,19 @@
package cli
import (
"sevenkeys/figlet"
"github.com/inancgumus/screen"
)
func ClearScreen() {
screen.Clear()
screen.MoveTopLeft()
}
func ShowSplashScreen() {
ClearScreen()
figlet.PrintMsgSlant("SEVENKEYS", "center")
figlet.PrintMsgTerm("the ultimate Magic: the Gathering trading card storage system", "center")
}

38
sevenkeys/cli/stashui.go Normal file
View File

@ -0,0 +1,38 @@
package cli
import (
"fmt"
"sevenkeys/logic"
)
var (
selectedCardPrintingId string
selectedCardPrintingSearchLine string
)
func ShowSelectedCard() {
if selectedCardPrintingId != "" {
fmt.Println("Selected card:", selectedCardPrintingSearchLine)
}
}
func StashCliLoop(searchOptions logic.SearchOptions) {
var command string
for {
ShowSelectedCard()
command = GetStringResponse("SEVENKEYS (stash) $")
switch command {
case "back":
return
case "search":
var err error
selectedCardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
logic.Check(err)
break
default:
fmt.Println("Unrecognized command:", command)
}
}
}

37
sevenkeys/cli/updateui.go Normal file
View File

@ -0,0 +1,37 @@
package cli
import (
"database/sql"
"fmt"
"sevenkeys/logic"
"sevenkeys/logic/scryfall"
)
func RunUpdateCheck(db *sql.DB) {
fmt.Println("Checking for updates...")
bulkData, err := scryfall.GetBulkDataByType(scryfall.BulkDataTypeAllCards)
logic.Check(err)
needsUpdate, err := logic.CheckForUpdates(db, bulkData)
logic.Check(err)
if !needsUpdate {
fmt.Println("No update required.")
return
}
fmt.Println("Update required.")
if GetYesNoResponse("Run update? (y/N)") {
fmt.Println("Running update...")
logic.CreateCacheDirectories()
err = logic.UpdateSets(db)
logic.Check(err)
err = logic.UpdateCards(db, bulkData)
logic.Check(err)
}
fmt.Println("Update finished.")
}

View File

@ -1,23 +1,60 @@
package figlet
import (
"os"
"path/filepath"
"sevenkeys/constants"
"sevenkeys/logic"
"github.com/lukesampson/figlet/figletlib"
)
var (
FigletFontSlant *figletlib.Font
FigletFontTerm *figletlib.Font
)
func ReadFigletFonts() {
cwd, err := os.Getwd()
logic.Check(err)
fontsdir := filepath.Join(cwd, "fonts")
FigletFontSlant, err = figletlib.GetFontByName(fontsdir, "slant")
logic.Check(err)
FigletFontTerm, err = figletlib.GetFontByName(fontsdir, "term")
logic.Check(err)
}
func PrintMsgSlant(msg, alignment string) {
figletlib.PrintMsg(msg,
FigletFontSlant,
200,
FigletFontSlant.Settings(),
alignment)
}
func PrintMsgTerm(msg, alignment string) {
figletlib.PrintMsg(msg,
FigletFontTerm,
200,
FigletFontTerm.Settings(),
alignment)
}
func SprintMsgSlant(msg, alignment string) string {
return figletlib.SprintMsg(msg,
constants.FigletFontSlant,
FigletFontSlant,
constants.WindowWidth,
constants.FigletFontSlant.Settings(),
FigletFontSlant.Settings(),
alignment)
}
func SprintMsgTerm(msg, alignment string) string {
return figletlib.SprintMsg(msg,
constants.FigletFontTerm,
FigletFontTerm,
constants.WindowWidth,
constants.FigletFontTerm.Settings(),
FigletFontTerm.Settings(),
alignment)
}

View File

@ -15,6 +15,7 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/lipgloss v0.9.1 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 // 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
@ -25,8 +26,9 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.6 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
)

View File

@ -16,6 +16,8 @@ github.com/go-mysql/errors v0.0.0-20180603193453-03314bea68e0 h1:meiLwrW6ukHHehy
github.com/go-mysql/errors v0.0.0-20180603193453-03314bea68e0/go.mod h1:ZH8V0509n2OSZLMYTMHzcy4hqUB+rG8ghK1zsP4i5gE=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
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/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=
@ -39,13 +41,23 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=
github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=

View File

@ -11,7 +11,6 @@ import (
"os"
"sevenkeys/database"
"sevenkeys/logic/scryfall"
"strings"
"time"
sqlerr "github.com/go-mysql/errors"
@ -35,14 +34,6 @@ func CheckForUpdates(db *sql.DB, bulkData scryfall.BulkData) (bool, error) {
return bulkData.UpdatedAtTime.After(cachedFileTimestamp), nil
}
func ConfirmUpdate() bool {
fmt.Print("Run update? (y/N) ")
var response string
fmt.Scan(&response)
return strings.ToUpper(response) == "Y"
}
func UpdateSets(db *sql.DB) error {
sets, err := scryfall.GetAllSets()
if err != nil {

View File

@ -1,67 +1,32 @@
package main
import (
"os"
"path/filepath"
"sevenkeys/constants"
"fmt"
"sevenkeys/cli"
"sevenkeys/database"
"sevenkeys/figlet"
"sevenkeys/logic"
"sevenkeys/tui"
tea "github.com/charmbracelet/bubbletea"
"github.com/lukesampson/figlet/figletlib"
)
func readFigletFonts() {
cwd, err := os.Getwd()
logic.Check(err)
fontsdir := filepath.Join(cwd, "fonts")
constants.FigletFontSlant, err = figletlib.GetFontByName(fontsdir, "slant")
logic.Check(err)
constants.FigletFontTerm, err = figletlib.GetFontByName(fontsdir, "term")
logic.Check(err)
}
var searchOptions logic.SearchOptions
func main() {
constants.Database = database.GetDatabaseFromConfig("config.json")
db := database.GetDatabaseFromConfig("config.json")
figlet.ReadFigletFonts()
readFigletFonts()
cli.ShowSplashScreen()
m := tui.NewMainModel()
constants.Program = tea.NewProgram(m, tea.WithAltScreen())
_, err := constants.Program.Run()
cli.RunUpdateCheck(db)
fmt.Println("LOADING")
searchOptions, err := logic.GetAllSearchOptions(db)
logic.Check(err)
fmt.Println("READY")
fmt.Println("RUN")
cli.MainCliLoop(searchOptions)
/*
fmt.Println("Checking for updates...")
bulkData, err := scryfall.GetBulkDataByType(scryfall.BulkDataTypeAllCards)
logic.Check(err)
needsUpdate, err := logic.CheckForUpdates(db, bulkData)
logic.Check(err)
if needsUpdate {
fmt.Println("Update required.")
if logic.ConfirmUpdate() {
fmt.Println("Running update...")
logic.CreateCacheDirectories()
err = logic.UpdateSets(db)
logic.Check(err)
err = logic.UpdateCards(db, bulkData)
logic.Check(err)
}
fmt.Println("Update finished.")
} else {
fmt.Println("No update required.")
}
storageBox := logic.GetResponse("Enter storage box label:")
source := logic.GetResponse("Enter source:")