natty-ui 0.26.0 → 0.28.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.
@@ -24,12 +24,22 @@ module NattyUI
24
24
 
25
25
  # @!visibility private
26
26
  def align(value)
27
- value == :right || value == :centered ? value : :left
27
+ POS_ALI.include?(value) ? value : :left
28
+ end
29
+
30
+ # @!visibility private
31
+ def position(value)
32
+ value if POS_ALI.include?(value)
28
33
  end
29
34
 
30
35
  # @!visibility private
31
36
  def vertical(value)
32
- value == :bottom || value == :middle ? value : :top
37
+ VERT.include?(value) ? value : :top
38
+ end
39
+
40
+ # @!visibility private
41
+ def split_table_attr(values)
42
+ [values.slice(*TAB_ATTR), values.except(*TAB_ATTR)]
33
43
  end
34
44
 
35
45
  # @!visibility private
@@ -39,7 +49,7 @@ module NattyUI
39
49
  when 0
40
50
  [0, 0, 0, 0]
41
51
  when 1
42
- [value[0], 0, 0, 0]
52
+ Array.new(4, value[0])
43
53
  when 2
44
54
  value * 2
45
55
  when 3
@@ -49,29 +59,58 @@ module NattyUI
49
59
  end
50
60
  end
51
61
  alias margin padding
62
+
63
+ # @!visibility private
64
+ def as_size(range, value)
65
+ return range.begin if value == :min
66
+ return range.end if value.nil? || value.is_a?(Symbol)
67
+ (
68
+ if value.is_a?(Numeric)
69
+ (value > 0 && value < 1 ? (range.end * value) : value).round
70
+ else
71
+ value.to_i
72
+ end
73
+ ).clamp(range)
74
+ end
52
75
  end
76
+
77
+ POS_ALI = %i[right centered].freeze
78
+ VERT = %i[bottom middle].freeze
79
+ TAB_ATTR = %i[border_around border border_style position].freeze
53
80
  end
54
81
 
55
82
  class Str
56
83
  attr_reader :to_s
57
-
58
84
  alias to_str to_s
59
- def inspect = to_s.inspect
60
- def empty? = size == 0
85
+ def empty? = width == 0
86
+ def inspect = @to_s.inspect
61
87
 
62
- def size
63
- return @size if @size
64
- @size = Text.width(self)
88
+ def width
89
+ return @width if @width
90
+ @width = Text.width(self)
65
91
  freeze
66
- @size
92
+ @width
67
93
  end
68
- alias width size
69
94
 
70
- def initialize(str, size = nil)
71
- @to_s = Ansi.bbcode(str).freeze
72
- return unless size
73
- @size = @size.is_a?(Integer) ? size : Text.width(self)
74
- freeze
95
+ def +(other)
96
+ other = Str.new(other) unless other.is_a?(Str)
97
+ Str.new(@to_s + other.to_s, width + other.width)
98
+ end
99
+
100
+ if Terminal.ansi?
101
+ def initialize(str, width = nil)
102
+ @to_s = Ansi.bbcode(str).freeze
103
+ return unless width
104
+ @width = @width.is_a?(Integer) ? width : Text.width(self)
105
+ freeze
106
+ end
107
+ else
108
+ def initialize(str, width = nil)
109
+ @to_s = Ansi.plain(str).freeze
110
+ return unless width
111
+ @width = @width.is_a?(Integer) ? width : Text.width(self)
112
+ freeze
113
+ end
75
114
  end
76
115
  end
77
116
 
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NattyUI
4
+ module VBarsRenderer
5
+ class << self
6
+ def lines(vals, width, height, normalize, bar_width, style)
7
+ if style
8
+ bos = Ansi[*style]
9
+ eos = Ansi::RESET
10
+ end
11
+ if ((bar_width != :auto) && bar_width <= 1) || (vals.size * 2 > width)
12
+ db = '▉'
13
+ ds = ' '
14
+ el = "#{bos}#{'▔' * vals.size}#{eos}"
15
+ vals = vals.take(width) if vals.size > width
16
+ else
17
+ bw = (width - vals.size) / vals.size
18
+ bw = [bw, bar_width].min if bar_width != :auto
19
+ db = "#{'█' * bw} "
20
+ bw += 1
21
+ ds = ' ' * bw
22
+ el = "#{bos}#{'▔' * ((bw * vals.size) - 1)}#{eos}"
23
+ end
24
+ height -= 1
25
+ vals = normalize ? normalize!(vals, height) : adjust!(vals, height)
26
+ height
27
+ .downto(1)
28
+ .map { |i| "#{bos}#{vals.map { _1 < i ? ds : db }.join}#{eos}" } << el
29
+ end
30
+
31
+ private
32
+
33
+ def adjust!(vals, height)
34
+ max = vals.max.to_f
35
+ vals.map { ((_1 / max) * height).round }
36
+ end
37
+
38
+ def normalize!(vals, height)
39
+ min, max = vals.minmax
40
+ return Array.new(vals.size, height) if min == max
41
+ max = (max - min).to_f
42
+ vals.map { (((_1 - min) / max) * height).round }
43
+ end
44
+ end
45
+ end
46
+
47
+ private_constant :VBarsRenderer
48
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NattyUI
4
4
  # The version number of the gem.
