@@ -6,7 +6,9 @@ package auto // import "go.pennock.tech/tabular/auto"
66
77import (
88 "io"
9+ "regexp"
910 "sort"
11+ "strconv"
1012 "strings"
1113
1214 "go.pennock.tech/tabular"
@@ -63,10 +65,43 @@ func Wrap(t tabular.Table, style string) RenderTable {
6365 return rt
6466}
6567
68+ var reColorHex * regexp.Regexp
69+
70+ func init () {
71+ reColorHex = regexp .MustCompile (`^#?([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})\z` )
72+ }
73+
6674func setColorsOrDecorationsFromSections (tt * texttable.TextTable , sections []string ) {
6775 doneFG , doneBG , doneDecoration := false , false , false
6876 for _ , section := range sections {
69- if c , err := color .ByHTMLNamedColor (section ); err == nil {
77+ var (
78+ c color.Color
79+ err error
80+ red , green , blue uint64
81+ haveColor bool
82+ )
83+ haveColor = false
84+ if c , err = color .ByHTMLNamedColor (section ); err == nil {
85+ haveColor = true
86+ } else if m := reColorHex .FindStringSubmatch (section ); m != nil {
87+ red , err = strconv .ParseUint (m [1 ], 16 , 8 )
88+ if err == nil {
89+ green , err = strconv .ParseUint (m [2 ], 16 , 8 )
90+ }
91+ if err == nil {
92+ blue , err = strconv .ParseUint (m [3 ], 16 , 8 )
93+ }
94+ if err == nil {
95+ c = color .RGB24 (uint8 (red ), uint8 (green ), uint8 (blue ))
96+ haveColor = true
97+ }
98+ } else if section == "solid" {
99+ tt .SetBGSolid (true )
100+ } else if ! doneDecoration {
101+ tt .SetDecorationNamed (section )
102+ doneDecoration = true
103+ }
104+ if haveColor {
70105 if doneFG && doneBG {
71106 continue
72107 } else if doneFG {
@@ -76,11 +111,6 @@ func setColorsOrDecorationsFromSections(tt *texttable.TextTable, sections []stri
76111 tt .SetFGColor (c )
77112 doneFG = true
78113 }
79- } else if section == "solid" {
80- tt .SetBGSolid (true )
81- } else if ! doneDecoration {
82- tt .SetDecorationNamed (section )
83- doneDecoration = true
84114 }
85115 }
86116}
0 commit comments