From 1c05d46bd68cbc5ac64214ab8d7c0d2134fcfc42 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Wed, 26 Jun 2024 01:04:54 -0500 Subject: [PATCH] chore(config): Reuse `colorize.ShouldColor` --- internal/colorize/colorize.go | 4 ++-- internal/config/log.go | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/colorize/colorize.go b/internal/colorize/colorize.go index 8bb9547..d7f7d48 100644 --- a/internal/colorize/colorize.go +++ b/internal/colorize/colorize.go @@ -18,7 +18,7 @@ func format(attr color.Attribute) string { return escape + "[" + strconv.Itoa(int(attr)) + "m" } -func shouldColor(w io.Writer) bool { +func ShouldColor(w io.Writer) bool { if os.Getenv("NO_COLOR") != "" || os.Getenv("TERM") == "dumb" { return false } @@ -29,7 +29,7 @@ func shouldColor(w io.Writer) bool { } func WriteString(w io.Writer, s string) error { - if shouldColor(w) { + if ShouldColor(w) { s = Colorize(s) } diff --git a/internal/config/log.go b/internal/config/log.go index 922cce4..39a4783 100644 --- a/internal/config/log.go +++ b/internal/config/log.go @@ -3,10 +3,9 @@ package config import ( "fmt" "io" - "os" + "github.com/clevyr/yampl/internal/colorize" "github.com/fatih/color" - "github.com/mattn/go-isatty" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -34,10 +33,8 @@ func logFormat(out io.Writer, format string) io.Writer { var useColor bool switch format { case Auto: - if w, ok := out.(*os.File); ok { - if useColor = isatty.IsTerminal(w.Fd()); !useColor { - break - } + if useColor = colorize.ShouldColor(out); !useColor { + break } fallthrough case Color: