From 8202e3a669491853952ed674a5c81bf049f44dac Mon Sep 17 00:00:00 2001 From: Graham Clark Date: Thu, 23 May 2019 21:04:38 -0400 Subject: [PATCH] Ensure all strings representing urwid colors map to a valid terminal color. If the programmer creates an UrwidColor initialized with a string like "dark red", it would lead to a panic if TERM only supported 8 colors. This change maps "dark red" to an 8-color alternative if e.g. TERM=xterm. Fixes #10. --- decoration.go | 107 +++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/decoration.go b/decoration.go index c80b227..06639db 100644 --- a/decoration.go +++ b/decoration.go @@ -136,11 +136,16 @@ func (c ColorMode) String() string { const ( colorDefaultName = "default" colorBlackName = "black" + colorRedName = "red" colorDarkRedName = "dark red" + colorGreenName = "green" colorDarkGreenName = "dark green" colorBrownName = "brown" + colorBlueName = "blue" colorDarkBlueName = "dark blue" + colorMagentaName = "magenta" colorDarkMagentaName = "dark magenta" + colorCyanName = "cyan" colorDarkCyanName = "dark cyan" colorLightGrayName = "light gray" colorDarkGrayName = "dark gray" @@ -151,57 +156,59 @@ const ( colorLightMagentaName = "light magenta" colorLightCyanName = "light cyan" colorWhiteName = "white" - - tColorDefaultName = "default" - tColorBlackName = "black" - tColorRedName = "red" - tColorGreenName = "green" - tColorYellowName = "yellow" - tColorBlueName = "blue" - tColorMagentaName = "magenta" - tColorCyanName = "cyan" - tColorWhiteName = "white" ) var ( - basicColors = []string{ - colorDefaultName, - colorBlackName, - colorDarkRedName, - colorDarkGreenName, - colorBrownName, - colorDarkBlueName, - colorDarkMagentaName, - colorDarkCyanName, - colorLightGrayName, - colorDarkGrayName, - colorLightRedName, - colorLightGreenName, - colorYellowName, - colorLightBlueName, - colorLightMagentaName, - colorLightCyanName, - colorWhiteName, + basicColors = map[string]int{ + colorDefaultName: 0, + colorBlackName: 1, + colorDarkRedName: 2, + colorDarkGreenName: 3, + colorBrownName: 4, + colorDarkBlueName: 5, + colorDarkMagentaName: 6, + colorDarkCyanName: 7, + colorLightGrayName: 8, + colorDarkGrayName: 9, + colorLightRedName: 10, + colorLightGreenName: 11, + colorYellowName: 12, + colorLightBlueName: 13, + colorLightMagentaName: 14, + colorLightCyanName: 15, + colorWhiteName: 16, + colorRedName: 10, + colorGreenName: 11, + colorBlueName: 13, + colorMagentaName: 14, + colorCyanName: 15, } - basicColorsMap map[string]int - - tCellBasicColors = []string{ - tColorDefaultName, - tColorBlackName, - tColorRedName, - tColorGreenName, - tColorYellowName, - tColorBlueName, - tColorMagentaName, - tColorCyanName, - tColorWhiteName, + tBasicColors = map[string]int{ + colorDefaultName: 0, + colorBlackName: 1, + colorDarkRedName: 2, + colorDarkGreenName: 3, + colorBrownName: 4, + colorDarkBlueName: 5, + colorDarkMagentaName: 6, + colorDarkCyanName: 7, + colorLightGrayName: 8, + colorDarkGrayName: 1, + colorLightRedName: 2, + colorLightGreenName: 3, + colorYellowName: 4, + colorLightBlueName: 5, + colorLightMagentaName: 6, + colorLightCyanName: 7, + colorWhiteName: 8, + colorRedName: 2, + colorGreenName: 3, + colorBlueName: 5, + colorMagentaName: 6, + colorCyanName: 7, } - NumOfTCellBasicColors = len(tCellBasicColors) - - tBasicColorsMap map[string]int - CubeStart = 16 // first index of color cube CubeSize256 = 6 // one side of the color cube graySize256 = 24 @@ -375,14 +382,6 @@ func init() { grayLookup256_101[i] = grayLookup256[intScale(i, 101, 0x100)] grayLookup88_101[i] = grayLookup88[intScale(i, 101, 0x100)] } - basicColorsMap = make(map[string]int) - tBasicColorsMap = make(map[string]int) - for i, v := range basicColors { - basicColorsMap[v] = i - } - for i, v := range tCellBasicColors { - tBasicColorsMap[v] = i - } var err error for _, cache := range []**lru.Cache{&term2Cache, &term8Cache, &term16Cache} { @@ -756,9 +755,9 @@ func (s *UrwidColor) ToTCellColor(mode ColorMode) (TCellColor, bool) { idx := -1 switch mode { case Mode24BitColors, Mode256Colors, Mode88Colors, Mode16Colors: - idx = posInMap(s.Id, basicColorsMap) + idx = posInMap(s.Id, basicColors) case Mode8Colors, ModeMonochrome: - idx = posInMap(s.Id, tBasicColorsMap) + idx = posInMap(s.Id, tBasicColors) default: panic(errors.WithStack(ColorModeMismatch{Color: s, Mode: mode})) }