5
- VERSION = '0.26.0'
5
+ VERSION = '0.28.0'
6
6
  end
@@ -43,9 +43,9 @@ module NattyUI
43
43
 
44
44
  def fit_max_width(columns, max_width, saving_by_column)
45
45
  ret = max_width - columns.size + 1
46
+ columns = columns.map { normalized(_1, max_width) }
46
47
  return columns, ret if ret >= 1
47
48
  saving_by_column += 1
48
- columns = columns.dup
49
49
  until ret >= 1
50
50
  columns.pop
51
51
  ret += saving_by_column
@@ -53,6 +53,16 @@ module NattyUI
53
53
  [columns, ret]
54
54
  end
55
55
 
56
+ def normalized(value, max_width)
57
+ return (max_width * value).to_i if value.is_a?(Float)
58
+ return value unless value.is_a?(Range)
59
+ min = value.begin
60
+ max = value.end
61
+ min = (max_width * min).to_i if min.is_a?(Float)
62
+ max = (max_width * max).to_i if max.is_a?(Float)
63
+ (min..max)
64
+ end
65
+
56
66
  def expand(sum)
57
67
  max_width = [@columns.sum(&:max_val), @max_width].min
58
68
  expandables = @columns.find_all(&:expandable?)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natty-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.0
4
+ version: 0.28.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 0.11.0
18
+ version: '0.12'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
- version: 0.11.0
25
+ version: '0.12'
26
26
  description: |
27
27
  This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely,
28
28
  natty user interface tool you like to have for your command line applications.
@@ -44,13 +44,16 @@ files:
44
44
  - examples/cols.rb
45
45
  - examples/elements.rb
46
46
  - examples/examples.rb
47
+ - examples/hbars.rb
47
48
  - examples/illustration.rb
49
+ - examples/info.rb
48
50
  - examples/key-codes.rb
49
51
  - examples/ls.rb
50
52
  - examples/named-colors.rb
51
53
  - examples/sections.rb
52
54
  - examples/tables.rb
53
55
  - examples/tasks.rb
56
+ - examples/vbars.rb
54
57
  - lib/natty-ui.rb
55
58
  - lib/natty-ui/attributes.rb
56
59
  - lib/natty-ui/choice.rb
@@ -58,6 +61,7 @@ files:
58
61
  - lib/natty-ui/element.rb
59
62
  - lib/natty-ui/features.rb
60
63
  - lib/natty-ui/framed.rb
64
+ - lib/natty-ui/hbars_renderer.rb
61
65
  - lib/natty-ui/ls_renderer.rb
62
66
  - lib/natty-ui/progress.rb
63
67
  - lib/natty-ui/section.rb
@@ -67,6 +71,7 @@ files:
67
71
  - lib/natty-ui/temporary.rb
68
72
  - lib/natty-ui/theme.rb
69
73
  - lib/natty-ui/utils.rb
74
+ - lib/natty-ui/vbars_renderer.rb
70
75
  - lib/natty-ui/version.rb
71
76
  - lib/natty-ui/width_finder.rb
72
77
  - lib/natty_ui.rb
@@ -75,9 +80,10 @@ licenses:
75
80
  - BSD-3-Clause
76
81
  metadata:
77
82
  rubygems_mfa_required: 'true'
83
+ yard.run: yard
78
84
  source_code_uri: https://github.com/mblumtritt/natty-ui
79
85
  bug_tracker_uri: https://github.com/mblumtritt/natty-ui/issues
80
- documentation_uri: https://rubydoc.info/gems/natty-ui/0.26.0/NattyUI
86
+ documentation_uri: https://rubydoc.info/gems/natty-ui/NattyUI
81
87
  rdoc_options: []
82
88
  require_paths:
83
89
  - lib