flewkey
/
linger
Archived
1
0
Fork 0
TUI for The Lounge
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
linger/login.go

157 lines
3.8 KiB

package main
import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbles/textinput"
)
type loginModel struct {
index int
hostInput textinput.Model
pathInput textinput.Model
userInput textinput.Model
passInput textinput.Model
loginButton string
message string
}
func doConnect(conf *config, conn *loungeConnection) tea.Cmd {
conf.Host = conn.Host
conf.Path = conn.Path
conf.User = conn.User
configWrite(conf)
return func() tea.Msg {
loungeConnect(conn)
return 1
}
}
func loginModelInit(conf *config) loginModel {
host := textinput.NewModel()
host.Placeholder = "Host"
host.Prompt = focusedPrompt
host.TextColor = focusedColor
host.CharLimit = 64
host.Focus()
host.SetValue(conf.Host)
path := textinput.NewModel()
path.Placeholder = "Path"
path.Prompt = blurredPrompt
path.CharLimit = 64
path.SetValue(conf.Path)
user := textinput.NewModel()
user.Placeholder = "Username"
user.Prompt = blurredPrompt
user.CharLimit = 32
user.SetValue(conf.User)
pass := textinput.NewModel()
pass.Placeholder = "Password"
pass.Prompt = blurredPrompt
pass.CharLimit = 64
pass.EchoMode = textinput.EchoNone
loginButton := getButton("Login", false)
return loginModel{0, host, path, user, pass, loginButton, ""}
}
func (m mainModel) loginInit(conf *config) tea.Cmd {
if conf.Token != "" {
m.conn.Host = m.conf.Host
m.conn.Path = m.conf.Path
m.conn.User = m.conf.User
m.conn.Pass = ""
m.conn.Token = m.conf.Token
return doConnect(m.conf, m.conn)
}
return nil
}
func (m mainModel) loginView() string {
s := "Log in to The Lounge\n\n"
inputs := []string{
m.loginModel.hostInput.View(),
m.loginModel.pathInput.View(),
m.loginModel.userInput.View(),
m.loginModel.passInput.View(),
}
for i := 0; i < len(inputs); i++ {
s += inputs[i]
if i < len(inputs)-1 {
s += "\n"
}
}
s += "\n\n" + m.loginModel.loginButton + "\n"
if m.loginModel.message != "" {
s += "\n" + m.loginModel.message + "\n"
}
return s
}
func (m mainModel) loginUpdate(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
index := m.loginModel.index
refocus, index, button := updateIndex(msg, m.loginModel.index, 5, false, []int{4})
m.loginModel.index = index
if m.conn.Error != "" {
m.loginModel.message = m.conn.Error
}
if refocus {
inputs := []textinput.Model{
m.loginModel.hostInput,
m.loginModel.pathInput,
m.loginModel.userInput,
m.loginModel.passInput,
}
for i := 0; i < len(inputs); i++ {
if i == m.loginModel.index {
inputs[i] = inputFocus(inputs[i])
} else {
inputs[i] = inputBlur(inputs[i])
}
}
if m.loginModel.index == len(inputs) {
m.loginModel.loginButton = getButton("Login", true)
} else {
m.loginModel.loginButton = getButton("Login", false)
}
m.loginModel.hostInput = inputs[0]
m.loginModel.pathInput = inputs[1]
m.loginModel.userInput = inputs[2]
m.loginModel.passInput = inputs[3]
}
if button {
m.conn.Host = m.loginModel.hostInput.Value()
m.conn.Path = m.loginModel.pathInput.Value()
m.conn.User = m.loginModel.userInput.Value()
m.conn.Pass = m.loginModel.passInput.Value()
cmd = doConnect(m.conf, m.conn)
cmds = append(cmds, cmd)
}
m, cmd = loginUpdateInputs(msg, m)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func loginUpdateInputs(msg tea.Msg, m mainModel) (mainModel, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
inputs := []textinput.Model{
m.loginModel.hostInput,
m.loginModel.pathInput,
m.loginModel.userInput,
m.loginModel.passInput,
}
for i := 0; i < len(inputs); i++ {
inputs[i], cmd = inputs[i].Update(msg)
cmds = append(cmds, cmd)
}
m.loginModel.hostInput = inputs[0]
m.loginModel.pathInput = inputs[1]
m.loginModel.userInput = inputs[2]
m.loginModel.passInput = inputs[3]
return m, tea.Batch(cmds...)
}