bubbletea/examples/mouse/main.go

46 lines
830 B
Go
Raw Permalink Normal View History

2020-06-22 20:30:16 -04:00
package main
// A simple program that opens the alternate screen buffer and displays mouse
// coordinates and events.
2020-06-22 20:30:16 -04:00
import (
"log"
tea "github.com/charmbracelet/bubbletea"
)
func main() {
p := tea.NewProgram(model{}, tea.WithMouseAllMotion())
if _, err := p.Run(); err != nil {
2020-06-22 20:30:16 -04:00
log.Fatal(err)
}
}
type model struct {
mouseEvent tea.MouseEvent
}
func (m model) Init() tea.Cmd {
return nil
2020-06-22 20:30:16 -04:00
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2020-06-22 20:30:16 -04:00
switch msg := msg.(type) {
case tea.KeyMsg:
2021-05-01 09:28:58 -04:00
if s := msg.String(); s == "ctrl+c" || s == "q" || s == "esc" {
2020-06-22 20:30:16 -04:00
return m, tea.Quit
}
case tea.MouseMsg:
return m, tea.Printf("(X: %d, Y: %d) %s", msg.X, msg.Y, tea.MouseEvent(msg))
2020-06-22 20:30:16 -04:00
}
return m, nil
}
func (m model) View() string {
s := "Do mouse stuff. When you're done press q to quit.\n"
2020-06-22 20:30:16 -04:00
return s
}