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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
|
// Copyright 2020 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocui
import (
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
// We probably don't want this being a global variable for YOLO for now
var Screen tcell.Screen
// oldStyle is a representation of how a cell would be styled when we were using termbox
type oldStyle struct {
fg Attribute
bg Attribute
outputMode OutputMode
}
var runeReplacements = map[rune]string{
'┌': "+",
'┐': "+",
'└': "+",
'┘': "+",
'╭': "+",
'╮': "+",
'╰': "+",
'╯': "+",
'─': "-",
'═': "-",
'║': "|",
'╔': "+",
'╗': "+",
'╚': "+",
'╝': "+",
// using a hyphen here actually looks weird.
// We see these characters when in portrait mode
'╶': " ",
'╴': " ",
'┴': "+",
'┬': "+",
'╷': "|",
'├': "+",
'│': "|",
'▼': "v",
'►': ">",
'▲': "^",
'◄': "<",
}
// tcellInit initializes tcell screen for use.
func (g *Gui) tcellInit(runeReplacements map[rune]string) error {
runewidth.DefaultCondition.EastAsianWidth = false
tcell.SetEncodingFallback(tcell.EncodingFallbackASCII)
if s, e := tcell.NewScreen(); e != nil {
return e
} else if e = s.Init(); e != nil {
return e
} else {
registerRuneFallbacks(s, runeReplacements)
g.screen = s
Screen = s
return nil
}
}
func registerRuneFallbacks(s tcell.Screen, additional map[rune]string) {
for before, after := range runeReplacements {
s.RegisterRuneFallback(before, after)
}
for before, after := range additional {
s.RegisterRuneFallback(before, after)
}
}
// tcellInitSimulation initializes tcell screen for use.
func (g *Gui) tcellInitSimulation(width int, height int) error {
s := tcell.NewSimulationScreen("")
if e := s.Init(); e != nil {
return e
} else {
g.screen = s
Screen = s
// setting to a larger value than the typical terminal size
// so that during a test we're more likely to see an item to select in a view.
s.SetSize(width, height)
s.Sync()
return nil
}
}
// tcellSetCell sets the character cell at a given location to the given
// content (rune) and attributes using provided OutputMode
func tcellSetCell(x, y int, ch rune, fg, bg Attribute, outputMode OutputMode) {
st := getTcellStyle(oldStyle{fg: fg, bg: bg, outputMode: outputMode})
Screen.SetContent(x, y, ch, nil, st)
}
// getTcellStyle creates tcell.Style from Attributes
func getTcellStyle(input oldStyle) tcell.Style {
st := tcell.StyleDefault
// extract colors and attributes
if input.fg != ColorDefault {
st = st.Foreground(getTcellColor(input.fg, input.outputMode))
st = setTcellFontEffectStyle(st, input.fg)
}
if input.bg != ColorDefault {
st = st.Background(getTcellColor(input.bg, input.outputMode))
st = setTcellFontEffectStyle(st, input.bg)
}
return st
}
// setTcellFontEffectStyle add additional attributes to tcell.Style
func setTcellFontEffectStyle(st tcell.Style, attr Attribute) tcell.Style {
if attr&AttrBold != 0 {
st = st.Bold(true)
}
if attr&AttrUnderline != 0 {
st = st.Underline(true)
}
if attr&AttrReverse != 0 {
st = st.Reverse(true)
}
if attr&AttrBlink != 0 {
st = st.Blink(true)
}
if attr&AttrDim != 0 {
st = st.Dim(true)
}
if attr&AttrItalic != 0 {
st = st.Italic(true)
}
if attr&AttrStrikeThrough != 0 {
st = st.StrikeThrough(true)
}
return st
}
// gocuiEventType represents the type of event.
type gocuiEventType uint8
// GocuiEvent represents events like a keys, mouse actions, or window resize.
//
// The 'Mod', 'Key' and 'Ch' fields are valid if 'Type' is 'eventKey'.
// The 'MouseX' and 'MouseY' fields are valid if 'Type' is 'eventMouse'.
// The 'Width' and 'Height' fields are valid if 'Type' is 'eventResize'.
// The 'Focused' field is valid if 'Type' is 'eventFocus'.
// The 'Start' field is valid if 'Type' is 'eventPaste'. It is true for the
// beginning of a paste operation, false for the end.
// The 'Err' field is valid if 'Type' is 'eventError'.
type GocuiEvent struct {
Type gocuiEventType
Mod Modifier
Key Key
Ch rune
Width int
Height int
Err error
MouseX int
MouseY int
Focused bool
Start bool
N int
}
// Event types.
const (
eventNone gocuiEventType = iota
eventKey
eventResize
eventMouse
eventMouseMove // only used when no button is down, otherwise it's eventMouse
eventFocus
eventPaste
eventInterrupt
eventError
eventRaw
)
const (
NOT_DRAGGING int = iota
MAYBE_DRAGGING
DRAGGING
)
var (
lastMouseKey tcell.ButtonMask = tcell.ButtonNone
lastMouseMod tcell.ModMask = tcell.ModNone
dragState int = NOT_DRAGGING
lastX int = 0
lastY int = 0
)
// this wrapper struct has public keys so we can easily serialize/deserialize to JSON
type TcellKeyEventWrapper struct {
Timestamp int64
Mod tcell.ModMask
Key tcell.Key
Ch rune
}
func NewTcellKeyEventWrapper(event *tcell.EventKey, timestamp int64) *TcellKeyEventWrapper {
return &TcellKeyEventWrapper{
Timestamp: timestamp,
Mod: event.Modifiers(),
Key: event.Key(),
Ch: event.Rune(),
}
}
func (wrapper TcellKeyEventWrapper) toTcellEvent() tcell.Event {
return tcell.NewEventKey(wrapper.Key, wrapper.Ch, wrapper.Mod)
}
type TcellMouseEventWrapper struct {
Timestamp int64
X int
Y int
ButtonMask tcell.ButtonMask
ModMask tcell.ModMask
}
func NewTcellMouseEventWrapper(event *tcell.EventMouse, timestamp int64) *TcellMouseEventWrapper {
x, y := event.Position()
return &TcellMouseEventWrapper{
Timestamp: timestamp,
X: x,
Y: y,
ButtonMask: event.Buttons(),
ModMask: event.Modifiers(),
}
}
func (wrapper TcellMouseEventWrapper) toTcellEvent() tcell.Event {
return tcell.NewEventMouse(wrapper.X, wrapper.Y, wrapper.ButtonMask, wrapper.ModMask)
}
type TcellResizeEventWrapper struct {
Timestamp int64
Width int
Height int
}
func NewTcellResizeEventWrapper(event *tcell.EventResize, timestamp int64) *TcellResizeEventWrapper {
w, h := event.Size()
return &TcellResizeEventWrapper{
Timestamp: timestamp,
Width: w,
Height: h,
}
}
func (wrapper TcellResizeEventWrapper) toTcellEvent() tcell.Event {
return tcell.NewEventResize(wrapper.Width, wrapper.Height)
}
// pollEvent get tcell.Event and transform it into gocuiEvent
func (g *Gui) pollEvent() GocuiEvent {
var tev tcell.Event
if g.playRecording {
select {
case ev := <-g.ReplayedEvents.Keys:
tev = (ev).toTcellEvent()
case ev := <-g.ReplayedEvents.Resizes:
tev = (ev).toTcellEvent()
case ev := <-g.ReplayedEvents.MouseEvents:
tev = (ev).toTcellEvent()
}
} else {
tev = Screen.PollEvent()
}
switch tev := tev.(type) {
case *tcell.EventInterrupt:
return GocuiEvent{Type: eventInterrupt}
case *tcell.EventResize:
w, h := tev.Size()
return GocuiEvent{Type: eventResize, Width: w, Height: h}
case *tcell.EventKey:
k := tev.Key()
ch := rune(0)
if k == tcell.KeyRune {
k = 0 // if rune remove key (so it can match rune instead of key)
ch = tev.Rune()
if ch == ' ' {
// special handling for spacebar
k = 32 // tcell keys ends at 31 or starts at 256
ch = rune(0)
}
}
mod := tev.Modifiers()
// remove control modifier and setup special handling of ctrl+spacebar, etc.
if mod == tcell.ModCtrl && k == 32 {
mod = 0
ch = rune(0)
k = tcell.KeyCtrlSpace
} else if mod == tcell.ModShift && k == tcell.KeyUp {
mod = 0
ch = rune(0)
k = tcell.KeyF62
} else if mod == tcell.ModShift && k == tcell.KeyDown {
mod = 0
ch = rune(0)
k = tcell.KeyF63
} else if mod == tcell.ModCtrl || mod == tcell.ModShift {
// remove Ctrl or Shift if specified
// - shift - will be translated to the final code of rune
// - ctrl - is translated in the key
mod = 0
} else if mod == tcell.ModAlt && k == tcell.KeyEnter {
// for the sake of convenience I'm having a KeyAltEnter key. I will likely
// regret this laziness in the future. We're arbitrarily mapping that to tcell's
// KeyF64.
mod = 0
k = tcell.KeyF64
}
return GocuiEvent{
Type: eventKey,
Key: Key(k),
Ch: ch,
Mod: Modifier(mod),
}
case *tcell.EventMouse:
x, y := tev.Position()
button := tev.Buttons()
mouseKey := MouseRelease
mouseMod := ModNone
// process mouse wheel
if button&tcell.WheelUp != 0 {
mouseKey = MouseWheelUp
}
if button&tcell.WheelDown != 0 {
mouseKey = MouseWheelDown
}
if button&tcell.WheelLeft != 0 {
mouseKey = MouseWheelLeft
}
if button&tcell.WheelRight != 0 {
mouseKey = MouseWheelRight
}
wheeling := mouseKey == MouseWheelUp || mouseKey == MouseWheelDown || mouseKey == MouseWheelLeft || mouseKey == MouseWheelRight
// process button events (not wheel events)
button &= tcell.ButtonMask(0xff)
if button != tcell.ButtonNone && lastMouseKey == tcell.ButtonNone {
lastMouseKey = button
lastMouseMod = tev.Modifiers()
switch button {
case tcell.ButtonPrimary:
mouseKey = MouseLeft
dragState = MAYBE_DRAGGING
lastX = x
lastY = y
case tcell.ButtonSecondary:
mouseKey = MouseRight
case tcell.ButtonMiddle:
mouseKey = MouseMiddle
default:
}
}
switch tev.Buttons() {
case tcell.ButtonNone:
if lastMouseKey != tcell.ButtonNone {
switch lastMouseKey {
case tcell.ButtonPrimary:
dragState = NOT_DRAGGING
case tcell.ButtonSecondary:
case tcell.ButtonMiddle:
default:
}
mouseMod = Modifier(lastMouseMod)
lastMouseMod = tcell.ModNone
lastMouseKey = tcell.ButtonNone
}
default:
}
if !wheeling {
switch dragState {
case NOT_DRAGGING:
return GocuiEvent{
Type: eventMouseMove,
MouseX: x,
MouseY: y,
}
// if we haven't released the left mouse button and we've moved the cursor then we're dragging
case MAYBE_DRAGGING:
if x != lastX || y != lastY {
dragState = DRAGGING
}
case DRAGGING:
mouseMod = ModMotion
mouseKey = MouseLeft
}
}
return GocuiEvent{
Type: eventMouse,
MouseX: x,
MouseY: y,
Key: mouseKey,
Ch: 0,
Mod: mouseMod,
}
case *tcell.EventFocus:
return GocuiEvent{
Type: eventFocus,
Focused: tev.Focused,
}
case *tcell.EventPaste:
return GocuiEvent{
Type: eventPaste,
Start: tev.Start(),
}
default:
return GocuiEvent{Type: eventNone}
}
}
|