more_math 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 01200b0474b98510b8405a12492684e8d871d3a406a697c2d7775a26efe8c2ae
4
- data.tar.gz: dfa50772aedbc2f2530e35e7c1825a128cdaa5a9a76781d12484c9b8a2a995f9
3
+ metadata.gz: 72c52f7fecd9e87621f806bf86d7063f7aae2aef24e539d1d20182d5513bbd8f
4
+ data.tar.gz: 84a63a576ca178c7cefc591ba8373260259dfed4a83ec1c536817afce466c2b7
5
5
  SHA512:
6
- metadata.gz: c1dc76b84fed3a181158201798ce5fe6bff1ac22b2bff104b2f22ff5186104217c65ad1425cd676959f2358fc1b107722c3bc9bfcbd2ca6060fc033d927b47e6
7
- data.tar.gz: 90ad74b6b12f7735b088dc6bd06ccfbbe02af132718240a81eb8080c550a04c856698394c821441ddac4f7188524d02a006bf3579645a5b25a483a8e0dac568e
6
+ metadata.gz: 56ba92f6abcdc5b305f8069ce562418d799b9eeeab777e7b47b1c9b69a9cb1a61c69403750da8ecc59bb1d69a11b8ddca3fa40b18031e63d401550b2690acf67
7
+ data.tar.gz: 0cf1a0cab01791ff617910f4840ba58f1b14257b3bc611ece8085880ee1164abf20d2c8f3394108052c51a4ef00868d6d0b613146565dcd71ebbec2b08edca07
data/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changes
2
2
 
3
+ ## 2024-09-30 v1.3.0
4
+
5
+ * **Added support for displaying histograms based on percentage of terminal width**
6
+ + Added `terminal_width` method to `Histogram` class
7
+ + Updated `display` method to take interpret the `width` parameter as a percentage string, e.g. `75%`
8
+ + Updated test cases to use the new `display` method with different widths
9
+ + Added a new test case for displaying histograms with counts and `75%` width
10
+
3
11
  ## 2024-09-30 v1.2.2
4
12
 
5
13
  ### Improvements
@@ -1,3 +1,5 @@
1
+ require 'tins'
2
+
1
3
  module MoreMath
2
4
  # A histogram gives an overview of a sequence's elements.
3
5
  class Histogram
@@ -35,8 +37,12 @@ module MoreMath
35
37
 
36
38
  # Display this histogram to +output+, +width+ is the parameter for
37
39
  # +prepare_display+
38
- def display(output = $stdout, width = 50)
39
- width >= 1 or raise ArgumentError, "width needs to be >= 1"
40
+ def display(output = $stdout, width = 65)
41
+ if width.is_a?(String) && width =~ /(.+)%\z/
42
+ percentage = Float($1).clamp(0, 100)
43
+ width = (terminal_width * (percentage / 100.0)).floor
44
+ end
45
+ width > 15 or raise ArgumentError, "width needs to be >= 15"
40
46
  for r in rows
41
47
  output << output_row(r, width)
42
48
  end
@@ -44,6 +50,10 @@ module MoreMath
44
50
  self
45
51
  end
46
52
 
53
+ def terminal_width
54
+ Tins::Terminal.columns
55
+ end
56
+
47
57
  def max_count
48
58
  counts.max
49
59
  end
@@ -79,6 +89,7 @@ module MoreMath
79
89
  end
80
90
 
81
91
  def output_row_with_count(left, right, count, width)
92
+ width -= 15
82
93
  c = utf8? ? 2 : 1
83
94
  left_width = width - (counts.map { |x| x.to_s.size }.max + c)
84
95
  if left_width < 0
@@ -87,12 +98,13 @@ module MoreMath
87
98
  factor = left_width.to_f / max_count
88
99
  bar_width = (count * factor)
89
100
  bar = utf8? ? utf8_bar(bar_width) : ascii_bar(bar_width)
90
- max_count_length = max_count.to_s.size
101
+ max_count_length = max_count.to_s.size
91
102
  "%11.5f -|%#{-width + max_count_length}s%#{max_count_length}s\n" %
92
- [ (left + right) / 2.0, bar, count ]
103
+ [ (left + right) / 2.0, bar, count ]
93
104
  end
94
105
 
95
106
  def output_row_without_count(left, right, count, width)
107
+ width -= 15
96
108
  left_width = width
97
109
  left_width < 0 and left_width = width
98
110
  factor = left_width.to_f / max_count
@@ -1,6 +1,6 @@
1
1
  module MoreMath
2
2
  # MoreMath version
3
- VERSION = '1.2.2'
3
+ VERSION = '1.3.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
data/more_math.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: more_math 1.2.2 ruby lib
2
+ # stub: more_math 1.3.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "more_math".freeze
6
- s.version = "1.2.2".freeze
6
+ s.version = "1.3.0".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
@@ -23,7 +23,7 @@ class HistogramTest < Test::Unit::TestCase
23
23
  assert_equal [[2.0, 3.0, 1], [1.0, 2.0, 2], [0.0, 1.0, 2]],
24
24
  histogram.instance_eval { rows }
25
25
  output = StringIO.new
26
- histogram.display output
26
+ histogram.display output, 65
27
27
  assert_equal <<~end, output.string
28
28
  2.50000 -|*************************
29
29
  1.50000 -|**************************************************
@@ -41,7 +41,7 @@ class HistogramTest < Test::Unit::TestCase
41
41
  assert_equal [[2.0, 3.0, 1], [1.0, 2.0, 2], [0.0, 1.0, 2]],
42
42
  histogram.instance_eval { rows }
43
43
  output = StringIO.new
44
- histogram.display output
44
+ histogram.display output, 65
45
45
  assert_equal <<~end, output.string
46
46
  2.50000 -|************************ 1
47
47
  1.50000 -|************************************************ 2
@@ -58,7 +58,7 @@ class HistogramTest < Test::Unit::TestCase
58
58
  false
59
59
  end
60
60
  output = StringIO.new
61
- histogram.display output
61
+ histogram.display output, 65
62
62
  assert_equal <<~end, output.string
63
63
  67.50000 -|***** 81
64
64
  40.50000 -|************* 206
@@ -75,7 +75,7 @@ class HistogramTest < Test::Unit::TestCase
75
75
  true
76
76
  end
77
77
  output = StringIO.new
78
- histogram.display output
78
+ histogram.display output, 65
79
79
  assert_equal <<~end, output.string
80
80
  7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
81
81
  4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
@@ -92,7 +92,7 @@ class HistogramTest < Test::Unit::TestCase
92
92
  true
93
93
  end
94
94
  output = StringIO.new
95
- histogram.display output
95
+ histogram.display output, 65
96
96
  assert_equal <<~end, output.string
97
97
  7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇ 295
98
98
  4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇ 285
@@ -101,7 +101,7 @@ class HistogramTest < Test::Unit::TestCase
101
101
  end
102
102
  end
103
103
 
104
- def test_histogram_display_with_counts_utf8_zero
104
+ def test_histogram_display_with_counts_utf8_product
105
105
  srand 1337
106
106
  sequence = Sequence.new 1000.times.map { rand(10) * rand(10) }
107
107
  histogram = Histogram.new sequence, with_counts: true, bins: 3
@@ -109,7 +109,7 @@ class HistogramTest < Test::Unit::TestCase
109
109
  true
110
110
  end
111
111
  output = StringIO.new
112
- histogram.display output
112
+ histogram.display output, 65
113
113
  assert_equal <<~end, output.string
114
114
  67.50000 -|⣿⣿⣿⣿⣿ 81
115
115
  40.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 206
@@ -118,11 +118,31 @@ class HistogramTest < Test::Unit::TestCase
118
118
  end
119
119
  end
120
120
 
121
+ def test_histogram_display_with_counts_utf8_product_width_75_percent
122
+ srand 1337
123
+ sequence = Sequence.new 1000.times.map { rand(10) * rand(10) }
124
+ histogram = Histogram.new sequence, with_counts: true, bins: 3
125
+ def histogram.utf8?
126
+ true
127
+ end
128
+ def histogram.terminal_width
129
+ 80
130
+ end
131
+ output = StringIO.new
132
+ histogram.display output, '90%'
133
+ assert_equal <<~end, output.string
134
+ 67.50000 -|⣿⣿⣿⣿⣿⡇ 81
135
+ 40.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 206
136
+ 13.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 713
137
+ max_count=713
138
+ end
139
+ end
140
+
121
141
  private
122
142
 
123
- def output_histogram(histogram)
143
+ def output_histogram(histogram, width = 65)
124
144
  $stdout.puts
125
- histogram.display $stdout
145
+ histogram.display $stdout, width
126
146
  $stdout.puts
127
147
  end
128
148
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: more_math
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank