Skip to content

Commit 6691885

Browse files
committed
Let render method support color: option
1 parent 225937c commit 6691885

7 files changed

Lines changed: 55 additions & 15 deletions

File tree

lib/unicode_plot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'unicode_plot/version'
44

5+
require 'unicode_plot/io_context'
56
require 'unicode_plot/utils'
67
require 'unicode_plot/styled_printer'
78
require 'unicode_plot/value_transformer'

lib/unicode_plot/io_context.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
require "forwardable"
2+
3+
module UnicodePlot
4+
class IOContext
5+
extend Forwardable
6+
7+
def initialize(io, color: :auto)
8+
@io = io
9+
@color = check_color(color)
10+
end
11+
12+
def_delegators :@io, :print, :puts
13+
14+
def color?
15+
case @color
16+
when :auto
17+
@io.respond_to?(:tty?) ? @io.tty? : false
18+
else
19+
@color
20+
end
21+
end
22+
23+
private def check_color(color)
24+
case color
25+
when true, false, :auto
26+
color
27+
else
28+
raise ArgumentError, "color must be either true, false, :auto"
29+
end
30+
end
31+
end
32+
end

lib/unicode_plot/plot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ def annotate_row!(loc, row_index, value, color: :normal)
103103
end
104104
end
105105

106-
def render(out=$stdout, newline: true)
107-
Renderer.render(out, self, newline)
106+
def render(out=$stdout, newline: true, color: :auto)
107+
Renderer.render(IOContext.new(out, color: color), self, newline)
108108
end
109109

110110
COLOR_CYCLE = [

lib/unicode_plot/renderer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def render_row(row)
128128
left_len = nocolor_string(left_str).length
129129
right_len = nocolor_string(right_str).length
130130

131-
unless color?(out)
131+
unless out.color?
132132
left_str = nocolor_string(left_str)
133133
right_str = nocolor_string(right_str)
134134
end

lib/unicode_plot/styled_printer.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module StyledPrinter
5858
COLOR_DECODE = COLOR_ENCODE.map {|k, v| [v, k] }.to_h.freeze
5959

6060
def print_styled(out, *args, bold: false, color: :normal)
61-
return out.print(*args) unless color?(out)
61+
return out.print(*args) unless out.color?
6262

6363
str = StringIO.open {|sio| sio.print(*args); sio.close; sio.string }
6464
color = :nothing if bold && color == :bold
@@ -83,9 +83,5 @@ def print_color(out, color, *args)
8383
color = COLOR_DECODE[color]
8484
print_styled(out, *args, color: color)
8585
end
86-
87-
def color?(out)
88-
out&.tty? || false
89-
end
9086
end
9187
end

test/test-canvas.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ def setup
5555

5656
test("empty") do
5757
if self.class::CANVAS_NAME == :braille
58-
_, output = with_term { @canvas.show($stdout) }
58+
_, output = with_term { @canvas.show(UnicodePlot::IOContext.new($stdout)) }
5959
assert_equal(fixture_path("canvas/empty_braille_show.txt").read,
6060
output)
6161
else
62-
_, output = with_term { @canvas.show($stdout) }
62+
_, output = with_term { @canvas.show(UnicodePlot::IOContext.new($stdout)) }
6363
assert_equal(fixture_path("canvas/empty_show.txt").read,
6464
output)
6565
end
@@ -81,31 +81,31 @@ def setup
8181
end
8282

8383
test("print_row") do
84-
_, output = with_term { @canvas.print_row($stdout, 2) }
84+
_, output = with_term { @canvas.print_row(UnicodePlot::IOContext.new($stdout), 2) }
8585
assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_printrow.txt").read,
8686
output)
8787
end
8888

8989
test("print") do
90-
_, output = with_term { @canvas.print($stdout) }
90+
_, output = with_term { @canvas.print(UnicodePlot::IOContext.new($stdout)) }
9191
assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_print.txt").read,
9292
output)
9393
end
9494

9595
test("print_nocolor") do
96-
_, output = with_term(false) { @canvas.print($stdout) }
96+
_, output = with_term(false) { @canvas.print(UnicodePlot::IOContext.new($stdout)) }
9797
assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_print_nocolor.txt").read,
9898
output)
9999
end
100100

101101
test("sow") do
102-
_, output = with_term { @canvas.show($stdout) }
102+
_, output = with_term { @canvas.show(UnicodePlot::IOContext.new($stdout)) }
103103
assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_show.txt").read,
104104
output)
105105
end
106106

107107
test("show_nocolor") do
108-
_, output = with_term(false) { @canvas.show($stdout) }
108+
_, output = with_term(false) { @canvas.show(UnicodePlot::IOContext.new($stdout)) }
109109
assert_equal(fixture_path("canvas/#{self.class::CANVAS_NAME}_show_nocolor.txt").read,
110110
output)
111111
end

test/test-plot.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,16 @@ class TestPlot < Test::Unit::TestCase
1616
$stdout = save_stdout
1717
end
1818
end
19+
20+
test("color: true") do
21+
sio_notty = StringIO.new
22+
UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio_notty, color: true)
23+
24+
class << (sio_tty = StringIO.new)
25+
def tty?; true; end
26+
end
27+
UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio_tty)
28+
assert_equal(sio_tty.string, sio_notty.string)
29+
end
1930
end
2031
end

0 commit comments

Comments
 (0)