This commit introduces a simple counter application using Bubble Tea. It includes the necessary Go module files and the main application logic for incrementing, decrementing, and quitting the application.
53 lines
740 B
Go
53 lines
740 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
type model struct {
|
|
counter int
|
|
}
|
|
|
|
func initialModel() model {
|
|
return model{counter: 0}
|
|
}
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
return nil
|
|
}
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, 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
|
|
}
|
|
|
|
func (m model) View() string {
|
|
return fmt.Sprintf(
|
|
"Counter: %d\n\nPress + / - to change\nPress q to quit\n",
|
|
m.counter,
|
|
)
|
|
}
|
|
|
|
func main() {
|
|
p := tea.NewProgram(initialModel())
|
|
if err := p.Start(); err != nil {
|
|
fmt.Println("Error:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|