Skip to content

Commit 836ea74

Browse files
authored
Merge pull request #46 from red-data-tools/render_color_param
Let `render` method support `color:` option
2 parents 225937c + 4606a5c commit 836ea74

8 files changed

Lines changed: 76 additions & 25 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/helper/with_term.rb

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,28 @@
22

33
module Helper
44
module WithTerm
5-
def with_term(tty=true)
5+
def with_sio(tty: true)
66
sio = StringIO.new
77
def sio.tty?; true; end if tty
88

9-
orig_stdout, $stdout = $stdout, sio
10-
orig_env = ENV.to_h.dup
11-
ENV['TERM'] = 'xterm-256color'
12-
13-
result = yield
9+
result = yield(sio)
1410
sio.close
1511

1612
[result, sio.string]
17-
ensure
18-
$stdout.close
19-
$stdout = orig_stdout
20-
ENV.replace(orig_env) if orig_env
13+
end
14+
15+
def with_term(tty=true)
16+
with_sio(tty: tty) do |sio|
17+
begin
18+
orig_stdout, $stdout = $stdout, sio
19+
orig_env = ENV.to_h.dup
20+
ENV['TERM'] = 'xterm-256color'
21+
yield
22+
ensure
23+
$stdout = orig_stdout
24+
ENV.replace(orig_env) if orig_env
25+
end
26+
end
2127
end
2228
end
2329
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'stringio'
22

33
class TestPlot < Test::Unit::TestCase
4+
include Helper::WithTerm
5+
46
sub_test_case("#render") do
57
test("render to $stdout when no arguments") do
68
sio = StringIO.new
@@ -16,5 +18,19 @@ class TestPlot < Test::Unit::TestCase
1618
$stdout = save_stdout
1719
end
1820
end
21+
22+
test("color: true") do
23+
_, tty_output = with_sio(tty: true) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio) }
24+
_, notty_output = with_sio(tty: false) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio, color: true) }
25+
26+
assert_equal(tty_output, notty_output)
27+
end
28+
29+
test("color: false") do
30+
_, tty_output = with_sio(tty: true) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio, color: false) }
31+
_, notty_output = with_sio(tty: false) {|sio| UnicodePlot.barplot(data: {a: 23, b: 37}).render(sio) }
32+
33+
assert_equal(tty_output, notty_output)
34+
end
1935
end
2036
end

0 commit comments

Comments
 (0)