Skip to content

Commit

Permalink
add markdwon formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ch1aki committed May 22, 2020
1 parent 1341826 commit d4e8ec4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/st.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ func PlainTextFormatter(writer io.Writer, header []string, data [][]string) {
table.AppendBulk(data)
table.Render()
}

func MarkdownTableFormatter(writer io.Writer, header []string, data [][]string) {
table := tablewriter.NewWriter(writer)
table.SetHeader(header)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.AppendBulk(data)
table.Render()
}
27 changes: 27 additions & 0 deletions lib/st_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,30 @@ func TestPlainTextFromatter(t *testing.T) {
t.Errorf("expected != actual\n%s\n", diff)
}
}

func TestMarkdownTableFromatter(t *testing.T) {
stdout := new(bytes.Buffer)
st := St{
Formatter: MarkdownTableFormatter,

Count: true,
Min: true,
Max: true,
Sum: true,
Mean: true,
Stddev: true,
}

for _, v := range []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} {
st.Process(v)
}
st.Output(stdout)

expected := `| N | MIN | MAX | SUM | MEAN | STDDEV |
|----|-----|-----|-----|------|--------------------|
| 10 | 0 | 9 | 45 | 4.5 | 3.0276503540974917 |
`
if diff := cmp.Diff(expected, stdout.String()); diff != "" {
t.Errorf("expected != actual\n%s\n", diff)
}
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type options struct {
Mean bool `short:"m" long:"mean" description:"mean"`
Stddev bool `long:"sd" description:"standard deviation"`
Vatiance bool `long:"variance" description:"variance"`
Markdown bool `long:"markdown" description:"markdown table format"`
}

func main() {
Expand All @@ -35,8 +36,13 @@ func main() {
os.Exit(1)
}

f := st.PlainTextFormatter
if opts.Markdown {
f = st.MarkdownTableFormatter
}

s := st.St{
Formatter: st.PlainTextFormatter,
Formatter: f,

Count: opts.Count,
Min: opts.Min,
Expand Down

0 comments on commit d4e8ec4

Please sign in to comment.