diff --git a/cmd/tetrigo/common/model.go b/cmd/tetrigo/common/model.go deleted file mode 100644 index ada3d93..0000000 --- a/cmd/tetrigo/common/model.go +++ /dev/null @@ -1,8 +0,0 @@ -package common - -import tea "github.com/charmbracelet/bubbletea" - -type Model interface { - Init() tea.Cmd - Update(tea.Msg) (Model, tea.Cmd) -} diff --git a/cmd/tetrigo/main.go b/cmd/tetrigo/main.go index 96a9b06..f4c7fe7 100644 --- a/cmd/tetrigo/main.go +++ b/cmd/tetrigo/main.go @@ -2,42 +2,56 @@ package main import ( "fmt" - "log" - "os" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/common" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/starter" "github.com/Broderick-Westrope/tetrigo/internal/config" "github.com/Broderick-Westrope/tetrigo/internal/data" + "github.com/Broderick-Westrope/tetrigo/internal/tui/common" + "github.com/Broderick-Westrope/tetrigo/internal/tui/starter" "github.com/alecthomas/kong" tea "github.com/charmbracelet/bubbletea" ) type CLI struct { - Config string `help:"Path to config file" default:"config.toml" type:"path"` - DB string `help:"Path to database file" default:"tetrigo.db"` + GlobalVars Menu MenuCmd `cmd:"" help:"Start in the menu" default:"1"` - Marathon MarathonCmd `cmd:"" help:"Play in marathon mode"` + Play PlayCmd `cmd:"" help:"Play a specific game mode"` Leaderboard LeaderboardCmd `cmd:"" help:"Start on the leaderboard"` } +type GlobalVars struct { + Config string `help:"Path to config file" default:"config.toml" type:"path"` + DB string `help:"Path to database file" default:"tetrigo.db"` +} + type MenuCmd struct{} -type MarathonCmd struct { - Level uint `help:"Level to start at" short:"l" default:"1"` - Name string `help:"Name of the player" default:"Anonymous"` +func (c *MenuCmd) Run(globals *GlobalVars) error { + return launchStarter(globals, common.ModeMenu, common.NewMenuInput()) +} + +type PlayCmd struct { + GameMode string `arg:"" help:"Game mode to play" default:"marathon"` + Level uint `help:"Level to start at" short:"l" default:"1"` + Name string `help:"Name of the player" short:"n" default:"Anonymous"` +} + +func (c *PlayCmd) Run(globals *GlobalVars) error { + switch c.GameMode { + case "marathon": + return launchStarter(globals, common.ModeMarathon, common.NewMarathonInput(c.Level, c.Name)) + default: + return fmt.Errorf("invalid game mode: %s", c.GameMode) + } } type LeaderboardCmd struct { - GameMode string `help:"Game mode to display" default:"marathon"` + GameMode string `arg:"" help:"Game mode to display" default:"marathon"` } -var subcommandToStarterMode = map[string]common.Mode{ - "menu": common.ModeMenu, - "marathon": common.ModeMarathon, - "leaderboard": common.ModeLeaderboard, +func (c *LeaderboardCmd) Run(globals *GlobalVars) error { + return launchStarter(globals, common.ModeLeaderboard, common.NewLeaderboardInput(c.GameMode)) } func main() { @@ -47,53 +61,32 @@ func main() { kong.Description("A tetris TUI written in Go"), kong.UsageOnError(), ) + // Call the Run() method of the selected parsed command. + err := ctx.Run(&cli.GlobalVars) + ctx.FatalIfErrorf(err) +} - starterMode, ok := subcommandToStarterMode[ctx.Command()] - if !ok { - log.Printf("Invalid command: %s\n", ctx.Command()) - } - - db, err := data.NewDB(cli.DB) - if err != nil { - log.Printf("error opening database: %v", err) - os.Exit(1) - } - - cfg, err := config.GetConfig(cli.Config) +func launchStarter(globals *GlobalVars, starterMode common.Mode, switchIn common.SwitchModeInput) error { + db, err := data.NewDB(globals.DB) if err != nil { - log.Printf("error getting config: %v", err) - os.Exit(1) + return fmt.Errorf("error opening database: %w", err) } - switchIn, err := cli.getSwitchModeInput(starterMode) + cfg, err := config.GetConfig(globals.Config) if err != nil { - log.Printf("error getting switch mode input: %v", err) - os.Exit(1) + return fmt.Errorf("error getting config: %w", err) } model, err := starter.NewModel( starter.NewInput(starterMode, switchIn, db, cfg), ) if err != nil { - log.Printf("error creating starter model: %v", err) - os.Exit(1) + return fmt.Errorf("error creating starter model: %w", err) } if _, err = tea.NewProgram(model, tea.WithAltScreen()).Run(); err != nil { - log.Printf("Alas, there's been an error: %v", err) - os.Exit(1) + return fmt.Errorf("error running tea program: %w", err) } -} -func (cli CLI) getSwitchModeInput(starterMode common.Mode) (common.SwitchModeInput, error) { - switch starterMode { - case common.ModeMenu: - return common.NewMenuInput(), nil - case common.ModeMarathon: - return common.NewMarathonInput(cli.Marathon.Level, cli.Marathon.Name), nil - case common.ModeLeaderboard: - return common.NewLeaderboardInput(cli.Leaderboard.GameMode), nil - default: - return nil, fmt.Errorf("invalid starter mode: %v", starterMode) - } + return nil } diff --git a/cmd/tetrigo/common/input.go b/internal/tui/common/input.go similarity index 100% rename from cmd/tetrigo/common/input.go rename to internal/tui/common/input.go diff --git a/cmd/tetrigo/common/utils.go b/internal/tui/common/keys.go similarity index 100% rename from cmd/tetrigo/common/utils.go rename to internal/tui/common/keys.go diff --git a/cmd/tetrigo/common/mode.go b/internal/tui/common/mode.go similarity index 100% rename from cmd/tetrigo/common/mode.go rename to internal/tui/common/mode.go diff --git a/cmd/tetrigo/helpers/helpers.go b/internal/tui/common/overlay.go similarity index 99% rename from cmd/tetrigo/helpers/helpers.go rename to internal/tui/common/overlay.go index 50db47c..549145b 100644 --- a/cmd/tetrigo/helpers/helpers.go +++ b/internal/tui/common/overlay.go @@ -1,4 +1,4 @@ -package helpers +package common import ( "bytes" diff --git a/cmd/tetrigo/components/hpicker/keymap.go b/internal/tui/components/hpicker/keymap.go similarity index 100% rename from cmd/tetrigo/components/hpicker/keymap.go rename to internal/tui/components/hpicker/keymap.go diff --git a/cmd/tetrigo/components/hpicker/model.go b/internal/tui/components/hpicker/model.go similarity index 100% rename from cmd/tetrigo/components/hpicker/model.go rename to internal/tui/components/hpicker/model.go diff --git a/cmd/tetrigo/components/hpicker/styles.go b/internal/tui/components/hpicker/styles.go similarity index 100% rename from cmd/tetrigo/components/hpicker/styles.go rename to internal/tui/components/hpicker/styles.go diff --git a/cmd/tetrigo/components/textinput/textinput.go b/internal/tui/components/textinput/textinput.go similarity index 100% rename from cmd/tetrigo/components/textinput/textinput.go rename to internal/tui/components/textinput/textinput.go diff --git a/cmd/tetrigo/leaderboard/keymap.go b/internal/tui/leaderboard/keymap.go similarity index 100% rename from cmd/tetrigo/leaderboard/keymap.go rename to internal/tui/leaderboard/keymap.go diff --git a/cmd/tetrigo/leaderboard/model.go b/internal/tui/leaderboard/model.go similarity index 97% rename from cmd/tetrigo/leaderboard/model.go rename to internal/tui/leaderboard/model.go index 3cb390c..388d051 100644 --- a/cmd/tetrigo/leaderboard/model.go +++ b/internal/tui/leaderboard/model.go @@ -4,8 +4,8 @@ import ( "database/sql" "strconv" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/common" "github.com/Broderick-Westrope/tetrigo/internal/data" + "github.com/Broderick-Westrope/tetrigo/internal/tui/common" "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/table" diff --git a/cmd/tetrigo/marathon/keymap.go b/internal/tui/marathon/keymap.go similarity index 96% rename from cmd/tetrigo/marathon/keymap.go rename to internal/tui/marathon/keymap.go index 47b2948..0a808b2 100644 --- a/cmd/tetrigo/marathon/keymap.go +++ b/internal/tui/marathon/keymap.go @@ -1,8 +1,8 @@ package marathon import ( - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/common" "github.com/Broderick-Westrope/tetrigo/internal/config" + "github.com/Broderick-Westrope/tetrigo/internal/tui/common" "github.com/charmbracelet/bubbles/key" ) diff --git a/cmd/tetrigo/marathon/model.go b/internal/tui/marathon/model.go similarity index 97% rename from cmd/tetrigo/marathon/model.go rename to internal/tui/marathon/model.go index d9cd7a9..252cb1e 100644 --- a/cmd/tetrigo/marathon/model.go +++ b/internal/tui/marathon/model.go @@ -5,10 +5,9 @@ import ( "strconv" "time" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/common" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/helpers" "github.com/Broderick-Westrope/tetrigo/internal/config" "github.com/Broderick-Westrope/tetrigo/internal/data" + "github.com/Broderick-Westrope/tetrigo/internal/tui/common" "github.com/Broderick-Westrope/tetrigo/pkg/tetris" "github.com/Broderick-Westrope/tetrigo/pkg/tetris/modes/marathon" "github.com/charmbracelet/bubbles/help" @@ -248,11 +247,11 @@ func (m *Model) View() string { ) if m.game.IsGameOver() { - output = helpers.PlaceOverlayCenter(gameOverMsg, output) + output = common.PlaceOverlayCenter(gameOverMsg, output) } if m.isPaused { - output = helpers.PlaceOverlayCenter(pausedMsg, output) + output = common.PlaceOverlayCenter(pausedMsg, output) } output = lipgloss.JoinVertical(lipgloss.Left, output, m.help.View(m.keys)) diff --git a/cmd/tetrigo/marathon/styles.go b/internal/tui/marathon/styles.go similarity index 100% rename from cmd/tetrigo/marathon/styles.go rename to internal/tui/marathon/styles.go diff --git a/cmd/tetrigo/menu/keymap.go b/internal/tui/menu/keymap.go similarity index 100% rename from cmd/tetrigo/menu/keymap.go rename to internal/tui/menu/keymap.go diff --git a/cmd/tetrigo/menu/model.go b/internal/tui/menu/model.go similarity index 95% rename from cmd/tetrigo/menu/model.go rename to internal/tui/menu/model.go index f8c23a8..98eead1 100644 --- a/cmd/tetrigo/menu/model.go +++ b/internal/tui/menu/model.go @@ -4,9 +4,9 @@ import ( "fmt" "strconv" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/common" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/components/hpicker" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/components/textinput" + "github.com/Broderick-Westrope/tetrigo/internal/tui/common" + "github.com/Broderick-Westrope/tetrigo/internal/tui/components/hpicker" + "github.com/Broderick-Westrope/tetrigo/internal/tui/components/textinput" "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" diff --git a/cmd/tetrigo/menu/styles.go b/internal/tui/menu/styles.go similarity index 100% rename from cmd/tetrigo/menu/styles.go rename to internal/tui/menu/styles.go diff --git a/cmd/tetrigo/starter/model.go b/internal/tui/starter/model.go similarity index 91% rename from cmd/tetrigo/starter/model.go rename to internal/tui/starter/model.go index 74d57c6..6656471 100644 --- a/cmd/tetrigo/starter/model.go +++ b/internal/tui/starter/model.go @@ -5,11 +5,11 @@ import ( "errors" "reflect" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/common" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/leaderboard" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/marathon" - "github.com/Broderick-Westrope/tetrigo/cmd/tetrigo/menu" "github.com/Broderick-Westrope/tetrigo/internal/config" + "github.com/Broderick-Westrope/tetrigo/internal/tui/common" + "github.com/Broderick-Westrope/tetrigo/internal/tui/leaderboard" + "github.com/Broderick-Westrope/tetrigo/internal/tui/marathon" + "github.com/Broderick-Westrope/tetrigo/internal/tui/menu" "github.com/charmbracelet/bubbles/key" tea "github.com/charmbracelet/bubbletea" ) diff --git a/cmd/tetrigo/starter/styles.go b/internal/tui/starter/styles.go similarity index 100% rename from cmd/tetrigo/starter/styles.go rename to internal/tui/starter/styles.go