more_math 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2945331f84849efc01cafcdd9dec0e1402a6296c979d6b6d5ecb41ff49427a2
4
- data.tar.gz: 2e25d9b28a5c326eb50d7ca12ef66d0562f8e5839a8f9c37a9db504fc13c286c
3
+ metadata.gz: 72c52f7fecd9e87621f806bf86d7063f7aae2aef24e539d1d20182d5513bbd8f
4
+ data.tar.gz: 84a63a576ca178c7cefc591ba8373260259dfed4a83ec1c536817afce466c2b7
5
5
  SHA512:
6
- metadata.gz: 11a42069a9e921c857849cceb0f912a3989a4bf046645be765c8acb54f9bea299ced8790be585fd04367531e6484847866cde89486c7aa6943fb396d80bfcf31
7
- data.tar.gz: ee3e2b0ba782557eb94770833b0e98494254ce1a138caf493e72d208b54f56bb62645bd0779e71c84b32b949df2b8de3f4424c2c107b3c609a7bd5901a2cd471
6
+ metadata.gz: 56ba92f6abcdc5b305f8069ce562418d799b9eeeab777e7b47b1c9b69a9cb1a61c69403750da8ecc59bb1d69a11b8ddca3fa40b18031e63d401550b2690acf67
7
+ data.tar.gz: 0cf1a0cab01791ff617910f4840ba58f1b14257b3bc611ece8085880ee1164abf20d2c8f3394108052c51a4ef00868d6d0b613146565dcd71ebbec2b08edca07
data/CHANGES.md CHANGED
@@ -1,5 +1,22 @@
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
+
11
+ ## 2024-09-30 v1.2.2
12
+
13
+ ### Improvements
14
+ * Refactor Histogram display logic for better UTF-8 support:
15
+ + Extracted `output_row_with_count` and `output_row_without_count` methods
16
+ + Updated test cases for histogram display with counts and UTF-8 support
17
+ * Update Rakefile to ignore `.utilsrc` file:
18
+ - Add `.utilsrc` to `package_ignore` list in Rakefile
19
+
3
20
  ## 2024-09-30 v1.2.1
4
21
 
5
22
  * Refactor histogram display logic for utf8 and ascii bars
data/Rakefile CHANGED
@@ -14,7 +14,7 @@ GemHadar do
14
14
  '.AppleDouble', 'tags', '.byebug_history', '.DS_Store'
15
15
  readme 'README.md'
16
16
  title "#{name.camelize} -- More Math in Ruby"
17
- package_ignore '.all_images.yml', '.gitignore', 'VERSION',
17
+ package_ignore '.all_images.yml', '.gitignore', 'VERSION', '.utilsrc',
18
18
  *Dir.glob('.github/**/*', File::FNM_DOTMATCH)
19
19
 
20
20
  required_ruby_version '>= 2.0'
@@ -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
@@ -62,7 +72,7 @@ module MoreMath
62
72
  end
63
73
 
64
74
  def ascii_bar(bar_width)
65
- bar = ?* * bar_width
75
+ ?* * bar_width
66
76
  end
67
77
 
68
78
  def utf8?
@@ -72,29 +82,35 @@ module MoreMath
72
82
  def output_row(row, width)
73
83
  left, right, count = row
74
84
  if @with_counts
75
- c = utf8? ? 2 : 1
76
- left_width = width - (counts.map { |x| x.to_s.size }.max + c)
85
+ output_row_with_count(left, right, count, width)
77
86
  else
78
- left_width = width
87
+ output_row_without_count(left, right, count, width)
79
88
  end
89
+ end
90
+
91
+ def output_row_with_count(left, right, count, width)
92
+ width -= 15
93
+ c = utf8? ? 2 : 1
94
+ left_width = width - (counts.map { |x| x.to_s.size }.max + c)
80
95
  if left_width < 0
81
96
  left_width = width
82
97
  end
83
98
  factor = left_width.to_f / max_count
84
99
  bar_width = (count * factor)
85
100
  bar = utf8? ? utf8_bar(bar_width) : ascii_bar(bar_width)
