Skip to content

Commit ed4daf0

Browse files
committed
Add newline: keyword argument in render methods
Fix #23
1 parent 6fcb046 commit ed4daf0

8 files changed

Lines changed: 79 additions & 76 deletions

File tree

lib/unicode_plot/plot.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def next_color
124124

125125
def to_s
126126
StringIO.open do |sio|
127-
render(sio)
127+
render(sio, newline: false)
128128
sio.close
129129
sio.string
130130
end

lib/unicode_plot/renderer.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def print_border_bottom(out, padding, length, border=:solid, color: :light_black
5959
class Renderer
6060
include BorderPrinter
6161

62-
def self.render(out, plot)
63-
new(plot).render(out)
62+
def self.render(out, plot, newline: true)
63+
new(plot).render(out, newline: newline)
6464
end
6565

6666
def initialize(plot)
@@ -71,13 +71,14 @@ def initialize(plot)
7171
attr_reader :plot
7272
attr_reader :out
7373

74-
def render(out)
74+
def render(out, newline: true)
7575
@out = out
7676
init_render
7777

7878
render_top
7979
render_rows
8080
render_bottom
81+
out.puts if newline
8182
end
8283

8384
private

test/test-barplot.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class BarplotTest < Test::Unit::TestCase
1515
test("colored") do
1616
data = { bar: 23, foo: 37 }
1717
plot = UnicodePlot.barplot(data: data)
18-
_, output = with_term { plot.render($stdout) }
18+
_, output = with_term { plot.render($stdout, newline: false) }
1919
assert_equal(fixture_path("barplot/default.txt").read,
2020
output)
2121

2222
plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
23-
_, output = with_term { plot.render($stdout) }
23+
_, output = with_term { plot.render($stdout, newline: false) }
2424
assert_equal(fixture_path("barplot/default.txt").read,
2525
output)
2626
end
@@ -40,7 +40,7 @@ class BarplotTest < Test::Unit::TestCase
4040
test("mixed") do
4141
data = { bar: 23.0, 2.1 => 10, foo: 37.0 }
4242
plot = UnicodePlot.barplot(data: data)
43-
_, output = with_term { plot.render($stdout) }
43+
_, output = with_term { plot.render($stdout, newline: false) }
4444
assert_equal(fixture_path("barplot/default_mixed.txt").read,
4545
output)
4646
end
@@ -53,7 +53,7 @@ class BarplotTest < Test::Unit::TestCase
5353
title: "Logscale Plot",
5454
xscale: :log10
5555
)
56-
_, output = with_term { plot.render($stdout) }
56+
_, output = with_term { plot.render($stdout, newline: false) }
5757
assert_equal(fixture_path("barplot/log10.txt").read,
5858
output)
5959
end
@@ -66,7 +66,7 @@ class BarplotTest < Test::Unit::TestCase
6666
xlabel: "custom label",
6767
xscale: :log10
6868
)
69-
_, output = with_term { plot.render($stdout) }
69+
_, output = with_term { plot.render($stdout, newline: false) }
7070
assert_equal(fixture_path("barplot/log10_label.txt").read,
7171
output)
7272
end
@@ -83,7 +83,7 @@ class BarplotTest < Test::Unit::TestCase
8383
margin: 7,
8484
padding: 3
8585
)
86-
_, output = with_term { plot.render($stdout) }
86+
_, output = with_term { plot.render($stdout, newline: false) }
8787
assert_equal(fixture_path("barplot/parameters1.txt").read,
8888
output)
8989
end
@@ -99,7 +99,7 @@ class BarplotTest < Test::Unit::TestCase
9999
padding: 3,
100100
labels: false
101101
)
102-
_, output = with_term { plot.render($stdout) }
102+
_, output = with_term { plot.render($stdout, newline: false) }
103103
assert_equal(fixture_path("barplot/parameters1_nolabels.txt").read,
104104
output)
105105
end
@@ -115,29 +115,29 @@ class BarplotTest < Test::Unit::TestCase
115115
symbol: "=",
116116
width: 60
117117
)
118-
_, output = with_term { plot.render($stdout) }
118+
_, output = with_term { plot.render($stdout, newline: false) }
119119
assert_equal(fixture_path("barplot/parameters2.txt").read,
120120
output)
121121
end
122122
end
123123

124124
test("ranges") do
125125
plot = UnicodePlot.barplot(2..6, 11..15)
126-
_, output = with_term { plot.render($stdout) }
126+
_, output = with_term { plot.render($stdout, newline: false) }
127127
assert_equal(fixture_path("barplot/ranges.txt").read,
128128
output)
129129
end
130130

131131
test("all zeros") do
132132
plot = UnicodePlot.barplot([5, 4, 3, 2, 1], [0, 0, 0, 0, 0])
133-
_, output = with_term { plot.render($stdout) }
133+
_, output = with_term { plot.render($stdout, newline: false) }
134134
assert_equal(fixture_path("barplot/edgecase_zeros.txt").read,
135135
output)
136136
end
137137

