Draft: feat login

This commit is contained in:
2025-09-05 03:59:25 +08:00
parent 6d7074198f
commit 3a8e265ce3
28 changed files with 1131 additions and 86 deletions

79
plays/landing.go Normal file
View File

@@ -0,0 +1,79 @@
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()
}

123
plays/login.go Normal file
View File

@@ -0,0 +1,123 @@
package plays
import (
"fmt"
"strings"
"gitea.konchin.com/ytshih/inp2025/game/models"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type Login struct {
User *models.User
focusIndex int
inputs []textinput.Model
cursorMode cursor.Mode
}
func NewLogin() *Login {
m := Login{
User: nil,
focusIndex: 0,
inputs: make([]textinput.Model, 2),
cursorMode: cursor.CursorBlink,
}
for i := range m.inputs {
t := textinput.New()
t.CharLimit = 32
t.Width = 20
switch i {
case 0:
t.Placeholder = "Username"
t.Focus()
case 1:
t.Placeholder = "Password"
t.EchoMode = textinput.EchoPassword
t.EchoCharacter = '•'
}
m.inputs[i] = t
}
return &m
}
func (m *Login) Init() tea.Cmd {
return tea.Batch(tea.ClearScreen, textinput.Blink)
}
func (m *Login) 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 "tab", "shift+tab", "enter", "up", "down":
s := msg.String()
if s == "enter" && m.focusIndex == len(m.inputs)-1 {
return m, tea.Quit
}
if s == "up" || s == "shift+tab" {
m.focusIndex--
} else {
m.focusIndex++
}
if m.focusIndex >= len(m.inputs) {
m.focusIndex = 0
} else if m.focusIndex < 0 {
m.focusIndex = len(m.inputs) - 1
}
cmds := make([]tea.Cmd, len(m.inputs))
for i := 0; i < len(m.inputs); i++ {
if i == m.focusIndex {
cmds[i] = m.inputs[i].Focus()
continue
}
m.inputs[i].Blur()
}
return m, tea.Batch(cmds...)
}
}
cmd := m.updateInputs(msg)
return m, cmd
}
func (m *Login) updateInputs(msg tea.Msg) tea.Cmd {
cmds := make([]tea.Cmd, len(m.inputs))
for i := range m.inputs {
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
}
return tea.Batch(cmds...)
}
func (m *Login) View() string {
var b strings.Builder
fmt.Fprintf(&b, "User login:\n")
for i := range m.inputs {
b.WriteString(m.inputs[i].View())
if i < len(m.inputs) {
b.WriteRune('\n')
}
}
return b.String()
}

55
plays/redirect.go Normal file
View File

@@ -0,0 +1,55 @@
package plays
import (
"fmt"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type tickMsg time.Time
type Redirect struct {
Message string
secLeft int
}
func NewRedirect(msg string) *Redirect {
return &Redirect{
Message: msg,
secLeft: 3,
}
}
func tick() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
func (m *Redirect) Init() tea.Cmd {
return tick()
}
func (m *Redirect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c", "enter":
return m, tea.Quit
}
case tickMsg:
m.secLeft--
if m.secLeft <= 0 {
return m, tea.Quit
}
return m, tick()
}
return m, nil
}
func (m *Redirect) View() string {
return fmt.Sprintf("%s\n\nExit in %d seconds...", m.Message, m.secLeft)
}