Skip to content

Commit

Permalink
Add color support. Fixes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Oct 8, 2020
1 parent 750c099 commit 5b77c06
Show file tree
Hide file tree
Showing 5 changed files with 321 additions and 48 deletions.
155 changes: 107 additions & 48 deletions gum/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,36 @@
package gum

import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"github.com/gookit/color"
"github.com/grignaak/tribool"
"github.com/pelletier/go-toml"
)

// Config defines configuration settings for Gum
type Config struct {
theme theme
general general
gradle gradle
maven maven
jbang jbang
}

type theme struct {
t Theme

name string
symbol [2]uint8
section [2]uint8
key [2]uint8
boolean [2]uint8
literal [2]uint8
}

type general struct {
quiet bool
debug bool
Expand Down Expand Up @@ -67,61 +79,47 @@ type jbang struct {
}

func (c *Config) print() {
fmt.Println("[general]")
fmt.Println("quiet =", c.general.quiet)
fmt.Println("debug =", c.general.debug)
fmt.Println("discovery =", formatSlice(c.general.discovery))
fmt.Println("")
fmt.Println("[gradle]")
fmt.Println("replace =", c.gradle.replace)
fmt.Println("defaults =", c.gradle.defaults)
c.theme.t.PrintSection("theme")
c.theme.t.PrintKeyValueLiteral("name", c.theme.name)
if isInstanceOf(c.theme.t, (*ColoredTheme)(nil)) {
c.theme.t.PrintKeyValueArrayI("symbol", c.theme.symbol)
c.theme.t.PrintKeyValueArrayI("section", c.theme.section)
c.theme.t.PrintKeyValueArrayI("key", c.theme.key)
c.theme.t.PrintKeyValueArrayI("boolean", c.theme.boolean)
c.theme.t.PrintKeyValueArrayI("literal", c.theme.literal)
}
c.theme.t.PrintSection("general")
c.theme.t.PrintKeyValueBoolean("quiet", c.general.quiet)
c.theme.t.PrintKeyValueBoolean("debug", c.general.debug)
c.theme.t.PrintKeyValueArrayS("discovery", c.general.discovery)
c.theme.t.PrintSection("gradle")
c.theme.t.PrintKeyValueBoolean("replace", c.gradle.replace)
c.theme.t.PrintKeyValueBoolean("defaults", c.gradle.defaults)
if len(c.gradle.mappings) > 0 {
fmt.Println("[gradle.mappings]")
printMappings(c.gradle.mappings)
c.theme.t.PrintSection("gradle.mappings")
c.theme.t.PrintMap(c.gradle.mappings)
}
fmt.Println("")
fmt.Println("[maven]")
fmt.Println("replace =", c.maven.replace)
fmt.Println("defaults =", c.maven.defaults)
c.theme.t.PrintSection("maven")
c.theme.t.PrintKeyValueBoolean("replace", c.maven.replace)
c.theme.t.PrintKeyValueBoolean("defaults", c.maven.defaults)
if len(c.maven.mappings) > 0 {
fmt.Println("[maven.mappings]")
printMappings(c.maven.mappings)
}
fmt.Println("")
fmt.Println("[jbang]")
fmt.Println("discovery =", formatSlice(c.jbang.discovery))
}

func formatSlice(s []string) string {
var buffer bytes.Buffer
buffer.WriteString("[")

for i, w := range s {
if i != 0 {
buffer.WriteString(", ")
}
buffer.WriteString("\"")
buffer.WriteString(w)
buffer.WriteString("\"")
}

buffer.WriteString("]")
return buffer.String()
}

func printMappings(mappings map[string]string) {
for k, v := range mappings {
if strings.Contains(k, ":") {
fmt.Print("\"" + k + "\"")
} else {
fmt.Print(k)
}
fmt.Println(" = \"" + v + "\"")
c.theme.t.PrintSection("maven.mappings")
c.theme.t.PrintMap(c.maven.mappings)
}
c.theme.t.PrintSection("jbang")
c.theme.t.PrintKeyValueArrayS("discovery", c.jbang.discovery)
}

func newConfig() *Config {
return &Config{
theme: theme{
t: DarkTheme,
name: "dark",
symbol: [2]uint8{255, 0},
section: [2]uint8{28, 0},
key: [2]uint8{160, 0},
boolean: [2]uint8{99, 0},
literal: [2]uint8{33, 0}},
general: general{
q: tribool.Maybe,
d: tribool.Maybe,
Expand Down Expand Up @@ -305,6 +303,7 @@ func ReadConfigFile(context Context, path string) *Config {
return config
}

resolveSectionTheme(t, config)
resolveSectionGeneral(t, config)
resolveSectionGradle(t, config)
resolveSectionMaven(t, config)
Expand All @@ -313,6 +312,66 @@ func ReadConfigFile(context Context, path string) *Config {
return config
}

func resolveSectionTheme(t *toml.Tree, config *Config) {
tt := t.Get("theme")
if tt != nil {
table := tt.(*toml.Tree)
v := table.Get("name")
if v != nil {
config.theme.name = v.(string)
}
if config.theme.name == "none" {
config.theme.t = NoneTheme
return
}

v = table.Get("symbol")
if v != nil {
data := v.([]interface{})
config.theme.symbol = [2]uint8{uint8(data[0].(int64)), uint8(data[1].(int64))}
}
v = table.Get("section")
if v != nil {
data := v.([]interface{})
config.theme.section = [2]uint8{uint8(data[0].(int64)), uint8(data[1].(int64))}
}
v = table.Get("key")
if v != nil {
data := v.([]interface{})
config.theme.key = [2]uint8{uint8(data[0].(int64)), uint8(data[1].(int64))}
}
v = table.Get("boolean")
if v != nil {
data := v.([]interface{})
config.theme.boolean = [2]uint8{uint8(data[0].(int64)), uint8(data[1].(int64))}
}
v = table.Get("literal")
if v != nil {
data := v.([]interface{})
config.theme.literal = [2]uint8{uint8(data[0].(int64)), uint8(data[1].(int64))}
}
}

switch strings.ToLower(config.theme.name) {
case "light":
config.theme.t = LightTheme
break
case "custom":
config.theme.t = &ColoredTheme{
name: "custom",
symbol: color.S256(config.theme.symbol[0], config.theme.symbol[1]),
section: color.S256(config.theme.section[0], config.theme.section[1]),
key: color.S256(config.theme.key[0], config.theme.key[1]),
boolean: color.S256(config.theme.boolean[0], config.theme.boolean[1]),
literal: color.S256(config.theme.literal[0], config.theme.literal[1])}
break
case "dark":
default:
config.theme.t = DarkTheme
break
}
}

func resolveSectionGeneral(t *toml.Tree, config *Config) {
tt := t.Get("general")
if tt != nil {
Expand Down
4 changes: 4 additions & 0 deletions gum/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func TestLoadConfig(t *testing.T) {
}
}

if config.theme.name != "dark" {
t.Errorf("theme.name: got %s, want dark", config.theme.name)
}

if config.gradle.mappings["compile"] != "compileJava" {
t.Errorf("gradle.mappings.compileJava: got %s, want %s", config.gradle.mappings["compile"], "compileJava")
}
Expand Down
Loading

0 comments on commit 5b77c06

Please sign in to comment.