Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use text/tabwriter to align human output #317

Merged
merged 6 commits into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions prettyprinters/human/human.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"strings"
"sync"
"text/tabwriter"
"text/template"

"github.com/asteris-llc/converge/graph"
Expand Down Expand Up @@ -51,6 +52,7 @@ func NewFiltered(f FilterFunc) *Printer {
// InitColors initializes the colors used by the human printer
func (p *Printer) InitColors() {
reset := "\x1b[0m"
p.funcsMapWrite("bold", p.styled(func(in string) string { return "\x1b[1m" + in + reset }))
p.funcsMapWrite("black", p.styled(func(in string) string { return "\x1b[30m" + in + reset }))
p.funcsMapWrite("red", p.styled(func(in string) string { return "\x1b[31m" + in + reset }))
p.funcsMapWrite("green", p.styled(func(in string) string { return "\x1b[32m" + in + reset }))
Expand Down Expand Up @@ -124,16 +126,22 @@ func (p *Printer) DrawNode(g *graph.Graph, id string) (pp.Renderable, error) {
Has Changes: {{if .HasChanges}}{{yellow "yes"}}{{else}}no{{end}}
Changes:
{{- range $key, $values := .Changes}}
{{cyan $key}}:{{diff ($values.Original) ($values.Current)}}
{{cyan $key}}: {{diff ($values.Original) ($values.Current) | bold}}
{{- else}} No changes {{- end}}

`)
if err != nil {
return pp.HiddenString(), err
}

var out bytes.Buffer
err = tmpl.Execute(&out, &printerNode{ID: id, Printable: printable})
var intermediate, out bytes.Buffer
err = tmpl.Execute(&intermediate, &printerNode{ID: id, Printable: printable})
if err != nil {
return pp.HiddenString(), err
}

tabWriter := tabwriter.NewWriter(&out, 1, 1, 1, ' ', 0)
_, err = tabWriter.Write(intermediate.Bytes())

return &out, err
}
Expand Down Expand Up @@ -166,7 +174,7 @@ func (p *Printer) diff(before, after string) (string, error) {
// remember when modifying these that diff is responsible for leading
// whitespace
if !strings.Contains(strings.TrimSpace(before), "\n") && !strings.Contains(strings.TrimSpace(after), "\n") {
return fmt.Sprintf(" %q => %q", strings.TrimSpace(before), strings.TrimSpace(after)), nil
return fmt.Sprintf("%q\t%s\t%q", strings.TrimSpace(before), funcs["bold"].(func(string) string)("=>"), strings.TrimSpace(after)), nil
}

tmpl, err := p.template(`before:
Expand Down
6 changes: 3 additions & 3 deletions prettyprinters/human/human_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestDrawNodeNoChanges(t *testing.T) {
testDrawNodes(
t,
Printable{},
"root:\n\tMessages:\n\tHas Changes: no\n\tChanges: No changes\n\n",
"root:\n Messages:\n Has Changes: no\n Changes: No changes\n\n",
)
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func TestDrawNodeChanges(t *testing.T) {
testDrawNodes(
t,
Printable{"a": "b"},
"root:\n\tMessages:\n\tHas Changes: yes\n\tChanges:\n\t\ta: \"\" => \"b\"\n\n",
"root:\n Messages:\n Has Changes: yes\n Changes:\n a: \"\" => \"b\"\n\n",
)
}

Expand All @@ -197,7 +197,7 @@ func TestDrawNodeError(t *testing.T) {
testDrawNodes(
t,
Printable{"error": "x"},
"root:\n\tError: x\n\tMessages:\n\tHas Changes: yes\n\tChanges:\n\t\terror: \"\" => \"x\"\n\n",
"root:\n Error: x\n Messages:\n Has Changes: yes\n Changes:\n error: \"\" => \"x\"\n\n",
)
}

Expand Down