Source File
mouse.go
Belonging Package
github.com/charmbracelet/ultraviolet
package uvimport ()// MouseMode represents the mouse tracking mode for the terminal.type MouseMode uint8// Mouse tracking modes.//// These determine which mouse events the terminal reports.const (MouseModeNone MouseMode = iota // Disable mouse tracking.MouseModePress // Press only (DEC mode 9). Reports button press events.MouseModeClick // Click tracking (DEC mode 1000). Reports button press and release.MouseModeDrag // Drag tracking (DEC mode 1002). Reports press, release, and drag.MouseModeMotion // Motion tracking (DEC mode 1003). Reports all mouse events including motion.)// MouseEncoding represents the encoding used for mouse events.type MouseEncoding uint8// Mouse encodings.//// These determine how mouse coordinates and buttons are encoded in the// terminal's escape sequences. The encoding is only meaningful when mouse// tracking is enabled via [MouseMode].const (MouseEncodingLegacy MouseEncoding = iota // Legacy X10-compatible encoding. Coordinates limited to 223.MouseEncodingSGR // SGR encoding (DEC mode 1006). No coordinate limit, distinguishes press/release.MouseEncodingSGRPixel // SGR-pixel encoding (DEC mode 1016). Reports pixel coordinates.// TODO: support these additional encodings in the future.// MouseEncodingUTF8 // UTF-8 encoding (DEC mode 1005). Coordinates limited to 223.// MouseEncodingUrxvt // urxvt encoding (DEC mode 1015). No coordinate limit.// MouseEncodingSGRPixel // SGR-pixel encoding (DEC mode 1016). Reports pixel coordinates.)// MouseButton represents the button that was pressed during a mouse message.type MouseButton = ansi.MouseButton// Mouse event buttons//// This is based on X11 mouse button codes.//// 1 = left button// 2 = middle button (pressing the scroll wheel)// 3 = right button// 4 = turn scroll wheel up// 5 = turn scroll wheel down// 6 = push scroll wheel left// 7 = push scroll wheel right// 8 = 4th button (aka browser backward button)// 9 = 5th button (aka browser forward button)// 10// 11//// Other buttons are not supported.const (MouseNone = ansi.MouseNoneMouseLeft = ansi.MouseLeftMouseMiddle = ansi.MouseMiddleMouseRight = ansi.MouseRightMouseWheelUp = ansi.MouseWheelUpMouseWheelDown = ansi.MouseWheelDownMouseWheelLeft = ansi.MouseWheelLeftMouseWheelRight = ansi.MouseWheelRightMouseBackward = ansi.MouseBackwardMouseForward = ansi.MouseForwardMouseButton10 = ansi.MouseButton10MouseButton11 = ansi.MouseButton11)// Mouse represents a Mouse message. Use [MouseEvent] to represent all mouse// messages.//// The X and Y coordinates are zero-based, with (0,0) being the upper left// corner of the terminal. When using [MouseEncodingSGRPixel] (DEC mode 1016),// X and Y are in pixel coordinates; otherwise they are in cell coordinates.//// // Catch all mouse events// switch Event := Event.(type) {// case MouseEvent:// m := Event.Mouse()// fmt.Println("Mouse event:", m.X, m.Y, m)// }//// // Only catch mouse click events// switch Event := Event.(type) {// case MouseClickEvent:// fmt.Println("Mouse click event:", Event.X, Event.Y, Event)// }type Mouse struct {X, Y intButton MouseButtonMod KeyMod}// String returns a string representation of the mouse message.func ( Mouse) () ( string) {if .Mod.Contains(ModCtrl) {+= "ctrl+"}if .Mod.Contains(ModAlt) {+= "alt+"}if .Mod.Contains(ModShift) {+= "shift+"}:= .Button.String()if == "" {+= "unknown"} else if != "none" { // motion events don't have a button+=}return}// MousePixelToCell converts a mouse event with pixel coordinates to cell// coordinates.//// This is only meaningful when using [MouseEncodingSGRPixel] encoding, which// reports mouse coordinates in pixels rather than cell units. The conversion// is based on the terminal's reported pixel dimensions and cell dimensions.//// On Windows, or other platforms where [Terminal.GetWinsize] doesn't report// pixel dimensions, you can query for the terminal window pixel dimensions via// [ansi.WindowOp](4) and get back a [PixelSizeEvent] with the pixel// dimensions. You can then construct a [Winsize] struct with the pixel// dimensions and the cell dimensions from [Terminal.GetWinsize] and pass it to// this function.func ( Mouse, *Winsize) Mouse {var , intif .Xpixel > 0 {= .X * int(.Col) / int(.Xpixel)}if .Ypixel > 0 {= .Y * int(.Row) / int(.Ypixel)}return Mouse{X: ,Y: ,Button: .Button,Mod: .Mod,}}
The pages are generated with Golds v0.8.7. (GOOS=linux GOARCH=amd64)