138138
test("one large") do
139139
plot = UnicodePlot.barplot([:a, :b, :c, :d], [1, 1, 1, 1000000])
140-
_, output = with_term { plot.render($stdout) }
140+
_, output = with_term { plot.render($stdout, newline: false) }
141141
assert_equal(fixture_path("barplot/edgecase_onelarge.txt").read,
142142
output)
143143
end
@@ -159,21 +159,21 @@ class BarplotTest < Test::Unit::TestCase
159159
plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
160160
assert_same(plot,
161161
UnicodePlot.barplot!(plot, ["zoom"], [90]))
162-
_, output = with_term { plot.render($stdout) }
162+
_, output = with_term { plot.render($stdout, newline: false) }
163163
assert_equal(fixture_path("barplot/default2.txt").read,
164164
output)
165165

166166
plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
167167
assert_same(plot,
168168
UnicodePlot.barplot!(plot, "zoom", 90))
169-
_, output = with_term { plot.render($stdout) }
169+
_, output = with_term { plot.render($stdout, newline: false) }
170170
assert_equal(fixture_path("barplot/default2.txt").read,
171171
output)
172172

173173
plot = UnicodePlot.barplot([:bar, :foo], [23, 37])
174174
assert_same(plot,
175175
UnicodePlot.barplot!(plot, data: { zoom: 90 }))
176-
_, output = with_term { plot.render($stdout) }
176+
_, output = with_term { plot.render($stdout, newline: false) }
177177
assert_equal(fixture_path("barplot/default2.txt").read,
178178
output)
179179
end
@@ -182,7 +182,7 @@ class BarplotTest < Test::Unit::TestCase
182182
plot = UnicodePlot.barplot(2..6, 11..15)
183183
assert_same(plot,
184184
UnicodePlot.barplot!(plot, 9..10, 20..21))
185-
_, output = with_term { plot.render($stdout) }
185+
_, output = with_term { plot.render($stdout, newline: false) }
186186
assert_equal(fixture_path("barplot/ranges2.txt").read,
187187
output)
188188
end

test/test-boxplot.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class BoxplotTest < Test::Unit::TestCase
66
sub_test_case("print to tty") do
77
test("without name") do
88
plot = UnicodePlot.boxplot([1, 2, 3, 4, 5])
9-
_, output = with_term { plot.render($stdout) }
9+
_, output = with_term { plot.render($stdout, newline: false) }
1010
assert_equal(fixture_path("boxplot/default.txt").read,
1111
output)
1212
end
1313

1414
test("with name") do
1515
plot = UnicodePlot.boxplot("series1", [1, 2, 3, 4, 5])
16-
_, output = with_term { plot.render($stdout) }
16+
_, output = with_term { plot.render($stdout, newline: false) }
1717
assert_equal(fixture_path("boxplot/default_name.txt").read,
1818
output)
1919
end
@@ -28,14 +28,14 @@ def setup
2828
end
2929

3030
test("print to tty") do
31-
_, output = with_term { @plot.render($stdout) }
31+
_, output = with_term { @plot.render($stdout, newline: false) }
3232
assert_equal(fixture_path("boxplot/default_parameters.txt").read,
3333
output)
3434
end
3535

3636
test("print to non-tty IO") do
3737
output = StringIO.open do |sio|
38-
@plot.render(sio)
38+
@plot.render(sio, newline: false)
3939
sio.close
4040
sio.string
4141
end
@@ -49,7 +49,7 @@ def setup
4949
test("with scaling") do
5050
i, max_x = data
5151
plot = UnicodePlot.boxplot([1, 2, 3, 4, 5], xlim: [0, max_x])
52-
_, output = with_term { plot.render($stdout) }
52+
_, output = with_term { plot.render($stdout, newline: false) }
5353
assert_equal(fixture_path("boxplot/scale#{i}.txt").read,
5454
output)
5555
end
@@ -63,19 +63,19 @@ def setup
6363
title: "Multi-series",
6464
xlabel: "foo",
6565
color: :yellow)
66-
_, output = with_term { plot.render($stdout) }
66+
_, output = with_term { plot.render($stdout, newline: false) }
6767
assert_equal(fixture_path("boxplot/multi1.txt").read,
6868
output)
6969

7070
assert_same(plot,
7171
UnicodePlot.boxplot!(plot, "one more", [-1, 2, 3, 4, 11]))
72-
_, output = with_term { plot.render($stdout) }
72+
_, output = with_term { plot.render($stdout, newline: false) }
7373
assert_equal(fixture_path("boxplot/multi2.txt").read,
7474
output)
7575

7676
assert_same(plot,
7777
UnicodePlot.boxplot!(plot, [4, 2, 2.5, 4, 14], name: "last one"))
78-
_, output = with_term { plot.render($stdout) }
78+
_, output = with_term { plot.render($stdout, newline: false) }
7979
assert_equal(fixture_path("boxplot/multi3.txt").read,
8080
output)
8181
end

