1717package gum
1818
1919import (
20- "bytes"
2120 "fmt"
2221 "io/ioutil"
2322 "path/filepath"
2423 "strings"
2524
25+ "github.com/gookit/color"
2626 "github.com/grignaak/tribool"
2727 "github.com/pelletier/go-toml"
2828)
2929
3030// Config defines configuration settings for Gum
3131type Config struct {
32+ theme theme
3233 general general
3334 gradle gradle
3435 maven maven
3536 jbang jbang
3637}
3738
39+ type theme struct {
40+ t Theme
41+
42+ name string
43+ symbol [2 ]uint8
44+ section [2 ]uint8
45+ key [2 ]uint8
46+ boolean [2 ]uint8
47+ literal [2 ]uint8
48+ }
49+
3850type general struct {
3951 quiet bool
4052 debug bool
@@ -67,61 +79,47 @@ type jbang struct {
6779}
6880
6981func (c * Config ) print () {
70- fmt .Println ("[general]" )
71- fmt .Println ("quiet =" , c .general .quiet )
72- fmt .Println ("debug =" , c .general .debug )
73- fmt .Println ("discovery =" , formatSlice (c .general .discovery ))
74- fmt .Println ("" )
75- fmt .Println ("[gradle]" )
76- fmt .Println ("replace =" , c .gradle .replace )
77- fmt .Println ("defaults =" , c .gradle .defaults )
82+ c .theme .t .PrintSection ("theme" )
83+ c .theme .t .PrintKeyValueLiteral ("name" , c .theme .name )
84+ if isInstanceOf (c .theme .t , (* ColoredTheme )(nil )) {
85+ c .theme .t .PrintKeyValueArrayI ("symbol" , c .theme .symbol )
86+ c .theme .t .PrintKeyValueArrayI ("section" , c .theme .section )
87+ c .theme .t .PrintKeyValueArrayI ("key" , c .theme .key )
88+ c .theme .t .PrintKeyValueArrayI ("boolean" , c .theme .boolean )
89+ c .theme .t .PrintKeyValueArrayI ("literal" , c .theme .literal )
90+ }
91+ c .theme .t .PrintSection ("general" )
92+ c .theme .t .PrintKeyValueBoolean ("quiet" , c .general .quiet )
93+ c .theme .t .PrintKeyValueBoolean ("debug" , c .general .debug )
94+ c .theme .t .PrintKeyValueArrayS ("discovery" , c .general .discovery )
95+ c .theme .t .PrintSection ("gradle" )
96+ c .theme .t .PrintKeyValueBoolean ("replace" , c .gradle .replace )
97+ c .theme .t .PrintKeyValueBoolean ("defaults" , c .gradle .defaults )
7898 if len (c .gradle .mappings ) > 0 {
79- fmt . Println ( "[ gradle.mappings] " )
80- printMappings (c .gradle .mappings )
99+ c . theme . t . PrintSection ( " gradle.mappings" )
100+ c . theme . t . PrintMap (c .gradle .mappings )
81101 }
82- fmt .Println ("" )
83- fmt .Println ("[maven]" )
84- fmt .Println ("replace =" , c .maven .replace )
85- fmt .Println ("defaults =" , c .maven .defaults )
102+ c .theme .t .PrintSection ("maven" )
103+ c .theme .t .PrintKeyValueBoolean ("replace" , c .maven .replace )
104+ c .theme .t .PrintKeyValueBoolean ("defaults" , c .maven .defaults )
86105 if len (c .maven .mappings ) > 0 {
87- fmt .Println ("[maven.mappings]" )
88- printMappings (c .maven .mappings )
89- }
90- fmt .Println ("" )
91- fmt .Println ("[jbang]" )
92- fmt .Println ("discovery =" , formatSlice (c .jbang .discovery ))
93- }
94-
95- func formatSlice (s []string ) string {
96- var buffer bytes.Buffer
97- buffer .WriteString ("[" )
98-
99- for i , w := range s {
100- if i != 0 {
101- buffer .WriteString (", " )
102- }
103- buffer .WriteString ("\" " )
104- buffer .WriteString (w )
105- buffer .WriteString ("\" " )
106- }
107-
108- buffer .WriteString ("]" )
109- return buffer .String ()
110- }
111-
112- func printMappings (mappings map [string ]string ) {
113- for k , v := range mappings {
114- if strings .Contains (k , ":" ) {
115- fmt .Print ("\" " + k + "\" " )
116- } else {
117- fmt .Print (k )
118- }
119- fmt .Println (" = \" " + v + "\" " )
106+ c .theme .t .PrintSection ("maven.mappings" )
107+ c .theme .t .PrintMap (c .maven .mappings )
120108 }
109+ c .theme .t .PrintSection ("jbang" )
110+ c .theme .t .PrintKeyValueArrayS ("discovery" , c .jbang .discovery )
121111}
122112
123113func newConfig () * Config {
124114 return & Config {
115+ theme : theme {
116+ t : DarkTheme ,
117+ name : "dark" ,
118+ symbol : [2 ]uint8 {255 , 0 },
119+ section : [2 ]uint8 {28 , 0 },
120+ key : [2 ]uint8 {160 , 0 },
121+ boolean : [2 ]uint8 {99 , 0 },
122+ literal : [2 ]uint8 {33 , 0 }},
125123 general : general {
126124 q : tribool .Maybe ,
127125 d : tribool .Maybe ,
@@ -305,6 +303,7 @@ func ReadConfigFile(context Context, path string) *Config {
305303 return config
306304 }
307305
306+ resolveSectionTheme (t , config )
308307 resolveSectionGeneral (t , config )
309308 resolveSectionGradle (t , config )
310309 resolveSectionMaven (t , config )
@@ -313,6 +312,66 @@ func ReadConfigFile(context Context, path string) *Config {
313312 return config
314313}
315314
315+ func resolveSectionTheme (t * toml.Tree , config * Config ) {
316+ tt := t .Get ("theme" )
317+ if tt != nil {
318+ table := tt .(* toml.Tree )
319+ v := table .Get ("name" )
320+ if v != nil {
321+ config .theme .name = v .(string )
322+ }
323+ if config .theme .name == "none" {
324+ config .theme .t = NoneTheme
325+ return
326+ }
327+
328+ v = table .Get ("symbol" )
329+ if v != nil {
330+ data := v .([]interface {})
331+ config .theme .symbol = [2 ]uint8 {uint8 (data [0 ].(int64 )), uint8 (data [1 ].(int64 ))}
332+ }
333+ v = table .Get ("section" )
334+ if v != nil {
335+ data := v .([]interface {})
336+ config .theme .section = [2 ]uint8 {uint8 (data [0 ].(int64 )), uint8 (data [1 ].(int64 ))}
337+ }
338+ v = table .Get ("key" )
339+ if v != nil {
340+ data := v .([]interface {})
341+ config .theme .key = [2 ]uint8 {uint8 (data [0 ].(int64 )), uint8 (data [1 ].(int64 ))}
342+ }
343+ v = table .Get ("boolean" )
344+ if v != nil {
345+ data := v .([]interface {})
346+ config .theme .boolean = [2 ]uint8 {uint8 (data [0 ].(int64 )), uint8 (data [1 ].(int64 ))}
347+ }
348+ v = table .Get ("literal" )
349+ if v != nil {
350+ data := v .([]interface {})
351+ config .theme .literal = [2 ]uint8 {uint8 (data [0 ].(int64 )), uint8 (data [1 ].(int64 ))}
352+ }
353+ }
354+
355+ switch strings .ToLower (config .theme .name ) {
356+ case "light" :
357+ config .theme .t = LightTheme
358+ break
359+ case "custom" :
360+ config .theme .t = & ColoredTheme {
361+ name : "custom" ,
362+ symbol : color .S256 (config .theme .symbol [0 ], config .theme .symbol [1 ]),
363+ section : color .S256 (config .theme .section [0 ], config .theme .section [1 ]),
364+ key : color .S256 (config .theme .key [0 ], config .theme .key [1 ]),
365+ boolean : color .S256 (config .theme .boolean [0 ], config .theme .boolean [1 ]),
366+ literal : color .S256 (config .theme .literal [0 ], config .theme .literal [1 ])}
367+ break
368+ case "dark" :
369+ default :
370+ config .theme .t = DarkTheme
371+ break
372+ }
373+ }
374+
316375func resolveSectionGeneral (t * toml.Tree , config * Config ) {
317376 tt := t .Get ("general" )
318377 if tt != nil {
0 commit comments