Skip to content

Commit edd0815

Browse files
committed
Fix color command
1 parent cc076d5 commit edd0815

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

lib/youplot/backends/unicode_plot_backend.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,23 @@ def boxplot(data, params)
152152
end
153153

154154
def colors(color_names = false)
155+
# FIXME
156+
s = String.new
155157
UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
156-
print v
157-
print k
158+
s << v
159+
s << k.to_s
158160
unless color_names
159-
print "\t"
160-
print ' ●'
161+
s << "\t"
162+
s << ' ●'
161163
end
162-
print "\033[0m"
163-
print "\t"
164+
s << "\033[0m"
165+
s << "\t"
164166
end
165-
puts
167+
s << "\n"
168+
def s.render(obj)
169+
obj.print(self)
170+
end
171+
s
166172
end
167173

168174
def check_series_size(data, fmt)

lib/youplot/command.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ def run
2828
@options ||= parser.options
2929
@params ||= parser.params
3030

31-
if command == :colors
32-
@backend.colors(parser.color_names)
33-
exit
34-
end
35-
36-
# Sometimes the input file does not end with a newline code.
37-
while (input = Kernel.gets(nil))
38-
main(input)
31+
if %i[colors color colours colour].include? @command
32+
plot = create_plot
33+
output_plot(plot)
34+
else
35+
# Sometimes the input file does not end with a newline code.
36+
while (input = Kernel.gets(nil))
37+
main(input)
38+
end
3939
end
4040
end
4141

@@ -75,6 +75,8 @@ def create_plot
7575
@backend.density(data, params, options[:fmt])
7676
when :box, :boxplot
7777
@backend.boxplot(data, params)
78+
when :colors, :color, :colours, :colour
79+
@backend.colors(options[:color_names])
7880
else
7981
raise "unrecognized plot_type: #{command}"
8082
end

lib/youplot/command/parser.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def main_parser
123123
scatter s draw a scatter plot
124124
density d draw a density plot
125125
boxplot box draw a horizontal boxplot
126-
colors show the list of available colors
126+
colors color show the list of available colors
127127
128128
count c draw a baplot based on the number of
129129
occurrences (slow)
@@ -252,7 +252,7 @@ def sub_parser
252252
params.xlim = v.take(2)
253253
end
254254

255-
when :colors
255+
when :colors, :color, :colours, :colour
256256
parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
257257
@options[:color_names] = v
258258
end

test/youplot/command_test.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,26 @@ def fixture(fname)
106106

107107
test :plot_output_stdout do
108108
YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
109-
assert_equal "", @stderr_file.read
109+
assert_equal '', @stderr_file.read
110110
end
111111

112112
test :data_output_stdout do
113113
YouPlot::Command.new(['bar', '-O', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
114114
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
115115
assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read
116116
end
117+
118+
%i[colors color colours colour].each do |cmd_name|
119+
test cmd_name do
120+
YouPlot::Command.new([cmd_name.to_s]).run
121+
assert_equal fixture('colors.txt'), @stderr_file.read
122+
assert_equal '', @stdout_file.read
123+
end
124+
end
125+
126+
test :colors_output_stdout do
127+
YouPlot::Command.new(['colors', '-o']).run
128+
assert_equal '', @stderr_file.read
129+
assert_equal fixture('colors.txt'), @stdout_file.read
130+
end
117131
end

0 commit comments

Comments
 (0)