bubbletea/examples/mouse/main.go

60 lines
1013 B
Go
Raw 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 (
"fmt"
"log"
tea "github.com/charmbracelet/bubbletea"
)
func main() {
p := tea.NewProgram(model{})
2020-06-22 20:30:16 -04:00
p.EnterAltScreen()
defer p.ExitAltScreen()
p.EnableMouseAllMotion()
defer p.DisableMouseAllMotion()
if err := p.Start(); err != nil {
log.Fatal(err)
}
}
type model struct {
init bool
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:
if msg.Type == tea.KeyCtrlC || (msg.Type == tea.KeyRune && msg.Rune == 'q') {
return m, tea.Quit
}
case tea.MouseMsg:
m.init = true
m.mouseEvent = tea.MouseEvent(msg)
}
return m, nil
}
func (m model) View() string {
2020-06-22 20:30:16 -04:00
s := "Do mouse stuff. When you're done press q to quit.\n\n"
if m.init {
e := m.mouseEvent
s += fmt.Sprintf("(X: %d, Y: %d) %s", e.X, e.Y, e)
}
return s
}