Skip to content
Myr

main.go

This file

Path

examples/mouse/main.go

44 lines · 834 bytes · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package main23// A simple program that opens the alternate screen buffer and displays mouse4// coordinates and events.56import (7	"log"89	tea "charm.land/bubbletea/v2"10)1112func main() {13	p := tea.NewProgram(model{})14	if _, err := p.Run(); err != nil {15		log.Fatal(err)16	}17}1819type model struct{}2021func (m model) Init() tea.Cmd {22	return nil23}2425func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {26	switch msg := msg.(type) {27	case tea.KeyPressMsg:28		if s := msg.String(); s == "ctrl+c" || s == "q" || s == "esc" {29			return m, tea.Quit30		}3132	case tea.MouseMsg:33		mouse := msg.Mouse()34		return m, tea.Printf("(X: %d, Y: %d) %s", mouse.X, mouse.Y, mouse)35	}3637	return m, nil38}3940func (m model) View() tea.View {41	v := tea.NewView("Do mouse stuff. When you're done press q to quit.\n")42	v.MouseMode = tea.MouseModeAllMotion43	return v44}