From 004ab1e9db2979ce6813d1e2c27175cb48331b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 27 Nov 2019 18:58:23 +0100 Subject: [PATCH 1/2] Fix color output to Git Bash --- utils/color.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/utils/color.go b/utils/color.go index fb8479734..0a7e346a0 100644 --- a/utils/color.go +++ b/utils/color.go @@ -1,15 +1,28 @@ package utils import ( + "os" + "github.com/mattn/go-isatty" "github.com/mgutz/ansi" - "os" ) +var _isStdoutTerminal = false +var checkedTerminal = false + +func isStdoutTerminal() bool { + if !checkedTerminal { + fd := os.Stdout.Fd() + _isStdoutTerminal = isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd) + checkedTerminal = true + } + return _isStdoutTerminal +} + func makeColorFunc(color string) func(string) string { return func(arg string) string { output := arg - if isatty.IsTerminal(os.Stdout.Fd()) { + if isStdoutTerminal() { output = ansi.Color(color+arg+ansi.Reset, "") } @@ -29,7 +42,7 @@ var Gray = makeColorFunc(ansi.LightBlack) func Bold(arg string) string { output := arg - if isatty.IsTerminal(os.Stdout.Fd()) { + if isStdoutTerminal() { // This is really annoying. If you just define Bold as ColorFunc("+b") it will properly bold but // will not use the default color, resulting in black and probably unreadable text. This forces // the default color before bolding. From a0458956c02e5b6dbc2689eb0fa078ea870a75d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 28 Nov 2019 11:55:14 +0100 Subject: [PATCH 2/2] Add docs to color funcs --- utils/color.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/utils/color.go b/utils/color.go index 097bad9cf..ed22bc3e9 100644 --- a/utils/color.go +++ b/utils/color.go @@ -36,11 +36,26 @@ func makeColorFunc(color string) func(string) string { } } +// Magenta outputs ANSI color if stdout is a tty var Magenta = makeColorFunc("magenta") + +// Cyan outputs ANSI color if stdout is a tty var Cyan = makeColorFunc("cyan") + +// Red outputs ANSI color if stdout is a tty var Red = makeColorFunc("red") + +// Yellow outputs ANSI color if stdout is a tty var Yellow = makeColorFunc("yellow") + +// Blue outputs ANSI color if stdout is a tty var Blue = makeColorFunc("blue") + +// Green outputs ANSI color if stdout is a tty var Green = makeColorFunc("green") + +// Gray outputs ANSI color if stdout is a tty var Gray = makeColorFunc("black+h") + +// Bold outputs ANSI color if stdout is a tty var Bold = makeColorFunc("default+b")