test/test-densityplot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def setup
1616
dy2 = @dy.map {|y| y + 2 }
1717
assert_same(plot,
1818
UnicodePlot.densityplot!(plot, dx2, dy2))
19-
_, output = with_term { plot.render($stdout) }
19+
_, output = with_term { plot.render($stdout, newline: false) }
2020
expected = fixture_path("scatterplot/densityplot.txt").read
2121
assert_equal(output, expected)
2222
end
@@ -31,7 +31,7 @@ def setup
3131
dy2 = @dy.map {|y| y + 2 }
3232
assert_same(plot,
3333
UnicodePlot.densityplot!(plot, dx2, dy2, name: "bar"))
34-
_, output = with_term { plot.render($stdout) }
34+
_, output = with_term { plot.render($stdout, newline: false) }
3535
expected = fixture_path("scatterplot/densityplot_parameters.txt").read
3636
assert_equal(output, expected)
3737
end

test/test-histogram.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ def setup
99

1010
test("default") do
1111
plot = UnicodePlot.histogram(@x)
12-
_, output = with_term { plot.render($stdout) }
12+
_, output = with_term { plot.render($stdout, newline: false) }
13+
assert_equal("\n", output[-1])
1314
assert_equal(fixture_path("histogram/default.txt").read,
14-
output)
15+
output.chomp)
1516
end
1617

1718
test("nocolor") do
@@ -21,50 +22,51 @@ def setup
2122
sio.close
2223
sio.string
2324
end
25+
assert_equal("\n", output[-1])
2426
assert_equal(fixture_path("histogram/default_nocolor.txt").read,
25-
output)
27+
output.chomp)
2628
end
2729

2830
test("losed: :left") do
2931
plot = UnicodePlot.histogram(@x, closed: :left)
30-
_, output = with_term { plot.render($stdout) }
32+
_, output = with_term { plot.render($stdout, newline: false) }
3133
assert_equal(fixture_path("histogram/default.txt").read,
3234
output)
3335
end
3436

3537
test("x 100") do
3638
x100 = @x.map {|a| a * 100 }
3739
plot = UnicodePlot.histogram(x100)
38-
_, output = with_term { plot.render($stdout) }
40+
_, output = with_term { plot.render($stdout, newline: false) }
3941
assert_equal(fixture_path("histogram/default_1e2.txt").read,
4042
output)
4143
end
4244

4345
test("x0.01") do
4446
x100 = @x.map {|a| a * 0.01 }
4547
plot = UnicodePlot.histogram(x100)
46-
_, output = with_term { plot.render($stdout) }
48+
_, output = with_term { plot.render($stdout, newline: false) }
4749
assert_equal(fixture_path("histogram/default_1e-2.txt").read,
4850
output)
4951
end
5052

5153
test("xscale: :log10") do
5254
plot = UnicodePlot.histogram(@x, xscale: :log10)
53-
_, output = with_term { plot.render($stdout) }
55+
_, output = with_term { plot.render($stdout, newline: false) }
5456
assert_equal(fixture_path("histogram/log10.txt").read,
5557
output)
5658
end
5759

5860
test("xscale: :log10 with custom label") do
5961
plot = UnicodePlot.histogram(@x, xscale: :log10, xlabel: "custom label")
60-
_, output = with_term { plot.render($stdout) }
62+
_, output = with_term { plot.render($stdout, newline: false) }
6163
assert_equal(fixture_path("histogram/log10_label.txt").read,
6264
output)
6365
end
6466

6567
test("nbins: 5, closed: :right") do
6668
plot = UnicodePlot.histogram(@x, nbins: 5, closed: :right)
67-
_, output = with_term { plot.render($stdout) }
69+
_, output = with_term { plot.render($stdout, newline: false) }
6870
assert_equal(fixture_path("histogram/hist_params.txt").read,
6971
output)
7072
end
@@ -76,7 +78,7 @@ def setup
7678
color: :blue,
7779
margin: 7,
7880
padding: 3)
79-
_, output = with_term { plot.render($stdout) }
81+
_, output = with_term { plot.render($stdout, newline: false) }
8082
assert_equal(fixture_path("histogram/parameters1.txt").read,
8183
output)
8284
end
@@ -89,7 +91,7 @@ def setup
8991
margin: 7,
9092
padding: 3,
9193
labels: false)
92-
_, output = with_term { plot.render($stdout) }
94+
_, output = with_term { plot.render($stdout, newline: false) }
9395
assert_equal(fixture_path("histogram/parameters1_nolabels.txt").read,
9496
output)
9597
end
@@ -102,7 +104,7 @@ def setup
102104
border: :solid,
103105
symbol: "=",
104106
width: 50)
105-
_, output = with_term { plot.render($stdout) }
107+
_, output = with_term { plot.render($stdout, newline: false) }
106108
assert_equal(fixture_path("histogram/parameters2.txt").read,
107109
output)
108110
end

0 commit comments

Comments
 (0)