package steps import ( "miasma-installer/tui/styles" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" ) type UserModel struct { Username string Password string usernameInput textinput.Model passwordInput textinput.Model confirmInput textinput.Model focusIndex int Finished bool } func NewUserModel() UserModel { ui := textinput.New() ui.Placeholder = "username" ui.Focus() ui.CharLimit = 32 pi := textinput.New() pi.Placeholder = "password" pi.EchoMode = textinput.EchoPassword ci := textinput.New() ci.Placeholder = "confirm password" ci.EchoMode = textinput.EchoPassword return UserModel{ usernameInput: ui, passwordInput: pi, confirmInput: ci, } } func (m UserModel) Init() tea.Cmd { return textinput.Blink } func (m UserModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { if m.Finished { return m, nil } switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "enter": if m.focusIndex == 0 && m.usernameInput.Value() != "" { m.focusIndex = 1 m.usernameInput.Blur() return m, m.passwordInput.Focus() } else if m.focusIndex == 1 && m.passwordInput.Value() != "" { m.focusIndex = 2 m.passwordInput.Blur() return m, m.confirmInput.Focus() } else if m.focusIndex == 2 { if m.passwordInput.Value() == m.confirmInput.Value() && m.passwordInput.Value() != "" { m.Username = m.usernameInput.Value() m.Password = m.passwordInput.Value() m.Finished = true return m, nil } } case "tab", "shift+tab": m.focusIndex = (m.focusIndex + 1) % 3 cmds := make([]tea.Cmd, 3) if m.focusIndex == 0 { cmds[0] = m.usernameInput.Focus() m.passwordInput.Blur() m.confirmInput.Blur() } else if m.focusIndex == 1 { cmds[1] = m.passwordInput.Focus() m.usernameInput.Blur() m.confirmInput.Blur() } else { cmds[2] = m.confirmInput.Focus() m.usernameInput.Blur() m.passwordInput.Blur() } return m, tea.Batch(cmds...) } } var cmd tea.Cmd if m.focusIndex == 0 { m.usernameInput, cmd = m.usernameInput.Update(msg) } else if m.focusIndex == 1 { m.passwordInput, cmd = m.passwordInput.Update(msg) } else { m.confirmInput, cmd = m.confirmInput.Update(msg) } return m, cmd } func (m UserModel) View() string { s := styles.TitleStyle.Render("User Account") s += "\n\n" s += "Create your user account:\n\n" s += "Username:\n" s += m.usernameInput.View() + "\n\n" s += "Password:\n" s += m.passwordInput.View() + "\n\n" s += "Confirm Password:\n" s += m.confirmInput.View() + "\n\n" if m.passwordInput.Value() != "" && m.confirmInput.Value() != "" { if m.passwordInput.Value() != m.confirmInput.Value() { s += styles.HelpStyle.Render("Passwords do not match\n") } } s += styles.HelpStyle.Render("[Tab] Switch fields [Enter] Continue") return styles.MainStyle.Render(s) }