Skip to content

Commit

Permalink
make caption centred
Browse files Browse the repository at this point in the history
fixes #22
  • Loading branch information
guptarohit committed Sep 13, 2020
1 parent bd6114e commit 67bb1a4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion asciigraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func Plot(series []float64, options ...Option) string {
// add caption if not empty
if config.Caption != "" {
lines.WriteRune('\n')
lines.WriteString(strings.Repeat(" ", config.Offset+maxWidth+2))
lines.WriteString(strings.Repeat(" ", config.Offset+maxWidth))
if len(config.Caption) < len(series) {
lines.WriteString(strings.Repeat(" ", (len(series)-len(config.Caption))/2))
}
lines.WriteString(config.Caption)
}

Expand Down
8 changes: 4 additions & 4 deletions asciigraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func TestPlot(t *testing.T) {
0.00 ┤ ││ ╰╯
-1.00 ┤ ││
-2.00 ┤ ╰╯
Plot using asciigraph.`},
Plot using asciigraph.`},
{
[]float64{.2, .1, .2, 2, -.9, .7, .91, .3, .7, .4, .5},
[]Option{Caption("Plot using asciigraph.")},
` 2.00 ┤ ╭╮ ╭╮
0.55 ┼──╯│╭╯╰───
-0.90 ┤ ╰╯
Plot using asciigraph.`},
Plot using asciigraph.`},
{
[]float64{2, 1, 1, 2, -2, 5, 7, 11, 3, 7, 1},
[]Option{Height(4), Offset(3)},
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestPlot(t *testing.T) {
0.23 ┤ ╰╮ ╭╯ ╰╮│ ╰╮╭╯ ╰╮ ╭╯
0.20 ┤ ╰╮│ ╰╯ ╰╯ │╭╯
0.16 ┤ ╰╯ ╰╯
Plot with custom height & width.`},
Plot with custom height & width.`},
{
[]float64{
0, 0, 0, 0, 1.5, 0, 0, -0.5, 9, -3, 0, 0, 1, 2, 1, 0, 0, 0, 0,
Expand All @@ -136,7 +136,7 @@ func TestPlot(t *testing.T) {
-0.40 ┼───╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───────╯╰──╯│╭─╯ ╰───
-1.70 ┤ ││ ││ ││
-3.00 ┤ ╰╯ ╰╯ ╰╯
I'm a doctor, not an engineer.`},
I'm a doctor, not an engineer.`},
{
[]float64{-5, -2, -3, -4, 0, -5, -6, -7, -8, 0, -9, -3, -5, -2, -9, -3, -1},
nil,
Expand Down

0 comments on commit 67bb1a4

Please sign in to comment.