One color package to rule them all.

This commit is contained in:
Arturo Filastò 2018-06-29 16:50:05 +02:00
parent c4c13cb279
commit d8cbfd1b90
6 changed files with 28 additions and 69 deletions

View File

@ -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",

View File

@ -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)
}

View File

@ -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

View File

@ -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)
}

View File

@ -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)

View File

@ -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")