Refactor: Add text input functionality

Replaces the counter with a text input field for user interaction. This
change leverages the `textinput` bubble to provide a more interactive
experience within the TUI. The `go.mod` and `go.sum` files have been
updated to include the necessary dependency.
This commit is contained in:
vaibhav
2026-01-14 04:13:52 +05:30
parent f06f418651
commit 761961b452
4 changed files with 20 additions and 16 deletions

33
main.go
View File

@@ -4,49 +4,50 @@ import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type model struct {
counter int
textInput textinput.Model
}
func initialModel() model {
return model{counter: 0}
ti := textinput.New()
ti.Placeholder = "Search"
ti.Width = 30
ti.CharLimit = 30
ti.Focus()
return model{
textInput: ti,
}
}
func (m model) Init() tea.Cmd {
return nil
return textinput.Blink
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "q", "ctrl+c":
return m, tea.Quit
case "+":
m.counter++
case "-":
m.counter--
}
}
return m, nil
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m model) View() string {
return fmt.Sprintf(
"Counter: %d\n\nPress + / - to change\nPress q to quit\n",
m.counter,
)
return fmt.Sprint(m.textInput.View())
}
func main() {
p := tea.NewProgram(initialModel())
if err := p.Start(); err != nil {
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}