Skip to content

Commit fc5d4b3

Browse files
committed
Add use_color option to show_plot
1 parent 1e53022 commit fc5d4b3

2 files changed

Lines changed: 30 additions & 2 deletions

File tree

‎spec/unicode_plot_spec.cr‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,30 @@ describe UnicodePlot do
2323
end
2424
end
2525

26+
describe "show_plot" do
27+
it "allows color output to be controlled explicitly" do
28+
plot = UnicodePlot.lineplot([1.0, 2.0], [1.0, 2.0], color: :red)
29+
30+
default_io = IO::Memory.new
31+
UnicodePlot.show_plot(default_io, plot)
32+
33+
colored_io = IO::Memory.new
34+
UnicodePlot.show_plot(colored_io, plot, use_color: true)
35+
36+
plain_io = IO::Memory.new
37+
UnicodePlot.show_plot(plain_io, plot, use_color: false)
38+
39+
default_output = default_io.to_s
40+
colored_output = colored_io.to_s
41+
plain_output = plain_io.to_s
42+
43+
default_output.should_not contain("\e[")
44+
colored_output.should contain("\e[")
45+
plain_output.should_not contain("\e[")
46+
strip_ansi(colored_output).should eq(plain_output)
47+
end
48+
end
49+
2650
describe "lineplot" do
2751
it "returns a Plot" do
2852
p = UnicodePlot.lineplot([1.0, 2.0, 3.0], [1.0, 4.0, 9.0])

‎src/unicode_plot/show.cr‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ module UnicodePlot
33

44
def show_plot(io : IO, p : Plot) : Nil
55
use_color = io.is_a?(IO::FileDescriptor) && io.as(IO::FileDescriptor).tty?
6-
_show_plot(io, p, use_color)
6+
show_plot(io, p, use_color: use_color)
7+
end
8+
9+
def show_plot(io : IO, p : Plot, *, use_color : Bool) : Nil
10+
render_plot(io, p, use_color)
711
end
812

913
# Print the zmax/zmin value label adjacent to the colorbar border.
@@ -96,7 +100,7 @@ module UnicodePlot
96100
# ameba:enable Metrics/CyclomaticComplexity
97101

98102
# ameba:disable Metrics/CyclomaticComplexity
99-
private def _show_plot(io : IO, p : Plot, use_color : Bool) : Nil
103+
private def render_plot(io : IO, p : Plot, use_color : Bool) : Nil
100104
g = p.graphics
101105
blank_in = g.blank
102106

0 commit comments

Comments
 (0)