diff options
author | Santo Cariotti <santo@dcariotti.me> | 2025-04-05 16:59:10 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2025-04-05 17:07:20 +0200 |
commit | de0cae741138cde6c0444455fc4863a59b70470f (patch) | |
tree | abf8da423f593de7920854df509da523a53469ce | |
parent | b5a6e1108a7209b68b5fc050d8545ab32e9a9dc7 (diff) |
Switch models
-rw-r--r-- | ui/main.go | 4 | ||||
-rw-r--r-- | ui/views/auth.go | 29 | ||||
-rw-r--r-- | ui/views/play.go | 69 | ||||
-rw-r--r-- | ui/views/views.go | 86 |
4 files changed, 166 insertions, 22 deletions
@@ -9,7 +9,9 @@ import ( func main() { views.ClearScreen() - p := tea.NewProgram(views.NewAuthModel(), tea.WithAltScreen()) + + p := tea.NewProgram(views.NewRahannaModel(), tea.WithAltScreen()) + if _, err := p.Run(); err != nil { log.Fatal(err) } diff --git a/ui/views/auth.go b/ui/views/auth.go index 0737a62..9599b4a 100644 --- a/ui/views/auth.go +++ b/ui/views/auth.go @@ -51,9 +51,7 @@ type authResponse struct { } // Initialize AuthModel which contains both tabs -func NewAuthModel() AuthModel { - width, height := GetTerminalSize() - +func NewAuthModel(width, height int) AuthModel { return AuthModel{ loginModel: initLoginModel(width, height), signupModel: initSignupModel(width, height), @@ -147,13 +145,11 @@ func (m AuthModel) Init() tea.Cmd { func (m AuthModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmds []tea.Cmd - switch msg := msg.(type) { - case tea.WindowSizeMsg: - m.width, m.height = msg.Width, msg.Height - m.loginModel.width, m.loginModel.height = msg.Width, msg.Height - m.signupModel.width, m.signupModel.height = msg.Width, msg.Height - return m, nil + if exit := handleExit(msg); exit != nil { + return m, exit + } + switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "alt+1": @@ -182,8 +178,6 @@ func (m AuthModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } return m, nil - case "ctrl+c": - return m, tea.Quit } } @@ -218,13 +212,6 @@ func (m AuthModel) View() string { windowStyle.Width(getFormWidth(width)).Render(tabContent), ) - // Logo style - logoStyle := lipgloss.NewStyle(). - Foreground(lipgloss.Color("#7ee2a8")). - Bold(true). - Align(lipgloss.Center). - Width(width) - // Center logo and form in available space contentHeight := lipgloss.Height(logo) + lipgloss.Height(ui) + 2 paddingTop := (height - contentHeight) / 2 @@ -237,7 +224,7 @@ func (m AuthModel) View() string { MarginTop(paddingTop). Render( lipgloss.JoinVertical(lipgloss.Center, - logoStyle.Render(logo), + getLogo(m.width), lipgloss.PlaceHorizontal(width, lipgloss.Center, ui), ), ) @@ -284,7 +271,7 @@ func (m loginModel) Update(msg tea.Msg) (loginModel, tea.Cmd) { } defer f.Close() f.Write([]byte(m.token)) - return m, tea.Quit + return m, SwitchModelCmd(NewPlayModel(m.width, m.height)) } case error: m.isLoading = false @@ -339,7 +326,7 @@ func (m signupModel) Update(msg tea.Msg) (signupModel, tea.Cmd) { } defer f.Close() f.Write([]byte(m.token)) - return m, tea.Quit + return m, SwitchModelCmd(NewPlayModel(m.width, m.height)) } case error: m.isLoading = false diff --git a/ui/views/play.go b/ui/views/play.go new file mode 100644 index 0000000..0c09d2a --- /dev/null +++ b/ui/views/play.go @@ -0,0 +1,69 @@ +package views + +import ( + "github.com/charmbracelet/bubbles/textinput" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +type PlayModel struct { + width int + height int +} + +func NewPlayModel(width, height int) PlayModel { + + return PlayModel{ + width: width, + height: height, + } +} + +func (m PlayModel) Init() tea.Cmd { + ClearScreen() + return textinput.Blink +} + +func (m PlayModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if exit := handleExit(msg); exit != nil { + return m, exit + } + + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "q": + return m, SwitchModelCmd(NewAuthModel(m.width, m.height)) + + } + } + return m, nil +} + +func (m PlayModel) View() string { + width, height := m.width, m.height + + // Create the window with tab content + ui := lipgloss.JoinVertical(lipgloss.Center, + windowStyle.Width(getFormWidth(width)).Render("New Play"), + ) + + // Center logo and form in available space + contentHeight := lipgloss.Height(logo) + lipgloss.Height(ui) + 2 + paddingTop := (height - contentHeight) / 2 + if paddingTop < 0 { + paddingTop = 0 + } + + // Combine logo and tabs with vertical centering + output := lipgloss.NewStyle(). + MarginTop(paddingTop). + Render( + lipgloss.JoinVertical(lipgloss.Center, + getLogo(m.width), + lipgloss.PlaceHorizontal(width, lipgloss.Center, ui), + ), + ) + + return output +} diff --git a/ui/views/views.go b/ui/views/views.go index 18c1124..2ce7eaa 100644 --- a/ui/views/views.go +++ b/ui/views/views.go @@ -4,6 +4,8 @@ import ( "os" "os/exec" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" "golang.org/x/term" ) @@ -43,3 +45,87 @@ func getFormWidth(width int) int { return formWidth } + +type RahannaModel struct { + width int + height int + currentModel tea.Model + auth AuthModel + play PlayModel +} + +func NewRahannaModel() RahannaModel { + width, height := GetTerminalSize() + + auth := NewAuthModel(width, height) + play := NewPlayModel(width, height) + + return RahannaModel{ + width: width, + height: height, + currentModel: auth, + auth: auth, + play: play, + } +} + +func (m RahannaModel) Init() tea.Cmd { + return m.currentModel.Init() +} + +type switchModel struct { + model tea.Model +} + +func SwitchModelCmd(model tea.Model) tea.Cmd { + s := switchModel{ + model: model, + } + + return func() tea.Msg { + return s + } +} + +func (m RahannaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case switchModel: + m.currentModel = msg.model + return m, nil + + case tea.WindowSizeMsg: + m.width = msg.Width + m.height = msg.Height + return m, nil + } + var cmd tea.Cmd + m.currentModel, cmd = m.currentModel.Update(msg) + return m, cmd +} + +func (m RahannaModel) View() string { + return m.currentModel.View() +} + +func handleExit(msg tea.Msg) tea.Cmd { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "ctrl+c": + return tea.Quit + } + } + + return nil +} + +func getLogo(width int) string { + logoStyle := lipgloss.NewStyle(). + Foreground(lipgloss.Color("#7ee2a8")). + Bold(true). + Align(lipgloss.Center). + Width(width) + + return logoStyle.Render(logo) + +} |