From f7578e54ccf891182a267d07a499847ad1284d00 Mon Sep 17 00:00:00 2001 From: kvnxiao Date: Sat, 29 Feb 2020 18:46:20 -0500 Subject: [PATCH] Fix tests for ColorDefault --- terminal/tcell/cell_options_test.go | 6 ++++-- terminal/tcell/event.go | 26 +++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/terminal/tcell/cell_options_test.go b/terminal/tcell/cell_options_test.go index 32c808d..b3df570 100644 --- a/terminal/tcell/cell_options_test.go +++ b/terminal/tcell/cell_options_test.go @@ -79,7 +79,7 @@ func TestFixColor(t *testing.T) { {terminalapi.ColorModeNormal, cell.ColorWhite, tcell.ColorSilver}, {terminalapi.ColorModeNormal, cell.ColorNumber(42), tcell.Color(10)}, // Grayscale colors (all the grey colours from 231 to 255) - {terminalapi.ColorModeGrayscale, cell.ColorDefault, tcell.Color231}, + {terminalapi.ColorModeGrayscale, cell.ColorDefault, tcell.ColorDefault}, {terminalapi.ColorModeGrayscale, cell.ColorBlack, tcell.Color232}, {terminalapi.ColorModeGrayscale, cell.ColorRed, tcell.Color233}, {terminalapi.ColorModeGrayscale, cell.ColorGreen, tcell.Color234}, @@ -90,7 +90,7 @@ func TestFixColor(t *testing.T) { {terminalapi.ColorModeGrayscale, cell.ColorWhite, tcell.Color239}, {terminalapi.ColorModeGrayscale, cell.ColorNumber(42), tcell.Color(250)}, // 216 colors (16 to 231) - {terminalapi.ColorMode216, cell.ColorDefault, tcell.ColorWhite}, + {terminalapi.ColorMode216, cell.ColorDefault, tcell.ColorDefault}, {terminalapi.ColorMode216, cell.ColorBlack, tcell.Color16}, {terminalapi.ColorMode216, cell.ColorRed, tcell.Color17}, {terminalapi.ColorMode216, cell.ColorGreen, tcell.Color18}, @@ -100,6 +100,8 @@ func TestFixColor(t *testing.T) { {terminalapi.ColorMode216, cell.ColorCyan, tcell.Color22}, {terminalapi.ColorMode216, cell.ColorWhite, tcell.Color23}, {terminalapi.ColorMode216, cell.ColorNumber(42), tcell.Color(58)}, + // Unknown color mode + {-1, cell.ColorRed, tcell.ColorDefault}, } for _, tc := range tests { diff --git a/terminal/tcell/event.go b/terminal/tcell/event.go index 4b315ce..a196660 100644 --- a/terminal/tcell/event.go +++ b/terminal/tcell/event.go @@ -107,7 +107,9 @@ func convKey(event *tcell.EventKey) terminalapi.Event { } // convMouse converts a tcell mouse event to the termdash format. -func convMouse(event *tcell.EventMouse) terminalapi.Event { +// Since tcell supports many combinations of mouse events, such as multiple mouse buttons pressed at the same time, +// this function returns a secondary bool that denotes whether the event is valid for termdash. +func convMouse(event *tcell.EventMouse) (terminalapi.Event, bool) { var button mouse.Button x, y := event.Position() @@ -115,7 +117,7 @@ func convMouse(event *tcell.EventMouse) terminalapi.Event { // tcell uses signed int16 for button masks, and negative values are invalid if tcellBtn < 0 { - return terminalapi.NewErrorf("unknown mouse key %v in a mouse event", tcellBtn) + return terminalapi.NewErrorf("unknown mouse key %v in a mouse event", tcellBtn), true } // Get wheel events @@ -130,7 +132,7 @@ func convMouse(event *tcell.EventMouse) terminalapi.Event { return &terminalapi.Mouse{ Position: image.Point{X: x, Y: y}, Button: button, - } + }, true } // Get only button events, not wheel events @@ -145,14 +147,17 @@ func convMouse(event *tcell.EventMouse) terminalapi.Event { case tcell.Button3: button = mouse.ButtonMiddle default: - // Do nothing, since tcell allows multiple buttons to be pressed at the same time - // Maybe refactor terminalapi to handle multiple mouse buttons being pressed at the same time (e.g. M1 + M2) + // Unknown event to termdash + return &terminalapi.Mouse{ + Position: image.Point{X: x, Y: y}, + Button: button, + }, false } return &terminalapi.Mouse{ Position: image.Point{X: x, Y: y}, Button: button, - } + }, true } // convResize converts a tcell resize event to the termdash format. @@ -177,7 +182,14 @@ func toTermdashEvents(event tcell.Event) []terminalapi.Event { case *tcell.EventKey: return []terminalapi.Event{convKey(event)} case *tcell.EventMouse: - return []terminalapi.Event{convMouse(event)} + mouseEvent, termdashOk := convMouse(event) + if termdashOk { + return []terminalapi.Event{mouseEvent} + } else { + return []terminalapi.Event{ + terminalapi.NewErrorf("unknown tcell event type: %v", event), + } + } case *tcell.EventResize: return []terminalapi.Event{convResize(event)} case *tcell.EventError: