From d8cbfd1b90858512b5ecaa4e13734f56593fc5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Fri, 29 Jun 2018 16:50:05 +0200 Subject: [PATCH] One color package to rule them all. --- internal/cli/run/run.go | 4 ++-- internal/colors/colors.go | 40 ------------------------------------- internal/onboard/onboard.go | 10 +++++----- internal/util/util.go | 6 +++--- internal/util/util_test.go | 3 +-- nettests/nettests.go | 34 +++++++++++++++---------------- 6 files changed, 28 insertions(+), 69 deletions(-) delete mode 100644 internal/colors/colors.go diff --git a/internal/cli/run/run.go b/internal/cli/run/run.go index 103d499..7c8a9aa 100644 --- a/internal/cli/run/run.go +++ b/internal/cli/run/run.go @@ -9,8 +9,8 @@ import ( "github.com/alecthomas/kingpin" "github.com/apex/log" + "github.com/fatih/color" "github.com/ooni/probe-cli/internal/cli/root" - "github.com/ooni/probe-cli/internal/colors" "github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/nettests" "github.com/ooni/probe-cli/nettests/groups" @@ -22,7 +22,7 @@ func init() { var nettestGroupNames []string for name := range groups.NettestGroups { - nettestGroupNames = append(nettestGroupNames, colors.Blue(name)) + nettestGroupNames = append(nettestGroupNames, color.BlueString(name)) } nettestGroup := cmd.Arg("name", diff --git a/internal/colors/colors.go b/internal/colors/colors.go deleted file mode 100644 index 09b0c58..0000000 --- a/internal/colors/colors.go +++ /dev/null @@ -1,40 +0,0 @@ -package colors - -import ( - color "github.com/aybabtme/rgbterm" -) - -// Gray string. -func Gray(s string) string { - return color.FgString(s, 150, 150, 150) -} - -// Blue string. -func Blue(s string) string { - return color.FgString(s, 77, 173, 247) -} - -// Cyan string. -func Cyan(s string) string { - return color.FgString(s, 34, 184, 207) -} - -// Green string. -func Green(s string) string { - return color.FgString(s, 0, 200, 255) -} - -// Red string. -func Red(s string) string { - return color.FgString(s, 194, 37, 92) -} - -// Yellow string. -func Yellow(s string) string { - return color.FgString(s, 252, 196, 25) -} - -// Purple string. -func Purple(s string) string { - return color.FgString(s, 96, 97, 190) -} diff --git a/internal/onboard/onboard.go b/internal/onboard/onboard.go index fa8a133..f34a9ff 100644 --- a/internal/onboard/onboard.go +++ b/internal/onboard/onboard.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/apex/log" + "github.com/fatih/color" "github.com/ooni/probe-cli/config" - "github.com/ooni/probe-cli/internal/colors" "github.com/ooni/probe-cli/internal/output" survey "gopkg.in/AlecAivazis/survey.v1" ) @@ -42,10 +42,10 @@ func Onboarding(config *config.Config) error { } survey.AskOne(quiz1, &answer, nil) if answer != "true" { - output.Paragraph(colors.Red("Actually...")) + output.Paragraph(color.RedString("Actually...")) output.Paragraph("OONI Probe is not a privacy tool. Therefore, anyone monitoring your internet activity may be able to see which software you are running.") } else { - output.Paragraph(colors.Blue("Good job!")) + output.Paragraph(color.BlueString("Good job!")) } answer = "" quiz2 := &survey.Select{ @@ -55,10 +55,10 @@ func Onboarding(config *config.Config) error { } survey.AskOne(quiz2, &answer, nil) if answer != "true" { - output.Paragraph(colors.Red("Actually...")) + output.Paragraph(color.RedString("Actually...")) output.Paragraph("The network data you will collect will automatically be published to increase transparency of internet censorship (unless you opt-out in the settings).") } else { - output.Paragraph(colors.Blue("Well done!")) + output.Paragraph(color.BlueString("Well done!")) } changeDefaults := false diff --git a/internal/util/util.go b/internal/util/util.go index d647138..c47d301 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -9,17 +9,17 @@ import ( "unicode" "unicode/utf8" - "github.com/ooni/probe-cli/internal/colors" + "github.com/fatih/color" ) // Log outputs a log message. func Log(msg string, v ...interface{}) { - fmt.Printf(" %s\n", colors.Purple(fmt.Sprintf(msg, v...))) + fmt.Printf(" %s\n", color.CyanString(msg, v...)) } // Fatal error func Fatal(err error) { - fmt.Fprintf(os.Stderr, "\n %s %s\n\n", colors.Red("Error:"), err) + fmt.Fprintf(os.Stderr, "\n %s %s\n\n", color.RedString("Error:"), err) os.Exit(1) } diff --git a/internal/util/util_test.go b/internal/util/util_test.go index 4a08074..3a567c7 100644 --- a/internal/util/util_test.go +++ b/internal/util/util_test.go @@ -4,14 +4,13 @@ import ( "testing" "github.com/fatih/color" - ocolor "github.com/ooni/probe-cli/internal/colors" ) func TestEscapeAwareRuneCountInString(t *testing.T) { var bold = color.New(color.Bold) var myColor = color.New(color.FgBlue) - s := myColor.Sprintf("•ABC%s%s", bold.Sprintf("DEF"), ocolor.Red("GHI")) + s := myColor.Sprintf("•ABC%s%s", bold.Sprintf("DEF"), "\x1B[00;38;5;244m\x1B[m\x1B[00;38;5;33mGHI\x1B[0m") count := EscapeAwareRuneCountInString(s) if count != 10 { t.Errorf("Count was incorrect, got: %d, want: %d.", count, 10) diff --git a/nettests/nettests.go b/nettests/nettests.go index c32de5d..b77bb56 100644 --- a/nettests/nettests.go +++ b/nettests/nettests.go @@ -7,11 +7,11 @@ import ( "path/filepath" "github.com/apex/log" + "github.com/fatih/color" "github.com/measurement-kit/go-measurement-kit" homedir "github.com/mitchellh/go-homedir" ooni "github.com/ooni/probe-cli" "github.com/ooni/probe-cli/internal/cli/version" - "github.com/ooni/probe-cli/internal/colors" "github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/output" "github.com/ooni/probe-cli/utils" @@ -132,7 +132,7 @@ func (c *Controller) Init(nt *mk.Nettest) error { log.Debugf("GeoIPCountryPath: %s", nt.Options.GeoIPCountryPath) nt.On("log", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) level := e.Value.LogLevel msg := e.Value.Message @@ -163,7 +163,7 @@ func (c *Controller) Init(nt *mk.Nettest) error { }) nt.On("status.geoip_lookup", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) msmtTemplate.ASN = e.Value.ProbeASN msmtTemplate.IP = e.Value.ProbeIP @@ -171,7 +171,7 @@ func (c *Controller) Init(nt *mk.Nettest) error { }) nt.On("status.measurement_start", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) idx := e.Value.Idx msmt, err := database.CreateMeasurement(c.Ctx.DB, msmtTemplate, e.Value.Input) @@ -183,64 +183,64 @@ func (c *Controller) Init(nt *mk.Nettest) error { }) nt.On("status.progress", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) c.OnProgress(e.Value.Percentage, e.Value.Message) }) nt.On("status.update.*", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) }) // XXX should these be made into permanent failures? nt.On("failure.asn_lookup", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) log.Debugf("%v", e.Value) }) nt.On("failure.cc_lookup", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) log.Debugf("%v", e.Value) }) nt.On("failure.ip_lookup", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) log.Debugf("%v", e.Value) }) nt.On("failure.resolver_lookup", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) log.Debugf("%v", e.Value) }) nt.On("failure.report_create", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) log.Debugf("%v", e.Value) }) nt.On("failure.report_close", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) log.Debugf("%v", e.Value) }) nt.On("failure.startup", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) c.msmts[e.Value.Idx].Failed(c.Ctx.DB, e.Value.Failure) }) nt.On("failure.measurement", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) c.msmts[e.Value.Idx].Failed(c.Ctx.DB, e.Value.Failure) }) nt.On("failure.measurement_submission", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) failure := e.Value.Failure c.msmts[e.Value.Idx].UploadFailed(c.Ctx.DB, failure) }) nt.On("status.measurement_submission", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) if err := c.msmts[e.Value.Idx].UploadSucceeded(c.Ctx.DB); err != nil { log.WithError(err).Error("failed to mark msmt as uploaded") @@ -248,7 +248,7 @@ func (c *Controller) Init(nt *mk.Nettest) error { }) nt.On("status.measurement_done", func(e mk.Event) { - log.Debugf(colors.Red(e.Key)) + log.Debugf(color.RedString(e.Key)) if err := c.msmts[e.Value.Idx].Done(c.Ctx.DB); err != nil { log.WithError(err).Error("failed to mark msmt as done")