Files
inp2025/plays/landing.go
2025-09-10 11:35:46 +08:00

93 lines
1.5 KiB
Go

package plays
import (
"fmt"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
var (
landingChoices = []string{"Login", "Register"}
)
type Landing struct {
*Base
choice string
cursor int
}
func NewLanding(base *Base) *Landing {
m := Landing{
Base: base,
choice: "",
cursor: 0,
}
return &m
}
func (m *Landing) Init() tea.Cmd {
return tea.ClearScreen
}
func (m *Landing) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+l":
return m, tea.ClearScreen
case "ctrl+c", "q":
return m, tea.Quit
case "enter":
m.choice = landingChoices[m.cursor]
return m, tea.Quit
case "tab", "shift+tab", "up", "down":
s := msg.String()
if s == "up" || s == "shift+tab" {
m.cursor--
} else {
m.cursor++
}
if m.cursor >= len(landingChoices) {
m.cursor = 0
} else if m.cursor < 0 {
m.cursor = len(landingChoices) - 1
}
}
}
return m, nil
}
func (m *Landing) View() string {
var b strings.Builder
for i := 0; i < len(landingChoices); i++ {
if m.cursor == i {
fmt.Fprintf(&b, "(•) %s\n", landingChoices[i])
} else {
fmt.Fprintf(&b, "( ) %s\n", landingChoices[i])
}
}
return b.String()
}
func (m *Landing) Next(queue *[]*tea.Program) error {
switch m.choice {
case "Login":
*queue = append(*queue,
tea.NewProgram(NewLogin(m.Base)))
case "Register":
*queue = append(*queue,
tea.NewProgram(NewRegister(m.Base)))
}
return nil
}