package main // A simple example demonstrating the use of multiple text input components // from the Bubbles component library. import ( "encoding/json" "fmt" "os" "strings" "github.com/charmbracelet/bubbles/cursor" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" ) var ( focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205")) blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240")) cursorStyle = focusedStyle.Copy() noStyle = lipgloss.NewStyle() helpStyle = blurredStyle.Copy() cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244")) focusedButton = focusedStyle.Copy().Render("[ Submit ]") blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit")) ) // new stuff type formData struct { Username string Email string SSH string } func save(data formData) { d, err := json.Marshal(data) //encode the data as json if err != nil { fmt.Printf("Error when saving to disk: %s\n", err) os.Exit(1) } os.WriteFile("/home/nyet/Documents/go/test/temp.txt", d, 0644) } type model struct { focusIndex int inputs []textinput.Model cursorMode cursor.Mode } func initialModel() model { m := model{ inputs: make([]textinput.Model, 3), } var t textinput.Model for i := range m.inputs { t = textinput.New() t.Cursor.Style = cursorStyle t.CharLimit = 32 switch i { case 0: t.Placeholder = "Username" t.Focus() t.PromptStyle = focusedStyle t.TextStyle = focusedStyle t.CharLimit = 32 case 1: t.Placeholder = "Email" t.CharLimit = 64 case 2: t.Placeholder = "ssh key" t.CharLimit = 1000 } m.inputs[i] = t } return m } func (m model) Init() tea.Cmd { return textinput.Blink } func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "ctrl+c", "esc": return m, tea.Quit // Change cursor mode case "ctrl+r": m.cursorMode++ if m.cursorMode > cursor.CursorHide { m.cursorMode = cursor.CursorBlink } cmds := make([]tea.Cmd, len(m.inputs)) for i := range m.inputs { cmds[i] = m.inputs[i].Cursor.SetMode(m.cursorMode) } return m, tea.Batch(cmds...) // Set focus to next input case "tab", "shift+tab", "enter", "up", "down": s := msg.String() // Did the user press enter while the submit button was focused? // If so, exit. if s == "enter" && m.focusIndex == len(m.inputs) { data := formData{ Username: string(m.inputs[0].Value()), Email: string(m.inputs[1].Value()), SSH: string(m.inputs[2].Value()), } save(data) return m, tea.Quit } // Cycle indexes 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) } cmds := make([]tea.Cmd, len(m.inputs)) for i := 0; i <= len(m.inputs)-1; i++ { if i == m.focusIndex { // Set focused state cmds[i] = m.inputs[i].Focus() m.inputs[i].PromptStyle = focusedStyle m.inputs[i].TextStyle = focusedStyle continue } // Remove focused state m.inputs[i].Blur() m.inputs[i].PromptStyle = noStyle m.inputs[i].TextStyle = noStyle } return m, tea.Batch(cmds...) } } // Handle character input and blinking cmd := m.updateInputs(msg) return m, cmd } func (m *model) updateInputs(msg tea.Msg) tea.Cmd { cmds := make([]tea.Cmd, len(m.inputs)) // Only text inputs with Focus() set will respond, so it's safe to simply // update all of them here without any further logic. for i := range m.inputs { m.inputs[i], cmds[i] = m.inputs[i].Update(msg) } return tea.Batch(cmds...) } func (m model) View() string { var b strings.Builder for i := range m.inputs { b.WriteString(m.inputs[i].View()) //fmt.Printf("contets: %v", m.inputs[i]) //sets up labeled prompt. Currently they're to the left of where they need to be. Likely have to replace the Prompt value in tea.Model switch i { case 0: //tea.Model case 1: //b.WriteString("Email\n") case 2: //b.WriteString("SSH Key\n") } if i < len(m.inputs)-1 { b.WriteRune('\n') } } button := &blurredButton if m.focusIndex == len(m.inputs) { button = &focusedButton } fmt.Fprintf(&b, "\n\n%s\n\n", *button) b.WriteString(helpStyle.Render("cursor mode is ")) b.WriteString(cursorModeHelpStyle.Render(m.cursorMode.String())) b.WriteString(helpStyle.Render(" (ctrl+r to change style)")) // not what's neeede b.WriteString("test") return b.String() } func main() { if _, err := tea.NewProgram(initialModel()).Run(); err != nil { fmt.Printf("could not start program: %s\n", err) os.Exit(1) } }