Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color config #25

Merged
merged 15 commits into from
Apr 20, 2018
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
name = "github.com/fatih/color"
version = "1.6.0"

[[constraint]]
name = "github.com/imdario/mergo"
version = "0.3.4"

[[constraint]]
branch = "master"
name = "github.com/olekukonko/tablewriter"
Expand All @@ -49,10 +53,6 @@
name = "github.com/zefhemel/kingpin"
version = "2.2.5"

[[constraint]]
name = "github.com/zefhemel/termui"
version = "2.2.0"

[[constraint]]
branch = "master"
name = "golang.org/x/crypto"
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,31 @@ or pretty JSON:

ax --output pretty-json

# Customizing colors for "text" output

In your `~/.config/ax/ax.yaml` file (`ax env edit`) you can override the default colors as follows:

colors:
timestamp:
fg: magenta
message:
bold: true
attributekey:
faint: true
fg: green
attributevalue:
faint: true
fg: blue

For each "color" you can set:

* `fg` — foreground color (`red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`)
* `bg` — background color (same options)
* `bold` — bold font (`true` or `false`)
* `italic` — italic font (`true` or `false`)
* `underline` — underline font (`true` or `false`)
* `faint` — faint (color) font (`true` or `false`)

# Getting help

ax --help
Expand Down
14 changes: 8 additions & 6 deletions cmd/ax/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/egnyte/ax/pkg/backend/common"
"github.com/egnyte/ax/pkg/complete"
"github.com/egnyte/ax/pkg/config"
"github.com/fatih/color"
"github.com/zefhemel/kingpin"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -127,25 +126,28 @@ func queryMain(ctx context.Context, rc config.RuntimeConfig, client common.Clien
}
seenBeforeHash[contentHash] = true
}
printMessage(message, queryFlagOutputFormat)
printMessage(message, queryFlagOutputFormat, rc.Config.Colors)
}

}

func printMessage(message common.LogMessage, queryOutputFormat string) {
func printMessage(message common.LogMessage, queryOutputFormat string, colorConfig config.ColorConfig) {
switch queryOutputFormat {
case "text":
ts := message.Timestamp.Format(common.TimeFormat)
fmt.Printf("[%s] ", color.MagentaString(ts))
timestampColor := config.ColorToTermColor(colorConfig.Timestamp)
fmt.Printf("%s ", timestampColor.Sprintf("[%s]", ts))
if msg, ok := message.Attributes["message"].(string); ok {
messageColor := color.New(color.Bold)
messageColor := config.ColorToTermColor(colorConfig.Message)
fmt.Printf("%s ", messageColor.Sprint(msg))
}
attributeKeyColor := config.ColorToTermColor(colorConfig.AttributeKey)
attributeValueColor := config.ColorToTermColor(colorConfig.AttributeValue)
for key, value := range message.Attributes {
if key == "message" || value == nil {
continue
}
fmt.Printf("%s=%+v ", color.CyanString(key), value)
fmt.Printf("%s%s ", attributeKeyColor.Sprintf("%s=", key), attributeValueColor.Sprintf("%+v", value))
}
fmt.Println()
case "json":
Expand Down
83 changes: 83 additions & 0 deletions pkg/config/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package config

import "github.com/fatih/color"

type colorDef struct {
Fg string
Bg string
Bold bool
Faint bool
Italic bool
Underline bool
}

type ColorConfig struct {
Timestamp colorDef
AttributeKey colorDef
AttributeValue colorDef
Message colorDef
}

var defaultColorConfig ColorConfig = ColorConfig{
Timestamp: colorDef{
Fg: "magenta",
},
Message: colorDef{
Bold: true,
},
AttributeKey: colorDef{
Fg: "cyan",
},
}

func colorStringToAttribute(s string, fg bool) color.Attribute {
fgOrBg := func(f color.Attribute, b color.Attribute) color.Attribute {
if fg {
return f
} else {
return b
}
}
switch s {
case "red":
return fgOrBg(color.FgRed, color.BgRed)
case "green":
return fgOrBg(color.FgGreen, color.BgGreen)
case "yellow":
return fgOrBg(color.FgYellow, color.BgYellow)
case "blue":
return fgOrBg(color.FgBlue, color.BgBlue)
case "magenta":
return fgOrBg(color.FgMagenta, color.BgMagenta)
case "cyan":
return fgOrBg(color.FgCyan, color.BgCyan)
case "white":
return fgOrBg(color.FgWhite, color.BgWhite)
}
return color.Reset
}

func ColorToTermColor(colorDef colorDef) *color.Color {
c := color.New()

if colorDef.Fg != "" {
c = c.Add(colorStringToAttribute(colorDef.Fg, true))
}
if colorDef.Bg != "" {
c = c.Add(colorStringToAttribute(colorDef.Bg, false))
}

if colorDef.Bold {
c = c.Add(color.Bold)
}
if colorDef.Faint {
c = c.Add(color.Faint)
}
if colorDef.Italic {
c = c.Add(color.Italic)
}
if colorDef.Underline {
c = c.Add(color.Underline)
}
return c
}
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"golang.org/x/crypto/ssh/terminal"

"github.com/imdario/mergo"
"github.com/zefhemel/kingpin"
yaml "gopkg.in/yaml.v2"

Expand All @@ -27,6 +28,7 @@ type EnvMap map[string]string

type Config struct {
DefaultEnv string `yaml:"default"`
Colors ColorConfig `yaml:"colors"`
Environments map[string]EnvMap `yaml:"env"`
Alerts []AlertConfig `yaml:"alerts"`
}
Expand Down Expand Up @@ -80,6 +82,9 @@ func LoadConfig() Config {
if config.Alerts == nil {
config.Alerts = make([]AlertConfig, 0)
}
if err := mergo.Merge(&config.Colors, defaultColorConfig); err != nil {
panic("Could not set default colors")
}
return config
}

Expand Down