From b8333aceb27c8bbd15c151791b93cd7671138356 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Tue, 29 Oct 2024 12:47:17 -0500 Subject: [PATCH] feat: Format comments with a gray foreground --- cmd/cmd.go | 7 +-- go.mod | 5 +- go.sum | 4 ++ internal/colorize/colorize.go | 89 ----------------------------------- 4 files changed, 11 insertions(+), 94 deletions(-) delete mode 100644 internal/colorize/colorize.go diff --git a/cmd/cmd.go b/cmd/cmd.go index afc5263..a8033e3 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -10,7 +10,7 @@ import ( "path/filepath" "strings" - "github.com/clevyr/yampl/internal/colorize" + "gabe565.com/utils/coloryaml" "github.com/clevyr/yampl/internal/config" "github.com/clevyr/yampl/internal/util" "github.com/clevyr/yampl/internal/visitor" @@ -87,7 +87,7 @@ func run(cmd *cobra.Command, args []string) error { return err } - if err := colorize.WriteString(cmd.OutOrStdout(), s); err != nil { + if _, err := coloryaml.WriteString(cmd.OutOrStdout(), s); err != nil { return err } @@ -192,7 +192,8 @@ func openAndTemplateFile(conf *config.Config, w io.Writer, path string) error { } } - return colorize.WriteString(w, s) + _, err := coloryaml.WriteString(w, s) + return err } temp, err := os.CreateTemp("", "yampl_*_"+filepath.Base(path)) diff --git a/go.mod b/go.mod index 0954bdc..b924b02 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,9 @@ module github.com/clevyr/yampl go 1.23.2 require ( + gabe565.com/utils v0.0.0-20241029174609-934fb3acf91e github.com/Masterminds/sprig/v3 v3.3.0 github.com/dmarkham/enumer v1.5.10 - github.com/fatih/color v1.18.0 - github.com/goccy/go-yaml v1.13.0 github.com/lmittmann/tint v1.0.5 github.com/mattn/go-isatty v0.0.20 github.com/muesli/termenv v0.15.2 @@ -23,6 +22,8 @@ require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fatih/color v1.18.0 // indirect + github.com/goccy/go-yaml v1.13.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/huandu/xstrings v1.5.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect diff --git a/go.sum b/go.sum index 8c02ac7..81ec444 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s= dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +gabe565.com/utils v0.0.0-20241029174609-934fb3acf91e h1:UP9HzqvfJI8n6k2XIBe95pixGA36wFx3JiVbPa8RFJE= +gabe565.com/utils v0.0.0-20241029174609-934fb3acf91e/go.mod h1:i93PnuU9qG5dNr79l+206DXeYn+E6r7FrqNGSLhjrvw= github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0= @@ -10,6 +12,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.23 h1:4M6+isWdcStXEf15G/RbrMPOQj1dZ7HPZCGwE4kOeP0= +github.com/creack/pty v1.1.23/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dmarkham/enumer v1.5.10 h1:ygL0L6quiTiH1jpp68DyvsWaea6MaZLZrTTkIS++R0M= diff --git a/internal/colorize/colorize.go b/internal/colorize/colorize.go deleted file mode 100644 index 567d712..0000000 --- a/internal/colorize/colorize.go +++ /dev/null @@ -1,89 +0,0 @@ -package colorize - -import ( - "io" - "os" - "strconv" - "strings" - - "github.com/fatih/color" - "github.com/goccy/go-yaml/lexer" - "github.com/goccy/go-yaml/printer" - "github.com/mattn/go-isatty" -) - -const escape = "\x1b" - -func format(attr color.Attribute) string { - return escape + "[" + strconv.Itoa(int(attr)) + "m" -} - -func ShouldColor(w io.Writer) bool { - if os.Getenv("NO_COLOR") != "" || os.Getenv("TERM") == "dumb" { - return false - } - if f, ok := w.(*os.File); ok { - return isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd()) - } - return false -} - -func WriteString(w io.Writer, s string) error { - if ShouldColor(w) { - s = Colorize(s) - } - - _, err := io.WriteString(w, s) - return err -} - -func Printer() *printer.Printer { - return &printer.Printer{ - MapKey: func() *printer.Property { - return &printer.Property{ - Prefix: format(color.FgCyan), - Suffix: format(color.Reset), - } - }, - Anchor: func() *printer.Property { - return &printer.Property{ - Prefix: format(color.FgHiYellow), - Suffix: format(color.Reset), - } - }, - Alias: func() *printer.Property { - return &printer.Property{ - Prefix: format(color.FgHiYellow), - Suffix: format(color.Reset), - } - }, - Bool: func() *printer.Property { - return &printer.Property{ - Prefix: format(color.FgHiMagenta), - Suffix: format(color.Reset), - } - }, - String: func() *printer.Property { - return &printer.Property{ - Prefix: format(color.FgGreen), - Suffix: format(color.Reset), - } - }, - Number: func() *printer.Property { - return &printer.Property{ - Prefix: format(color.FgHiMagenta), - Suffix: format(color.Reset), - } - }, - } -} - -func Colorize(s string) string { - // https://github.com/mikefarah/yq/blob/v4.43.1/pkg/yqlib/color_print.go - tokens := lexer.Tokenize(s) - s = Printer().PrintTokens(tokens) - if !strings.HasSuffix(s, "\n") { - return s + "\n" - } - return s -}