diff --git a/lib/youplot/backends/unicode_plot.rb b/lib/youplot/backends/unicode_plot.rb index 971deba..28d8d2e 100644 --- a/lib/youplot/backends/unicode_plot.rb +++ b/lib/youplot/backends/unicode_plot.rb @@ -67,6 +67,10 @@ def barplot(data, params, fmt = nil, count: false, reverse: false) series[y_col].map(&:to_f) end end + # If all values are integers, display without decimal points (#44) + if values.all? { |v| v.finite? && v == v.to_i } + values = values.map(&:to_i) + end ::UnicodePlot.barplot(labels, values, **params.to_hc) end diff --git a/test/fixtures/integer-barplot.txt b/test/fixtures/integer-barplot.txt new file mode 100644 index 0000000..23ce4ff --- /dev/null +++ b/test/fixtures/integer-barplot.txt @@ -0,0 +1,5 @@ + ┌ ┐ + a ┤■■■■■■■■■ 3 + b ┤■■■■■■■■■■■■■■■■■■■■■ 7 + c ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 12 + └ ┘ diff --git a/test/fixtures/integer.tsv b/test/fixtures/integer.tsv new file mode 100644 index 0000000..12c7a37 --- /dev/null +++ b/test/fixtures/integer.tsv @@ -0,0 +1,3 @@ +a 3 +b 7 +c 12 diff --git a/test/youplot/integer_test.rb b/test/youplot/integer_test.rb new file mode 100644 index 0000000..ef02d2b --- /dev/null +++ b/test/youplot/integer_test.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require 'tempfile' +require_relative '../test_helper' + +class YouPlotIntegerTest < Test::Unit::TestCase + class << self + def startup + @stdin = $stdin.dup + @stdout = $stdout.dup + @stderr = $stderr.dup + end + + def shutdown + $stdin = @stdin + $stdout = @stdout + $stderr = @stderr + end + end + + def setup + @stderr_file = Tempfile.new + @stdout_file = Tempfile.new + $stderr = @stderr_file + $stdout = @stdout_file + end + + def teardown + @stderr_file.close + @stdout_file.close + end + + def fixture(fname) + File.read(File.expand_path("../fixtures/#{fname}", __dir__)) + end + + # Issue #44: When all values are integers, display without decimal points + test :barplot_integer_values do + $stdin = File.open(File.expand_path('../fixtures/integer.tsv', __dir__), 'r') + YouPlot::Command.new(['bar']).run + assert_equal fixture('integer-barplot.txt'), @stderr_file.read + end +end