Skip to content

Commit

Permalink
Ensure all strings representing urwid colors map to a valid terminal …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
gcla committed May 24, 2019
1 parent 1cdc86e commit 8202e3a
Showing 1 changed file with 53 additions and 54 deletions.
107 changes: 53 additions & 54 deletions decoration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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} {
Expand Down Expand Up @@ -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}))
}
Expand Down

0 comments on commit 8202e3a

Please sign in to comment.