From 655f4a7d437910f7c59f32cd146bb80f9d98c743 Mon Sep 17 00:00:00 2001 From: Jim Raden Date: Tue, 11 Jul 2023 12:26:00 -0400 Subject: [PATCH 1/5] Remove unused golang.org/x/net --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index 72ed1d0..9d6016f 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.18 require ( github.com/smartystreets/goconvey v1.7.2 github.com/spf13/cobra v1.3.0 - golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d ) require ( diff --git a/go.sum b/go.sum index 3b4274e..a28c65b 100644 --- a/go.sum +++ b/go.sum @@ -432,8 +432,6 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d h1:1n1fc535VhN8SYtD4cDUyNlfpAF2ROMM9+11equK3hs= -golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= From 58640f1fae46764ab39381fe96c1225061f2709b Mon Sep 17 00:00:00 2001 From: Jim Raden Date: Tue, 11 Jul 2023 12:13:19 -0400 Subject: [PATCH 2/5] Initial crude colors version --- ping/ping.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ping/ping.go b/ping/ping.go index 431ff21..1fcd304 100644 --- a/ping/ping.go +++ b/ping/ping.go @@ -255,8 +255,11 @@ func (p *Pinger) logStats(stats *Stats) { status = "connected" } + const colorRed = "\033[0;31m" + const colorNone = "\033[0m" + if stats.Error != nil { - _, _ = fmt.Fprintf(p.out, "Ping %s(%s) %s(%s) - time=%s dns=%s", p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration) + _, _ = fmt.Fprintf(p.out, "%sPing %s(%s) %s(%s) - time=%s dns=%s%s", colorRed, p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration, colorNone) } else { _, _ = fmt.Fprintf(p.out, "Ping %s(%s) %s - time=%s dns=%s", p.url.String(), stats.Address, status, stats.Duration, stats.DNSDuration) } From 9fb29a4810bd33d8f37580650b90d09719edfaf8 Mon Sep 17 00:00:00 2001 From: Jim Raden Date: Tue, 11 Jul 2023 12:27:30 -0400 Subject: [PATCH 3/5] Squelch color output if not a TTY --- go.mod | 2 ++ go.sum | 4 ++++ ping/ping.go | 18 +++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9d6016f..7acbe06 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,8 @@ require ( github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/jtolds/gls v4.20.0+incompatible // indirect + github.com/mattn/go-isatty v0.0.19 github.com/smartystreets/assertions v1.2.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sys v0.6.0 // indirect; indirect) ) diff --git a/go.sum b/go.sum index a28c65b..4639856 100644 --- a/go.sum +++ b/go.sum @@ -257,6 +257,8 @@ github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcME github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso= @@ -525,6 +527,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/ping/ping.go b/ping/ping.go index 1fcd304..03c999a 100644 --- a/ping/ping.go +++ b/ping/ping.go @@ -15,6 +15,8 @@ import ( "strings" "sync" "time" + + "github.com/mattn/go-isatty" ) var pinger = map[Protocol]Factory{} @@ -259,7 +261,11 @@ func (p *Pinger) logStats(stats *Stats) { const colorNone = "\033[0m" if stats.Error != nil { - _, _ = fmt.Fprintf(p.out, "%sPing %s(%s) %s(%s) - time=%s dns=%s%s", colorRed, p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration, colorNone) + if isTerminal(p.out) { + _, _ = fmt.Fprintf(p.out, "%sPing %s(%s) %s(%s) - time=%s dns=%s%s", colorRed, p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration, colorNone) + } else { + _, _ = fmt.Fprintf(p.out, "Ping %s(%s) %s(%s) - time=%s dns=%s", p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration) + } } else { _, _ = fmt.Fprintf(p.out, "Ping %s(%s) %s - time=%s dns=%s", p.url.String(), stats.Address, status, stats.Duration, stats.DNSDuration) } @@ -272,6 +278,16 @@ func (p *Pinger) logStats(stats *Stats) { } } +func isTerminal(out io.Writer) bool { + if out == nil { + return false + } + if f, ok := out.(*os.File); ok { + return isatty.IsTerminal(f.Fd()) + } + return false +} + // Result ... type Result struct { Counter int From 91dd17be0a446a3aaf430d19460efc122c2b17d5 Mon Sep 17 00:00:00 2001 From: Jim Raden Date: Tue, 11 Jul 2023 12:29:28 -0400 Subject: [PATCH 4/5] DRY up the color code --- ping/ping.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ping/ping.go b/ping/ping.go index 03c999a..b469bd6 100644 --- a/ping/ping.go +++ b/ping/ping.go @@ -261,11 +261,15 @@ func (p *Pinger) logStats(stats *Stats) { const colorNone = "\033[0m" if stats.Error != nil { + var colorBefore, colorAfter string if isTerminal(p.out) { - _, _ = fmt.Fprintf(p.out, "%sPing %s(%s) %s(%s) - time=%s dns=%s%s", colorRed, p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration, colorNone) + colorBefore = colorRed + colorAfter = colorNone } else { - _, _ = fmt.Fprintf(p.out, "Ping %s(%s) %s(%s) - time=%s dns=%s", p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration) + colorBefore = "" + colorAfter = "" } + _, _ = fmt.Fprintf(p.out, "%sPing %s(%s) %s(%s) - time=%s dns=%s%s", colorBefore, p.url.String(), stats.Address, status, p.formatError(stats.Error), stats.Duration, stats.DNSDuration, colorAfter) } else { _, _ = fmt.Fprintf(p.out, "Ping %s(%s) %s - time=%s dns=%s", p.url.String(), stats.Address, status, stats.Duration, stats.DNSDuration) } From b66d0945a833f886d14b41a6f08301ef9a40e1c5 Mon Sep 17 00:00:00 2001 From: Jim Raden Date: Tue, 11 Jul 2023 12:36:18 -0400 Subject: [PATCH 5/5] Revert "Remove unused golang.org/x/net", which is outside the scope of this feature This reverts commit 655f4a7d437910f7c59f32cd146bb80f9d98c743. --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 7acbe06..0032487 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.18 require ( github.com/smartystreets/goconvey v1.7.2 github.com/spf13/cobra v1.3.0 + golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d ) require ( diff --git a/go.sum b/go.sum index 4639856..51c0c11 100644 --- a/go.sum +++ b/go.sum @@ -434,6 +434,8 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8= golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d h1:1n1fc535VhN8SYtD4cDUyNlfpAF2ROMM9+11equK3hs= +golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=