80 lines
1.2 KiB
Go
80 lines
1.2 KiB
Go
package plays
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
var (
|
|
landingChoices = []string{"Login", "Register"}
|
|
)
|
|
|
|
type Landing struct {
|
|
Choice string
|
|
|
|
cursor int
|
|
}
|
|
|
|
func NewLanding() *Landing {
|
|
m := Landing{
|
|
cursor: 0,
|
|
Choice: "",
|
|
}
|
|
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()
|
|
}
|