Skip to content

Commit

Permalink
refactor: move tui models (#7)
Browse files Browse the repository at this point in the history
* refactor: move tui models from cmd/tetrigo/ to internal/tui/

* chore: fix lint
  • Loading branch information
Broderick-Westrope authored Aug 20, 2024
1 parent 91d2c34 commit cf35613
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 70 deletions.
8 changes: 0 additions & 8 deletions cmd/tetrigo/common/model.go

This file was deleted.

89 changes: 41 additions & 48 deletions cmd/tetrigo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helpers
package common

import (
"bytes"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions cmd/tetrigo/menu/model.go → internal/tui/menu/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
File renamed without changes.

0 comments on commit cf35613

Please sign in to comment.