Skip to content

Commit

Permalink
fix(cmd): don't use FgWhite, breaks light-colored termimals
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b5 committed May 23, 2019
1 parent 1b68c3a commit 32b8793
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 67 deletions.
31 changes: 3 additions & 28 deletions cmd/diff.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
66 changes: 27 additions & 39 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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{}) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 32b8793

Please sign in to comment.