Skip to content

Commit

Permalink
Merge pull request #77 from chelnak/chroma_config
Browse files Browse the repository at this point in the history
Colorize config output
  • Loading branch information
chelnak committed May 20, 2022
2 parents 9bf6961 + 0776673 commit 450bb45
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
10 changes: 7 additions & 3 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"github.com/spf13/cobra"
)

var output string
var (
output string
noColor bool
)

// configCmd is the entry point for printing the applications configuration in the terminal
var configCmd = &cobra.Command{
Expand All @@ -20,9 +23,9 @@ var configCmd = &cobra.Command{
RunE: func(command *cobra.Command, args []string) error {
switch output {
case "json":
return configuration.Config.PrintJSON(os.Stdout)
return configuration.Config.PrintJSON(noColor, os.Stdout)
case "yaml":
return configuration.Config.PrintYAML(os.Stdout)
return configuration.Config.PrintYAML(noColor, os.Stdout)
default:
return errors.New("invalid output format. Valid values are 'json' and 'yaml'")
}
Expand All @@ -31,4 +34,5 @@ var configCmd = &cobra.Command{

func init() {
configCmd.Flags().StringVarP(&output, "output", "o", "yaml", "The output format. Valid values are 'json' and 'yaml'. Defaults to 'yaml'.")
configCmd.Flags().BoolVarP(&noColor, "no-color", "n", false, "Disable color output")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/alecthomas/chroma v0.10.0
github.com/briandowns/spinner v1.18.1
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
Expand All @@ -21,7 +22,6 @@ require (
)

require (
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.1 // indirect
Expand Down
65 changes: 58 additions & 7 deletions internal/pkg/configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import (
"os"
"path/filepath"

"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
Expand All @@ -28,28 +32,73 @@ type configuration struct {
CheckForUpdates bool `mapstructure:"check_for_updates" yaml:"check_for_updates" json:"checkForUpdates"`
}

func write(data []byte, writer io.Writer) error {
_, err := fmt.Fprint(writer, string(data))
return err
type writeOptions struct {
data string
lexerName string
noColor bool
writer io.Writer
}

func (c *configuration) PrintJSON(writer io.Writer) error {
func prettyWrite(opts writeOptions) error {
lexer := lexers.Get(opts.lexerName)
if lexer == nil {
lexer = lexers.Fallback
}

lexer = chroma.Coalesce(lexer)

style := styles.Get("native")
if style == nil {
style = styles.Fallback
}

formatter := formatters.Get("terminal16m")

if opts.noColor {
formatter = formatters.Get("noop")
}

iterator, err := lexer.Tokenise(nil, opts.data)
if err != nil {
return err
}

return formatter.Format(opts.writer, style, iterator)
}

func (c *configuration) PrintJSON(noColor bool, writer io.Writer) error {
b, err := json.MarshalIndent(c, "", " ")
b = append(b, '\n')
if err != nil {
return err
}

return write(b, writer)
opts := writeOptions{
data: string(b),
lexerName: "json",
noColor: noColor,
writer: writer,
}

return prettyWrite(opts)
}

func (c *configuration) PrintYAML(writer io.Writer) error {
func (c *configuration) PrintYAML(noColor bool, writer io.Writer) error {
b, err := yaml.Marshal(c)
y := []byte("---\n")
y = append(y, b...)
if err != nil {
return err
}

return write(b, writer)
opts := writeOptions{
data: string(y),
lexerName: "json",
noColor: noColor,
writer: writer,
}

return prettyWrite(opts)
}

func InitConfig() error {
Expand Down Expand Up @@ -103,4 +152,6 @@ func setDefaults() {
viper.SetDefault("show_unreleased", true)

viper.SetDefault("check_for_updates", true)

viper.SetDefault("no_color", false)
}
7 changes: 4 additions & 3 deletions internal/pkg/configuration/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestPrintJSON(t *testing.T) {
config := configuration.Config

var buf bytes.Buffer
err = config.PrintJSON(&buf)
err = config.PrintJSON(true, &buf)
assert.NoError(t, err)

cfg := `{
Expand Down Expand Up @@ -73,10 +73,11 @@ func TestPrintYAML(t *testing.T) {
config := configuration.Config

var buf bytes.Buffer
err = config.PrintYAML(&buf)
err = config.PrintYAML(true, &buf)
assert.NoError(t, err)

cfg := `file_name: CHANGELOG.md
cfg := `---
file_name: CHANGELOG.md
excluded_labels:
- maintenance
sections:
Expand Down

0 comments on commit 450bb45

Please sign in to comment.