| 1 |
package ansi |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
type MouseButton byte |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
const ( |
| 28 |
MouseNone MouseButton = iota |
| 29 |
MouseButton1 |
| 30 |
MouseButton2 |
| 31 |
MouseButton3 |
| 32 |
MouseButton4 |
| 33 |
MouseButton5 |
| 34 |
MouseButton6 |
| 35 |
MouseButton7 |
| 36 |
MouseButton8 |
| 37 |
MouseButton9 |
| 38 |
MouseButton10 |
| 39 |
MouseButton11 |
| 40 |
|
| 41 |
MouseLeft = MouseButton1 |
| 42 |
MouseMiddle = MouseButton2 |
| 43 |
MouseRight = MouseButton3 |
| 44 |
MouseWheelUp = MouseButton4 |
| 45 |
MouseWheelDown = MouseButton5 |
| 46 |
MouseWheelLeft = MouseButton6 |
| 47 |
MouseWheelRight = MouseButton7 |
| 48 |
MouseBackward = MouseButton8 |
| 49 |
MouseForward = MouseButton9 |
| 50 |
MouseRelease = MouseNone |
| 51 |
) |
| 52 |
|
| 53 |
var mouseButtons = map[MouseButton]string{ |
| 54 |
MouseNone: "none", |
| 55 |
MouseLeft: "left", |
| 56 |
MouseMiddle: "middle", |
| 57 |
MouseRight: "right", |
| 58 |
MouseWheelUp: "wheelup", |
| 59 |
MouseWheelDown: "wheeldown", |
| 60 |
MouseWheelLeft: "wheelleft", |
| 61 |
MouseWheelRight: "wheelright", |
| 62 |
MouseBackward: "backward", |
| 63 |
MouseForward: "forward", |
| 64 |
MouseButton10: "button10", |
| 65 |
MouseButton11: "button11", |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
func (b MouseButton) String() string { |
| 70 |
return mouseButtons[b] |
| 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 |
func EncodeMouseButton(b MouseButton, motion, shift, alt, ctrl bool) (m byte) { |
| 98 |
|
| 99 |
const ( |
| 100 |
bitShift = 0b0000_0100 |
| 101 |
bitAlt = 0b0000_1000 |
| 102 |
bitCtrl = 0b0001_0000 |
| 103 |
bitMotion = 0b0010_0000 |
| 104 |
bitWheel = 0b0100_0000 |
| 105 |
bitAdd = 0b1000_0000 |
| 106 |
|
| 107 |
bitsMask = 0b0000_0011 |
| 108 |
) |
| 109 |
|
| 110 |
if b == MouseNone { |
| 111 |
m = bitsMask |
| 112 |
} else if b >= MouseLeft && b <= MouseRight { |
| 113 |
m = byte(b - MouseLeft) |
| 114 |
} else if b >= MouseWheelUp && b <= MouseWheelRight { |
| 115 |
m = byte(b - MouseWheelUp) |
| 116 |
m |= bitWheel |
| 117 |
} else if b >= MouseBackward && b <= MouseButton11 { |
| 118 |
m = byte(b - MouseBackward) |
| 119 |
m |= bitAdd |
| 120 |
} else { |
| 121 |
m = 0xff |
| 122 |
} |
| 123 |
|
| 124 |
if shift { |
| 125 |
m |= bitShift |
| 126 |
} |
| 127 |
if alt { |
| 128 |
m |= bitAlt |
| 129 |
} |
| 130 |
if ctrl { |
| 131 |
m |= bitCtrl |
| 132 |
} |
| 133 |
if motion { |
| 134 |
m |= bitMotion |
| 135 |
} |
| 136 |
|
| 137 |
return m |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
const x10Offset = 32 |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
func MouseX10(b byte, x, y int) string { |
| 151 |
return "\x1b[M" + string(b+x10Offset) + string(byte(x)+x10Offset+1) + string(byte(y)+x10Offset+1) |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
func MouseSgr(b byte, x, y int, release bool) string { |
| 161 |
s := 'M' |
| 162 |
if release { |
| 163 |
s = 'm' |
| 164 |
} |
| 165 |
if x < 0 { |
| 166 |
x = -x |
| 167 |
} |
| 168 |
if y < 0 { |
| 169 |
y = -y |
| 170 |
} |
| 171 |
return fmt.Sprintf("\x1b[<%d;%d;%d%c", b, x+1, y+1, s) |
| 172 |
} |