86
- if @with_counts
87
- if utf8?
88
- if bar[-1] == ' '
89
- bar += count.to_s.rjust(width - bar_width - 1)
90
- else
91
- bar += count.to_s.rjust(width - bar_width)
92
- end
93
- else
94
- bar += count.to_s.rjust(width - bar_width)
95
- end
96
- end
97
- "%11.5f -|%s\n" % [ (left + right) / 2.0, bar ]
101
+ max_count_length = max_count.to_s.size
102
+ "%11.5f -|%#{-width + max_count_length}s%#{max_count_length}s\n" %
103
+ [ (left + right) / 2.0, bar, count ]
104
+ end
105
+
106
+ def output_row_without_count(left, right, count, width)
107
+ width -= 15
108
+ left_width = width
109
+ left_width < 0 and left_width = width
110
+ factor = left_width.to_f / max_count
111
+ bar_width = (count * factor)
112
+ bar = utf8? ? utf8_bar(bar_width) : ascii_bar(bar_width)
113
+ "%11.5f -|%#{-width}s\n" % [ (left + right) / 2.0, bar ]
98
114
  end
99
115
 
100
116
  def rows
@@ -1,6 +1,6 @@
1
1
  module MoreMath
2
2
  # MoreMath version
3
- VERSION = '1.2.1'
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.1 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.1".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,9 +23,9 @@ 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
- 2.50000 -|*************************
28
+ 2.50000 -|*************************
29
29
  1.50000 -|**************************************************
30
30
  0.50000 -|**************************************************
31
31
  max_count=2
@@ -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
@@ -50,6 +50,23 @@ class HistogramTest < Test::Unit::TestCase
50
50
  end
51
51
  end
52
52
 
53
+ def test_histogram_display_with_counts_lots
54
+ srand 1337
55
+ sequence = Sequence.new 1000.times.map { rand(10) * rand(10) }
56
+ histogram = Histogram.new sequence, with_counts: true, bins: 3
57
+ def histogram.utf8?
58
+ false
59
+ end
60
+ output = StringIO.new
61
+ histogram.display output, 65
62
+ assert_equal <<~end, output.string
63
+ 67.50000 -|***** 81
64
+ 40.50000 -|************* 206
65
+ 13.50000 -|********************************************** 713
66
+ max_count=713
67
+ end
68
+ end
69
+
53
70
  def test_histogram_display_utf8
54
71
  srand 1337
55
72
  sequence = Sequence.new 1000.times.map { rand(10) }
@@ -58,10 +75,10 @@ class HistogramTest < Test::Unit::TestCase
58
75
  true
59
76
  end
60
77
  output = StringIO.new
61
- histogram.display output
78
+ histogram.display output, 65
62
79
  assert_equal <<~end, output.string
63
- 7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
64
- 4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
80
+ 7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
81
+ 4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇
65
82
  1.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿
66
83
  max_count=420
67
84
  end
@@ -75,7 +92,7 @@ class HistogramTest < Test::Unit::TestCase
75
92
  true
76
93
  end
77
94
  output = StringIO.new
78
- histogram.display output
95
+ histogram.display output, 65
79
96
  assert_equal <<~end, output.string
80
97
  7.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇ 295
81
98
  4.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇ 285
@@ -83,4 +100,49 @@ class HistogramTest < Test::Unit::TestCase
83
100
  max_count=420
84
101
  end
85
102
  end
103
+
104
+ def test_histogram_display_with_counts_utf8_product
105
+ srand 1337
106
+ sequence = Sequence.new 1000.times.map { rand(10) * rand(10) }
107
+ histogram = Histogram.new sequence, with_counts: true, bins: 3
108
+ def histogram.utf8?
109
+ true
110
+ end
111
+ output = StringIO.new
112
+ histogram.display output, 65
113
+ assert_equal <<~end, output.string
114
+ 67.50000 -|⣿⣿⣿⣿⣿ 81
115
+ 40.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 206
116
+ 13.50000 -|⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ 713
117
+ max_count=713
118
+ end
119
+ end
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
+
141
+ private
142
+
143
+ def output_histogram(histogram, width = 65)
144
+ $stdout.puts
145
+ histogram.display $stdout, width
146
+ $stdout.puts
147
+ end
86
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.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank