Fix color command

This commit is contained in:
kojix2 2020-12-17 12:37:27 +09:00
parent cc076d5cfe
commit edd081562e
4 changed files with 40 additions and 18 deletions

View File

@ -152,17 +152,23 @@ module YouPlot
end end
def colors(color_names = false) def colors(color_names = false)
# FIXME
s = String.new
UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v| UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
print v s << v
print k s << k.to_s
unless color_names unless color_names
print "\t" s << "\t"
print ' ●' s << ' ●'
end end
print "\033[0m" s << "\033[0m"
print "\t" s << "\t"
end end
puts s << "\n"
def s.render(obj)
obj.print(self)
end
s
end end
def check_series_size(data, fmt) def check_series_size(data, fmt)

View File

@ -28,16 +28,16 @@ module YouPlot
@options ||= parser.options @options ||= parser.options
@params ||= parser.params @params ||= parser.params
if command == :colors if %i[colors color colours colour].include? @command
@backend.colors(parser.color_names) plot = create_plot
exit output_plot(plot)
end else
# Sometimes the input file does not end with a newline code. # Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil)) while (input = Kernel.gets(nil))
main(input) main(input)
end end
end end
end
private private
@ -75,6 +75,8 @@ module YouPlot
@backend.density(data, params, options[:fmt]) @backend.density(data, params, options[:fmt])
when :box, :boxplot when :box, :boxplot
@backend.boxplot(data, params) @backend.boxplot(data, params)
when :colors, :color, :colours, :colour
@backend.colors(options[:color_names])
else else
raise "unrecognized plot_type: #{command}" raise "unrecognized plot_type: #{command}"
end end

View File

@ -123,7 +123,7 @@ module YouPlot
scatter s draw a scatter plot scatter s draw a scatter plot
density d draw a density plot density d draw a density plot
boxplot box draw a horizontal boxplot boxplot box draw a horizontal boxplot
colors show the list of available colors colors color show the list of available colors
count c draw a baplot based on the number of count c draw a baplot based on the number of
occurrences (slow) occurrences (slow)
@ -252,7 +252,7 @@ module YouPlot
params.xlim = v.take(2) params.xlim = v.take(2)
end end
when :colors when :colors, :color, :colours, :colour
parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v| parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
@options[:color_names] = v @options[:color_names] = v
end end

View File

@ -106,7 +106,7 @@ class YouPlotCommandTest < Test::Unit::TestCase
test :plot_output_stdout do test :plot_output_stdout do
YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
assert_equal "", @stderr_file.read assert_equal '', @stderr_file.read
end end
test :data_output_stdout do test :data_output_stdout do
@ -114,4 +114,18 @@ class YouPlotCommandTest < Test::Unit::TestCase
assert_equal fixture('iris-barplot.txt'), @stderr_file.read assert_equal fixture('iris-barplot.txt'), @stderr_file.read
assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read
end end
%i[colors color colours colour].each do |cmd_name|
test cmd_name do
YouPlot::Command.new([cmd_name.to_s]).run
assert_equal fixture('colors.txt'), @stderr_file.read
assert_equal '', @stdout_file.read
end
end
test :colors_output_stdout do
YouPlot::Command.new(['colors', '-o']).run
assert_equal '', @stderr_file.read
assert_equal fixture('colors.txt'), @stdout_file.read
end
end end