Skip to content

Commit

Permalink
Merge pull request #453 from mesg-foundation/fix/pretty-json
Browse files Browse the repository at this point in the history
Fix PrettyJson colorize function
  • Loading branch information
antho1404 authored Sep 12, 2018
2 parents 1481dad + 198969f commit 00c3c1a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
6 changes: 3 additions & 3 deletions commands/service_dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ loop:
case e := <-listenEventsC:
fmt.Printf("Receive event %s: %s\n",
pretty.Success(e.EventKey),
pretty.ColorizeJSON(pretty.FgCyan, pretty.FgMagenta, []byte(e.EventData)),
pretty.ColorizeJSON(pretty.FgCyan, nil, []byte(e.EventData)),
)
case err := <-eventsErrC:
fmt.Fprintf(os.Stderr, "%s Listening events error: %s", pretty.FailSign, err)
case r := <-listenResultsC:
fmt.Printf("Receive result %s %s with data\n%s\n",
fmt.Printf("Receive result %s %s: %s\n",
pretty.Success(r.TaskKey),
pretty.Colorize(color.New(color.FgCyan), r.OutputKey),
pretty.ColorizeJSON(pretty.FgBlue, pretty.FgMagenta, []byte(r.OutputData)),
pretty.ColorizeJSON(pretty.FgCyan, nil, []byte(r.OutputData)),
)
case err := <-resultsErrC:
fmt.Fprintf(os.Stderr, "%s Listening results error: %s", pretty.FailSign, err)
Expand Down
19 changes: 8 additions & 11 deletions utils/pretty/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (p *Pretty) ColorizeJSON(keyColor *color.Color, valueColor *color.Color, da

var (
in map[string]interface{}
out map[string]interface{}
out []string
)

if json.Unmarshal(data, &in) != nil {
Expand All @@ -223,17 +223,14 @@ func (p *Pretty) ColorizeJSON(keyColor *color.Color, valueColor *color.Color, da
if valueColor == nil {
valueColor = color.New()
}

for k, v := range in {
out[keyColor.Sprint(k)] = valueColor.Sprint(v)
for key, value := range in {
out = append(out, fmt.Sprintf(
`"%v": "%v"`,
keyColor.Sprint(key),
valueColor.Sprint(value),
))
}

b, err := json.Marshal(out)
if err != nil {
return data
}

return b
return []byte(fmt.Sprintf("{ %v }", strings.Join(out, ", ")))
}

// Progress prints spinner with the given message while calling fn function.
Expand Down

0 comments on commit 00c3c1a

Please sign in to comment.