1
0
mirror of https://github.com/mum4k/termdash.git synced 2025-04-25 13:48:50 +08:00

Fix tests for ColorDefault

This commit is contained in:
kvnxiao 2020-02-29 18:46:20 -05:00
parent a0d3da0527
commit f7578e54cc
2 changed files with 23 additions and 9 deletions

View File

@ -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 {

View File

@ -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: