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

1
go.mod
View File

@@ -3,6 +3,7 @@ module github.com/vvaibhavv11/player
go 1.25.0
require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/bubbles v0.21.0 // indirect
github.com/charmbracelet/bubbletea v1.3.10 // indirect

2
go.sum
View File

@@ -1,3 +1,5 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs=

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)
}
}

BIN
player Executable file

Binary file not shown.