| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package main |
| 22 |
|
| 23 |
import ( |
| 24 |
"fmt" |
| 25 |
"os" |
| 26 |
"os/exec" |
| 27 |
"runtime" |
| 28 |
|
| 29 |
"github.com/gdamore/tcell/v3" |
| 30 |
"github.com/gdamore/tcell/v3/color" |
| 31 |
) |
| 32 |
|
| 33 |
var defStyle tcell.Style |
| 34 |
|
| 35 |
func isCtrlRune(ev *tcell.EventKey, r string) bool { |
| 36 |
return ev.Key() == tcell.KeyRune && ev.Str() == r && ev.Modifiers()&tcell.ModCtrl != 0 |
| 37 |
} |
| 38 |
|
| 39 |
func keyboardProtocolName(p tcell.KeyProtocol) string { |
| 40 |
switch p { |
| 41 |
case tcell.LegacyKeyboard: |
| 42 |
return "Legacy" |
| 43 |
case tcell.KittyKeyboard: |
| 44 |
return "Kitty" |
| 45 |
case tcell.Win32Keyboard: |
| 46 |
return "Win32" |
| 47 |
case tcell.XTermKeyboard: |
| 48 |
return "XTerm" |
| 49 |
default: |
| 50 |
return "Unknown" |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) { |
| 55 |
rs := string(r) |
| 56 |
|
| 57 |
if y2 < y1 { |
| 58 |
y1, y2 = y2, y1 |
| 59 |
} |
| 60 |
if x2 < x1 { |
| 61 |
x1, x2 = x2, x1 |
| 62 |
} |
| 63 |
|
| 64 |
for col := x1; col <= x2; col++ { |
| 65 |
s.Put(col, y1, string(tcell.RuneHLine), style) |
| 66 |
s.Put(col, y2, string(tcell.RuneHLine), style) |
| 67 |
} |
| 68 |
for row := y1 + 1; row < y2; row++ { |
| 69 |
s.Put(x1, row, string(tcell.RuneVLine), style) |
| 70 |
s.Put(x2, row, string(tcell.RuneVLine), style) |
| 71 |
} |
| 72 |
if y1 != y2 && x1 != x2 { |
| 73 |
|
| 74 |
s.Put(x1, y1, string(tcell.RuneULCorner), style) |
| 75 |
s.Put(x2, y1, string(tcell.RuneURCorner), style) |
| 76 |
s.Put(x1, y2, string(tcell.RuneLLCorner), style) |
| 77 |
s.Put(x2, y2, string(tcell.RuneLRCorner), style) |
| 78 |
} |
| 79 |
for row := y1 + 1; row < y2; row++ { |
| 80 |
for col := x1 + 1; col < x2; col++ { |
| 81 |
s.Put(col, row, rs, style) |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func drawSelect(s tcell.Screen, x1, y1, x2, y2 int, sel bool) { |
| 87 |
|
| 88 |
if y2 < y1 { |
| 89 |
y1, y2 = y2, y1 |
| 90 |
} |
| 91 |
if x2 < x1 { |
| 92 |
x1, x2 = x2, x1 |
| 93 |
} |
| 94 |
for row := y1; row <= y2; row++ { |
| 95 |
for col := x1; col <= x2; col++ { |
| 96 |
str, style, width := s.Get(col, row) |
| 97 |
if style == tcell.StyleDefault { |
| 98 |
style = defStyle |
| 99 |
} |
| 100 |
style = style.Reverse(sel) |
| 101 |
s.Put(col, row, str, style) |
| 102 |
col += width - 1 |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func main() { |
| 110 |
|
| 111 |
shell := os.Getenv("SHELL") |
| 112 |
if shell == "" { |
| 113 |
if runtime.GOOS == "windows" { |
| 114 |
shell = "CMD.EXE" |
| 115 |
} else { |
| 116 |
shell = "/bin/sh" |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
s, e := tcell.NewScreen(tcell.OptAdvancedKeys(true)) |
| 121 |
if e != nil { |
| 122 |
fmt.Fprintf(os.Stderr, "%v\n", e) |
| 123 |
os.Exit(1) |
| 124 |
} |
| 125 |
if e := s.Init(); e != nil { |
| 126 |
fmt.Fprintf(os.Stderr, "%v\n", e) |
| 127 |
os.Exit(1) |
| 128 |
} |
| 129 |
|
| 130 |
s.SetTitle("Tcell Mouse Demonstration") |
| 131 |
defStyle = tcell.StyleDefault. |
| 132 |
Background(color.Reset). |
| 133 |
Foreground(color.Reset) |
| 134 |
s.SetStyle(defStyle) |
| 135 |
s.EnableMouse() |
| 136 |
s.EnablePaste() |
| 137 |
s.EnableFocus() |
| 138 |
s.Clear() |
| 139 |
|
| 140 |
posfmt := "Mouse: %d, %d " |
| 141 |
btnfmt := "Buttons: %s" |
| 142 |
keyfmt := "Keys: %s" |
| 143 |
pastefmt := "Paste: [%d] %s" |
| 144 |
focusfmt := "Focus: %s" |
| 145 |
termFmt := "Term: %s (%s)" |
| 146 |
kbdFmt := "Keyboard: %s" |
| 147 |
style := tcell.StyleDefault. |
| 148 |
Foreground(color.MidnightBlue).Background(color.LightCoral) |
| 149 |
keyDownStyle := style.Foreground(color.Green) |
| 150 |
keyUpStyle := style |
| 151 |
|
| 152 |
mx, my := -1, -1 |
| 153 |
ox, oy := -1, -1 |
| 154 |
bx, by := -1, -1 |
| 155 |
w, h := s.Size() |
| 156 |
lchar := '*' |
| 157 |
bstr := "" |
| 158 |
lks := "" |
| 159 |
lkey := "" |
| 160 |
kcnt := 0 |
| 161 |
pstr := "" |
| 162 |
ecnt := 0 |
| 163 |
pasting := false |
| 164 |
focus := true |
| 165 |
keyStyle := keyUpStyle |
| 166 |
|
| 167 |
for { |
| 168 |
drawBox(s, 1, 1, 42, 10, style, ' ') |
| 169 |
s.PutStrStyled(2, 2, "Press Ctrl-Q to Quit, C to clear.", style) |
| 170 |
s.PutStrStyled(2, 3, fmt.Sprintf(posfmt, mx, my), style) |
| 171 |
s.PutStrStyled(2, 4, fmt.Sprintf(btnfmt, bstr), style) |
| 172 |
s.PutStrStyled(2, 5, fmt.Sprintf(keyfmt, ""), style) |
| 173 |
s.PutStrStyled(8, 5, lks, keyStyle) |
| 174 |
|
| 175 |
ps := pstr |
| 176 |
if len(ps) > 26 { |
| 177 |
ps = "..." + ps[len(ps)-24:] |
| 178 |
} |
| 179 |
s.PutStrStyled(2, 6, fmt.Sprintf(pastefmt, len(pstr), ps), style) |
| 180 |
|
| 181 |
fstr := "false" |
| 182 |
if focus { |
| 183 |
fstr = "true" |
| 184 |
} |
| 185 |
s.PutStrStyled(2, 7, fmt.Sprintf(focusfmt, fstr), style) |
| 186 |
|
| 187 |
n, v := s.Terminal() |
| 188 |
s.PutStrStyled(2, 8, fmt.Sprintf(termFmt, n, v), style) |
| 189 |
s.PutStrStyled(2, 9, fmt.Sprintf(kbdFmt, keyboardProtocolName(s.KeyboardProtocol())), style) |
| 190 |
|
| 191 |
s.Show() |
| 192 |
bstr = "" |
| 193 |
ev := <-s.EventQ() |
| 194 |
st := tcell.StyleDefault.Background(color.Red) |
| 195 |
up := tcell.StyleDefault. |
| 196 |
Background(color.Blue). |
| 197 |
Foreground(color.Black) |
| 198 |
w, h = s.Size() |
| 199 |
|
| 200 |
|
| 201 |
if ox >= 0 && oy >= 0 && bx >= 0 { |
| 202 |
drawSelect(s, ox, oy, bx, by, false) |
| 203 |
} |
| 204 |
|
| 205 |
switch ev := ev.(type) { |
| 206 |
case *tcell.EventResize: |
| 207 |
s.Sync() |
| 208 |
s.Put(w-1, h-1, "R", st) |
| 209 |
case *tcell.EventKey: |
| 210 |
s.Put(w-2, h-2, ev.Str(), st) |
| 211 |
if !ev.Pressed() { |
| 212 |
pstr = "" |
| 213 |
lks = ev.Name() |
| 214 |
keyStyle = keyUpStyle |
| 215 |
for x := range w - 1 { |
| 216 |
s.Put(x, h-1, " ", tcell.StyleDefault) |
| 217 |
} |
| 218 |
s.Put(w-1, h-1, "U", st) |
| 219 |
continue |
| 220 |
} |
| 221 |
keyStyle = keyDownStyle |
| 222 |
if ev.Name() == lkey { |
| 223 |
kcnt++ |
| 224 |
} else { |
| 225 |
lkey = ev.Name() |
| 226 |
kcnt = 1 |
| 227 |
} |
| 228 |
if pasting { |
| 229 |
s.Put(w-1, h-1, "P", st) |
| 230 |
if ev.Key() == tcell.KeyRune { |
| 231 |
pstr = pstr + ev.Str() |
| 232 |
} else { |
| 233 |
pstr = pstr + "\ufffd" |
| 234 |
} |
| 235 |
lks = "" |
| 236 |
continue |
| 237 |
} |
| 238 |
pstr = "" |
| 239 |
s.Put(w-1, h-1, "K", st) |
| 240 |
if ev.Key() == tcell.KeyEscape { |
| 241 |
ecnt++ |
| 242 |
if ecnt > 1 { |
| 243 |
s.Fini() |
| 244 |
os.Exit(0) |
| 245 |
} |
| 246 |
} else if ev.Key() == tcell.KeyCtrlL || isCtrlRune(ev, "l") { |
| 247 |
s.Sync() |
| 248 |
} else if ev.Key() == tcell.KeyCtrlQ || isCtrlRune(ev, "q") { |
| 249 |
s.Fini() |
| 250 |
os.Exit(0) |
| 251 |
} else if ev.Key() == tcell.KeyCtrlZ || isCtrlRune(ev, "z") { |
| 252 |
|
| 253 |
if err := s.Suspend(); err == nil { |
| 254 |
fmt.Printf("Executing shell (%s -l)...\n", shell) |
| 255 |
fmt.Printf("Exit the shell to return to the demo.\n") |
| 256 |
c := exec.Command(shell, "-l") |
| 257 |
c.Stdin = os.Stdin |
| 258 |
c.Stdout = os.Stdout |
| 259 |
c.Stderr = os.Stderr |
| 260 |
c.Run() |
| 261 |
if err := s.Resume(); err != nil { |
| 262 |
panic("failed to resume: " + err.Error()) |
| 263 |
} |
| 264 |
} |
| 265 |
} else { |
| 266 |
ecnt = 0 |
| 267 |
if ev.Str() == "C" || ev.Str() == "c" { |
| 268 |
s.Clear() |
| 269 |
} |
| 270 |
} |
| 271 |
if ks := fmt.Sprintf("K%d", kcnt); w >= len(ks) { |
| 272 |
s.PutStrStyled(w-len(ks), h-1, ks, st) |
| 273 |
} else { |
| 274 |
s.PutStrStyled(0, h-1, ks, st) |
| 275 |
} |
| 276 |
lks = ev.Name() |
| 277 |
case *tcell.EventPaste: |
| 278 |
pasting = ev.Start() |
| 279 |
if pasting { |
| 280 |
pstr = "" |
| 281 |
} |
| 282 |
case *tcell.EventMouse: |
| 283 |
x, y := ev.Position() |
| 284 |
button := ev.Buttons() |
| 285 |
mods := ev.Modifiers() |
| 286 |
if mods&tcell.ModShift != 0 { |
| 287 |
bstr += " Shift" |
| 288 |
} |
| 289 |
if mods&tcell.ModCtrl != 0 { |
| 290 |
bstr += " Ctrl" |
| 291 |
} |
| 292 |
if mods&tcell.ModAlt != 0 { |
| 293 |
bstr += " Alt" |
| 294 |
} |
| 295 |
if mods&tcell.ModMeta != 0 { |
| 296 |
bstr += " Meta" |
| 297 |
} |
| 298 |
for i := uint(0); i < 8; i++ { |
| 299 |
if int(button)&(1<<i) != 0 { |
| 300 |
bstr += fmt.Sprintf(" Button%d", i+1) |
| 301 |
} |
| 302 |
} |
| 303 |
if button&tcell.WheelUp != 0 { |
| 304 |
bstr += " WheelUp" |
| 305 |
} |
| 306 |
if button&tcell.WheelDown != 0 { |
| 307 |
bstr += " WheelDown" |
| 308 |
} |
| 309 |
if button&tcell.WheelLeft != 0 { |
| 310 |
bstr += " WheelLeft" |
| 311 |
} |
| 312 |
if button&tcell.WheelRight != 0 { |
| 313 |
bstr += " WheelRight" |
| 314 |
} |
| 315 |
|
| 316 |
button &= tcell.ButtonMask(0xff) |
| 317 |
ch := '*' |
| 318 |
|
| 319 |
if button != tcell.ButtonNone && ox < 0 { |
| 320 |
ox, oy = x, y |
| 321 |
} |
| 322 |
theme := []color.Color{ |
| 323 |
color.Gray, |
| 324 |
color.Red, |
| 325 |
color.Lime, |
| 326 |
color.Yellow, |
| 327 |
color.Fuchsia, |
| 328 |
color.Blue, |
| 329 |
color.Aqua, |
| 330 |
color.Silver, |
| 331 |
} |
| 332 |
switch ev.Buttons() { |
| 333 |
case tcell.ButtonNone: |
| 334 |
if ox >= 0 { |
| 335 |
bg := theme[lchar%8] |
| 336 |
fg := color.Black |
| 337 |
drawBox(s, ox, oy, x, y, |
| 338 |
up.Background(bg).Foreground(fg), |
| 339 |
lchar) |
| 340 |
ox, oy = -1, -1 |
| 341 |
bx, by = -1, -1 |
| 342 |
} |
| 343 |
case tcell.Button1: |
| 344 |
ch = '1' |
| 345 |
case tcell.Button2: |
| 346 |
ch = '2' |
| 347 |
case tcell.Button3: |
| 348 |
ch = '3' |
| 349 |
case tcell.Button4: |
| 350 |
ch = '4' |
| 351 |
case tcell.Button5: |
| 352 |
ch = '5' |
| 353 |
case tcell.Button6: |
| 354 |
ch = '6' |
| 355 |
case tcell.Button7: |
| 356 |
ch = '7' |
| 357 |
case tcell.Button8: |
| 358 |
ch = '8' |
| 359 |
default: |
| 360 |
ch = '*' |
| 361 |
|
| 362 |
} |
| 363 |
if button != tcell.ButtonNone { |
| 364 |
bx, by = x, y |
| 365 |
} |
| 366 |
lchar = ch |
| 367 |
s.Put(w-1, h-1, "M", st) |
| 368 |
mx, my = x, y |
| 369 |
case *tcell.EventFocus: |
| 370 |
focus = ev.Focused |
| 371 |
default: |
| 372 |
s.Put(w-1, h-1, "X", st) |
| 373 |
} |
| 374 |
|
| 375 |
if ox >= 0 && bx >= 0 { |
| 376 |
drawSelect(s, ox, oy, bx, by, true) |
| 377 |
} |
| 378 |
} |
| 379 |
} |