From 32b8793d14ec2e93c0ede25010724bc38f8aab30 Mon Sep 17 00:00:00 2001 From: b5 Date: Thu, 23 May 2019 07:23:09 -0400 Subject: [PATCH] fix(cmd): don't use FgWhite, breaks light-colored termimals So, I wanted to solve this problem the "right" way, by making sure we're always writing ANSI codes that we know will print. After some digging, this turns out to be a bigger problem than I expected, and the easier answer is to avoid using colors like FgWhite or FgBlack. Thankfully there are only a few places in our codebase that use these, and one of them was dead code. --- cmd/diff.go | 31 +++--------------------- cmd/print.go | 66 +++++++++++++++++++++------------------------------- 2 files changed, 30 insertions(+), 67 deletions(-) diff --git a/cmd/diff.go b/cmd/diff.go index 51a9f97f1..30dbc03d0 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -1,11 +1,8 @@ package cmd import ( - "bytes" "encoding/json" - "github.com/fatih/color" - "github.com/qri-io/deepdiff" "github.com/qri-io/ioes" "github.com/qri-io/qri/lib" "github.com/spf13/cobra" @@ -101,16 +98,14 @@ func (o *DiffOptions) Complete(f Factory, args []string) (err error) { // Run executes the diff command func (o *DiffOptions) Run() (err error) { - var stats, text string - p := &lib.DiffParams{ LeftPath: o.Left, RightPath: o.Right, Selector: o.Selector, } - res := lib.DiffResponse{} - if err = o.DatasetRequests.Diff(p, &res); err != nil { + res := &lib.DiffResponse{} + if err = o.DatasetRequests.Diff(p, res); err != nil { return err } @@ -119,25 +114,5 @@ func (o *DiffOptions) Run() (err error) { return } - // TODO (b5): this reading from a package variable is pretty hacky :/ - if color.NoColor { - stats = deepdiff.FormatPrettyStats(res.Stat) - if !o.Summary { - text, err = deepdiff.FormatPretty(res.Diff) - if err != nil { - return err - } - } - } else { - stats = deepdiff.FormatPrettyStatsColor(res.Stat) - if !o.Summary { - text, err = deepdiff.FormatPrettyColor(res.Diff) - if err != nil { - return err - } - } - } - buf := bytes.NewBuffer([]byte(stats + "\n" + text)) - printToPager(o.Out, buf) - return + return printDiff(o.Out, res, o.Summary) } diff --git a/cmd/print.go b/cmd/print.go index 31cdb5496..29e9e1c40 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -10,8 +10,8 @@ import ( "strings" "github.com/fatih/color" + "github.com/qri-io/deepdiff" "github.com/qri-io/qri/lib" - "github.com/qri-io/qri/repo" ) const ( @@ -41,7 +41,7 @@ func printSuccess(w io.Writer, msg string, params ...interface{}) { } func printInfo(w io.Writer, msg string, params ...interface{}) { - fmt.Fprintln(w, color.New(color.FgWhite).Sprintf(msg, params...)) + fmt.Fprintln(w, fmt.Sprintf(msg, params...)) } func printWarning(w io.Writer, msg string, params ...interface{}) { @@ -166,43 +166,6 @@ func printByteInfo(n int) string { return fmt.Sprintf("%v %s", length.value, length.name) } -func printDatasetRefInfo(w io.Writer, i int, ref repo.DatasetRef) { - white := color.New(color.FgWhite).SprintFunc() - cyan := color.New(color.FgCyan).SprintFunc() - blue := color.New(color.FgBlue).SprintFunc() - ds := ref.Dataset - - fmt.Fprintf(w, "%s %s\n", cyan(i), white(ref.AliasString())) - if ds != nil && ds.Meta != nil && ds.Meta.Title != "" { - fmt.Fprintf(w, " %s\n", blue(ds.Meta.Title)) - } - if ref.Path != "" { - fmt.Fprintf(w, " %s\n", ref.Path) - } - if ds != nil && ds.Structure != nil { - fmt.Fprintf(w, " %s", printByteInfo(ds.Structure.Length)) - if ds.Structure.Entries == 1 { - fmt.Fprintf(w, ", %d entry", ds.Structure.Entries) - } else { - fmt.Fprintf(w, ", %d entries", ds.Structure.Entries) - } - if ds.Structure.ErrCount == 1 { - fmt.Fprintf(w, ", %d error", ds.Structure.ErrCount) - } else { - fmt.Fprintf(w, ", %d errors", ds.Structure.ErrCount) - } - if ds.NumVersions == 0 { - // nothing - } else if ds.NumVersions == 1 { - fmt.Fprintf(w, ", %d version", ds.NumVersions) - } else { - fmt.Fprintf(w, ", %d versions", ds.NumVersions) - } - } - - fmt.Fprintf(w, "\n") -} - func prompt(w io.Writer, r io.Reader, msg string) string { var input string printInfo(w, msg) @@ -253,3 +216,28 @@ func doesCommandExist(cmdName string) bool { } return true } + +func printDiff(w io.Writer, res *lib.DiffResponse, summaryOnly bool) (err error) { + var stats, text string + // TODO (b5): this reading from a package variable is pretty hacky :/ + if color.NoColor { + stats = deepdiff.FormatPrettyStats(res.Stat) + if !summaryOnly { + text, err = deepdiff.FormatPretty(res.Diff) + if err != nil { + return err + } + } + } else { + stats = deepdiff.FormatPrettyStatsColor(res.Stat) + if !summaryOnly { + text, err = deepdiff.FormatPrettyColor(res.Diff) + if err != nil { + return err + } + } + } + buf := bytes.NewBuffer([]byte(stats + "\n" + text)) + printToPager(w, buf) + return nil +}