Skip to content

Commit

Permalink
Fix color command
Browse files Browse the repository at this point in the history
  • Loading branch information
kojix2 committed Dec 17, 2020
1 parent cc076d5 commit edd0815
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
20 changes: 13 additions & 7 deletions lib/youplot/backends/unicode_plot_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,23 @@ def boxplot(data, params)
end

def colors(color_names = false)
# FIXME
s = String.new
UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
print v
print k
s << v
s << k.to_s
unless color_names
print "\t"
print ' ●'
s << "\t"
s << ' ●'
end
print "\033[0m"
print "\t"
s << "\033[0m"
s << "\t"
end
puts
s << "\n"
def s.render(obj)
obj.print(self)
end
s
end

def check_series_size(data, fmt)
Expand Down
18 changes: 10 additions & 8 deletions lib/youplot/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def run
@options ||= parser.options
@params ||= parser.params

if command == :colors
@backend.colors(parser.color_names)
exit
end

# Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil))
main(input)
if %i[colors color colours colour].include? @command
plot = create_plot
output_plot(plot)
else
# Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil))
main(input)
end
end
end

Expand Down Expand Up @@ -75,6 +75,8 @@ def create_plot
@backend.density(data, params, options[:fmt])
when :box, :boxplot
@backend.boxplot(data, params)
when :colors, :color, :colours, :colour
@backend.colors(options[:color_names])
else
raise "unrecognized plot_type: #{command}"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/youplot/command/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def main_parser
scatter s draw a scatter plot
density d draw a density plot
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
occurrences (slow)
Expand Down Expand Up @@ -252,7 +252,7 @@ def sub_parser
params.xlim = v.take(2)
end

when :colors
when :colors, :color, :colours, :colour
parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
@options[:color_names] = v
end
Expand Down
16 changes: 15 additions & 1 deletion test/youplot/command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,26 @@ def fixture(fname)

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

test :data_output_stdout do
YouPlot::Command.new(['bar', '-O', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
assert_equal File.read(File.expand_path('../fixtures/iris.csv', __dir__)), @stdout_file.read
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

0 comments on commit edd0815

Please sign in to comment.