Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/youplot/backends/unicode_plot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +70 to +73
::UnicodePlot.barplot(labels, values, **params.to_hc)
end

Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/integer-barplot.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
┌ ┐
a ┤■■■■■■■■■ 3
b ┤■■■■■■■■■■■■■■■■■■■■■ 7
c ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 12
└ ┘
3 changes: 3 additions & 0 deletions test/fixtures/integer.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a 3
b 7
c 12
43 changes: 43 additions & 0 deletions test/youplot/integer_test.rb
Original file line number Diff line number Diff line change
@@ -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