Skip to content

Commit d79f990

Browse files
feat: add config to model; use config for styling
1 parent d47b3df commit d79f990

File tree

3 files changed

+77
-22
lines changed

3 files changed

+77
-22
lines changed

internal/config/config.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,31 @@ type Config struct {
1515
GameEnds bool // Whether or not the game ends when the max level is reached
1616

1717
// The styling for the game in all modes
18-
Theme struct {
19-
Colours struct {
20-
TetriminoCells struct {
21-
I string
22-
O string
23-
T string
24-
S string
25-
Z string
26-
J string
27-
L string
28-
}
29-
EmptyCell string
30-
GhostCell string
31-
}
32-
Characters struct {
33-
Tetriminos string
34-
EmptyCell string
35-
GhostCell string
18+
Theme Theme
19+
}
20+
21+
type Theme struct {
22+
Colours struct {
23+
TetriminoCells struct {
24+
I string
25+
O string
26+
T string
27+
S string
28+
Z string
29+
J string
30+
L string
3631
}
32+
EmptyCell string
33+
GhostCell string
34+
}
35+
Characters struct {
36+
Tetriminos string
37+
EmptyCell string
38+
GhostCell string
3739
}
3840
}
3941

40-
func GetConfig() (*Config, error) {
42+
func GetConfig(path string) (*Config, error) {
4143
var c Config
4244

4345
c.QueueLength = 5
@@ -60,7 +62,7 @@ func GetConfig() (*Config, error) {
6062
c.Theme.Characters.EmptyCell = "▕ "
6163
c.Theme.Characters.GhostCell = "░░"
6264

63-
_, err := toml.DecodeFile("config.toml", &c)
65+
_, err := toml.DecodeFile(path, &c)
6466
if err != nil {
6567
if errors.Is(err, os.ErrNotExist) {
6668
return &c, nil

internal/marathon/model.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"time"
66

7+
"github.com/Broderick-Westrope/tetrigo/internal/config"
78
"github.com/Broderick-Westrope/tetrigo/tetris"
89
"github.com/charmbracelet/bubbles/help"
910
"github.com/charmbracelet/bubbles/key"
@@ -24,6 +25,7 @@ type Model struct {
2425
scoring *tetris.Scoring
2526
bag *tetris.Bag
2627
timer stopwatch.Model
28+
cfg *config.Config
2729
}
2830

2931
func InitialModel(level uint) *Model {
@@ -54,7 +56,7 @@ func InitialModel(level uint) *Model {
5456
}
5557

5658
func (m Model) Init() tea.Cmd {
57-
return tea.Batch(m.fall.stopwatch.Init(), m.timer.Init())
59+
return tea.Batch(m.fall.stopwatch.Init(), m.timer.Init(), configCmd("config.toml"))
5860
}
5961

6062
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
@@ -111,6 +113,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
111113
if err != nil {
112114
panic(fmt.Errorf("failed to lower tetrimino (tick): %w", err))
113115
}
116+
case configMsg:
117+
if msg.err != nil {
118+
panic(fmt.Errorf("failed to load config: %w", msg.err))
119+
}
120+
m.styles = CreateStyles(&msg.cfg.Theme)
121+
m.cfg = msg.cfg
114122
}
115123

116124
var cmd tea.Cmd
@@ -282,3 +290,18 @@ func (m *Model) lowerTetrimino() (bool, error) {
282290

283291
return false, nil
284292
}
293+
294+
type configMsg struct {
295+
err error
296+
cfg *config.Config
297+
}
298+
299+
func configCmd(path string) tea.Cmd {
300+
return func() tea.Msg {
301+
cfg, err := config.GetConfig(path)
302+
return configMsg{
303+
err: err,
304+
cfg: cfg,
305+
}
306+
}
307+
}

internal/marathon/style.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package marathon
22

3-
import "github.com/charmbracelet/lipgloss"
3+
import (
4+
"github.com/Broderick-Westrope/tetrigo/internal/config"
5+
"github.com/charmbracelet/lipgloss"
6+
)
47

58
type Styles struct {
69
Playfield lipgloss.Style
@@ -44,3 +47,30 @@ func defaultStyles() *Styles {
4447
}
4548
return &s
4649
}
50+
51+
func CreateStyles(theme *config.Theme) *Styles {
52+
s := Styles{
53+
Playfield: lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(0),
54+
ColIndicator: lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.EmptyCell)),
55+
TetriminoStyles: map[byte]lipgloss.Style{
56+
'I': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.I)),
57+
'O': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.O)),
58+
'T': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.T)),
59+
'S': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.S)),
60+
'Z': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.Z)),
61+
'J': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.J)),
62+
'L': lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.TetriminoCells.L)),
63+
},
64+
GhostCell: lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Colours.GhostCell)),
65+
Hold: holdStyles{
66+
View: lipgloss.NewStyle().Width(10).Height(5).Border(lipgloss.RoundedBorder(), true, false, true, true).Align(lipgloss.Center, lipgloss.Center),
67+
Label: lipgloss.NewStyle().Width(10).PaddingLeft(1).PaddingBottom(1),
68+
Item: lipgloss.NewStyle().Width(10).Height(2).Align(lipgloss.Center, lipgloss.Center),
69+
},
70+
Information: lipgloss.NewStyle().Width(13).Align(lipgloss.Left, lipgloss.Top),
71+
RowIndicator: lipgloss.NewStyle().Foreground(lipgloss.Color(theme.Characters.EmptyCell)).Align(lipgloss.Left).Padding(0, 1, 0),
72+
Bag: lipgloss.NewStyle().PaddingTop(1),
73+
}
74+
return &s
75+
76+
}

0 commit comments

Comments
 (0)