File: mouse.go

package info (click to toggle)
golang-sourcehut-rockorager-tcell-term 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,216 kB
  • sloc: makefile: 2
file content (109 lines) | stat: -rw-r--r-- 2,518 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tcellterm

import (
	"fmt"

	"github.com/gdamore/tcell/v2"
)

func (vt *VT) handleMouse(ev *tcell.EventMouse) string {
	if vt.mode&mouseButtons == 0 && vt.mode&mouseDrag == 0 && vt.mode&mouseMotion == 0 && vt.mode&mouseSGR == 0 {
		if vt.mode&altScroll != 0 && vt.mode&smcup != 0 {
			// Translate wheel motion into arrows up and down
			// 3x rows
			if ev.Buttons()&tcell.WheelUp != 0 {
				vt.pty.WriteString(info.KeyUp)
				vt.pty.WriteString(info.KeyUp)
				vt.pty.WriteString(info.KeyUp)
			}
			if ev.Buttons()&tcell.WheelDown != 0 {
				vt.pty.WriteString(info.KeyDown)
				vt.pty.WriteString(info.KeyDown)
				vt.pty.WriteString(info.KeyDown)
			}
		}
		return ""
	}
	// Return early if we aren't reporting motion or drag events
	if vt.mode&mouseButtons != 0 && vt.mouseBtn == ev.Buttons() {
		// motion or drag
		return ""
	}

	if vt.mode&mouseDrag != 0 && vt.mouseBtn == tcell.ButtonNone && ev.Buttons() == tcell.ButtonNone {
		// Motion event
		return ""
	}

	// Encode the button
	var b int
	if ev.Buttons()&tcell.Button1 != 0 {
		b += 0
	}
	if ev.Buttons()&tcell.Button3 != 0 {
		b += 1
	}
	if ev.Buttons()&tcell.Button2 != 0 {
		b += 2
	}
	if ev.Buttons() == tcell.ButtonNone {
		b += 3
	}
	if ev.Buttons()&tcell.WheelUp != 0 {
		b += 0 + 64
	}
	if ev.Buttons()&tcell.WheelDown != 0 {
		b += 1 + 64
	}
	if ev.Modifiers()&tcell.ModShift != 0 {
		b += 4
	}
	if ev.Modifiers()&tcell.ModAlt != 0 {
		b += 8
	}
	if ev.Modifiers()&tcell.ModCtrl != 0 {
		b += 16
	}

	if vt.mode&mouseButtons == 0 && vt.mouseBtn != tcell.ButtonNone && ev.Buttons() != tcell.ButtonNone {
		// drag event
		b += 32
	}

	col, row := ev.Position()

	if vt.mode&mouseSGR != 0 {
		switch {
		case ev.Buttons()&tcell.WheelUp != 0:
			return fmt.Sprintf("\x1b[<%d;%d;%dM", b, col+1, row+1)

		case ev.Buttons()&tcell.WheelDown != 0:
			return fmt.Sprintf("\x1b[<%d;%d;%dM", b, col+1, row+1)

		case ev.Buttons() == tcell.ButtonNone && vt.mouseBtn != tcell.ButtonNone:
			// Button was in, and now it's not
			var button int
			switch vt.mouseBtn {
			case tcell.Button1:
				button = 0
			case tcell.Button3:
				button = 1
			case tcell.Button2:
				button = 2
			}
			vt.mouseBtn = ev.Buttons()
			return fmt.Sprintf("\x1b[<%d;%d;%dm", button, col+1, row+1)

		default:
			vt.mouseBtn = ev.Buttons()
			return fmt.Sprintf("\x1b[<%d;%d;%dM", b, col+1, row+1)
		}
	}

	encodedCol := 32 + col + 1
	encodedRow := 32 + row + 1
	b += 32

	vt.mouseBtn = ev.Buttons()
	return fmt.Sprintf("\x1b[M%c%c%c", b, encodedCol, encodedRow)
}