2020-06-22 20:30:16 -04:00
|
|
|
package main
|
|
|
|
|
2020-10-14 11:51:04 -04:00
|
|
|
// 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() {
|
2021-05-19 20:52:01 -04:00
|
|
|
p := tea.NewProgram(model{}, tea.WithAltScreen(), tea.WithMouseAllMotion())
|
|
|
|
if err := p.Start(); err != nil {
|
2020-06-22 20:30:16 -04:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
init bool
|
|
|
|
mouseEvent tea.MouseEvent
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:33:17 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
2021-05-19 20:52:01 -04:00
|
|
|
return nil
|
2020-06-22 20:30:16 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:33:17 -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:
|
|
|
|
m.init = true
|
|
|
|
m.mouseEvent = tea.MouseEvent(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:33:17 -04:00
|
|
|
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
|
|
|
|
}
|