natty-ui 1.0.2 → 1.0.3

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: ffd6017665733941fd093cbedda29b355bc207fd0ed3cecfdc041fcbc4b015f1
4
- data.tar.gz: 0c8bd80f79200f5a0736a3830724e3effba02e45e90a7700ff3282206e0892fa
3
+ metadata.gz: 1bec48a569ad9b951d916baaa9388d825f78fdb63552d932e767e9ce52a9506a
4
+ data.tar.gz: 1959dea526e29424bec91acd9df4200256f04b748bd4e2a6f5469e1cad28cb7b
5
5
  SHA512:
6
- metadata.gz: fc669642409c79aab2bf8147e6a1a490b83920e2549d8bf869c11d21e914c21b2f3e8e17135ce281d619716bbfb35536213826494a7e83ac37d996365c4ef6e4
7
- data.tar.gz: 28ebfab029b8e77b9179f5d9652031e997cadc57e8eaf600729ccda22e0eaeb5443839ea58b7e157d7e425c495adddd38bd725d6c5e58758c9dff61b86be2f82
6
+ metadata.gz: 904823039984b10ddf34348fb81c9931396d8fe7b6e4079531c177a18d7f891a02227d7e5b4e371878d7837d32fbe3bff8afce7c25ca047265406f3d2ec9cef3
7
+ data.tar.gz: a993fbec0c910161f00f20e6333c13027f1407eeb39a3e7407f6c106f47b1bdb3f0059330340643b7b1cd3733d4fa2052ebcef04a0ac6f268204b5cd47852fed
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
- # ᓚᕠᗢ NattyUI v1.0.2
1
+ # ᓚᕠᗢ NattyUI v1.0.3
2
2
 
3
3
  This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely, natty user interface you like to have for your command line applications. It contains elegant, simple and beautiful tools that enhance your command line interfaces functionally and aesthetically.
4
4
 
5
5
  - Gem: [rubygems.org](https://rubygems.org/gems/natty-ui)
6
6
  - Source: [codeberg.org](https://codeberg.org/mblumtritt/natty-ui)
7
- - Help: [rubydoc.info](https://rubydoc.info/gems/natty-ui/1.0.2/NattyUI)
7
+ - Help: [rubydoc.info](https://rubydoc.info/gems/natty-ui/1.0.3/NattyUI)
8
8
 
9
9
  ## Features
10
10
 
@@ -60,7 +60,7 @@ module NattyUI
60
60
  if (cpref = options[:cprefix]).nil? || cpref == true
61
61
  options[:prefix] = pref ? @prefix + pref : @prefix
62
62
  else
63
- cpref = StrConst[cpref] unless StrConst === cpref
63
+ cpref = StrConst[cpref] unless cpref.is_a?(StrConst)
64
64
  options[:cprefix] = @prefix + cpref
65
65
  options[:prefix] = @prefix + (pref || StrConst.spacer(cpref.width))
66
66
  end
@@ -70,7 +70,7 @@ module NattyUI
70
70
  if (csuff = options[:csuffix]).nil? || csuff == true
71
71
  options[:suffix] = suff ? suff + @suffix : @suffix
72
72
  else
73
- csuff = StrConst[csuff] unless StrConst === csuff
73
+ csuff = StrConst[csuff] unless csuff.is_a?(StrConst)
74
74
  options[:csuffix] = csuff + @suffix
75
75
  options[:suffix] = (suff || StrConst.spacer(csuff.width)) + @suffix
76
76
  end
@@ -65,7 +65,7 @@ module NattyUI
65
65
  return self if max_width < 1
66
66
 
67
67
  unless (prefix = popts.delete(:prefix)).nil?
68
- prefix = StrConst[prefix] unless StrConst === prefix
68
+ prefix = StrConst[prefix] unless prefix.is_a?(StrConst)
69
69
  return self if (max_width -= prefix.width) < 1
70
70
  end
71
71
 
@@ -74,7 +74,7 @@ module NattyUI
74
74
  elsif cprefix == true
75
75
  prefix, cprefix = StrConst.spacer(prefix.width), prefix if prefix
76
76
  else
77
- cprefix = StrConst[cprefix] unless StrConst === cprefix
77
+ cprefix = StrConst[cprefix] unless cprefix.is_a?(StrConst)
78
78
  unless prefix
79
79
  return self if (max_width -= cprefix.width) < 1
80
80
  prefix = StrConst.spacer(cprefix.width)
@@ -82,7 +82,7 @@ module NattyUI
82
82
  end
83
83
 
84
84
  unless (suffix = popts.delete(:suffix)).nil?
85
- suffix = StrConst[suffix] unless StrConst === suffix
85
+ suffix = StrConst[suffix] unless suffix.is_a?(StrConst)
86
86
  return self if (max_width -= suffix.width) < 1
87
87
  end
88
88
 
@@ -91,7 +91,7 @@ module NattyUI
91
91
  elsif csuffix == true
92
92
  suffix, csuffix = StrConst.spacer(suffix.width), suffix if suffix
93
93
  else
94
- csuffix = StrConst[csuffix] unless StrConst === csuffix
94
+ csuffix = StrConst[csuffix] unless csuffix.is_a?(StrConst)
95
95
  return self if (max_width -= csuffix.width) < 1
96
96
  suffix = StrConst.spacer(csuffix.width)
97
97
  end
@@ -101,6 +101,7 @@ module NattyUI
101
101
 
102
102
  popts[:ansi] = Terminal.ansi?
103
103
  lines = Text.format(*text, **popts)
104
+
104
105
  if cprefix || csuffix
105
106
  lines.map! do |line|
106
107
  line = "#{cprefix}#{line}#{csuffix}"
@@ -407,7 +408,11 @@ module NattyUI
407
408
  return self unless block_given?
408
409
  yield(table = Table.new)
409
410
  return self if table.empty?
410
- puts(*TableRenderer.lines(columns, table, **options), bbcode: false)
411
+ puts(
412
+ *TableRenderer.lines(columns, table, **options),
413
+ bbcode: false,
414
+ eol: :no_paragraph
415
+ )
411
416
  end
412
417
 
413
418
  #
@@ -422,7 +427,7 @@ module NattyUI
422
427
  #
423
428
  # @example Manual close
424
429
  # tmp = ui.temporary
425
- # ui.puts 'Loading…'
430
+ # tmp.puts 'Loading…'
426
431
  # sleep 1
427
432
  # tmp.end # erases "Loading…"
428
433
  #
@@ -484,7 +489,7 @@ module NattyUI
484
489
  #
485
490
  # @example Manual close
486
491
  # sec = ui.section 'Results'
487
- # ui.ok 'All good'
492
+ # sec.ok 'All good'
488
493
  # sec.end
489
494
  #
490
495
  # @example Block form with title
@@ -570,7 +575,7 @@ module NattyUI
570
575
  #
571
576
  # @example Manual close
572
577
  # frm = ui.frame 'Preview'
573
- # ui.puts 'Content inside the frame.'
578
+ # frm.puts 'Content inside the frame.'
574
579
  # frm.end
575
580
  #
576
581
  # @example Block form with title and custom border
@@ -19,7 +19,7 @@ module NattyUI
19
19
  #
20
20
  # @example Manual close
21
21
  # frm = ui.frame 'Preview'
22
- # ui.puts 'Content inside the frame.'
22
+ # frm.puts 'Content inside the frame.'
23
23
  # frm.end
24
24
  class Frame < Element
25
25
  # @private
@@ -171,13 +171,13 @@ module NattyUI
171
171
  b = value.begin
172
172
  e = value.end
173
173
 
174
- if Integer === b || Float === b
174
+ if b.is_a?(Integer) || b.is_a?(Float)
175
175
  @min_width = [0, b].max
176
176
  return @max_width = @min_width if b == e
177
177
  return @max_width = e && @min_width < e ? e : nil
178
178
  end
179
179
 
180
- if Integer === e || Float === e
180
+ if e.is_a?(Integer) || e.is_a?(Float)
181
181
  @max_width = [0, e].max
182
182
  return @min_width = nil
183
183
  end
@@ -271,13 +271,13 @@ module NattyUI
271
271
  b = value.begin
272
272
  e = value.end
273
273
 
274
- if Integer === b || Float === b
274
+ if b.is_a?(Integer) || b.is_a?(Float)
275
275
  @min_height = [0, b.round].max
276
276
  return @max_height = @min_height if b == e
277
277
  return @max_height = e && @min_height < e ? e.round : nil
278
278
  end
279
279
 
280
- if Integer === e || Float === e
280
+ if e.is_a?(Integer) || e.is_a?(Float)
281
281
  @max_height = [0, e.round].max
282
282
  return @min_height = nil
283
283
  end
@@ -315,7 +315,7 @@ module NattyUI
315
315
  # @raise [ArgumentError] if value has more than 4 elements or wrong type
316
316
  def padding=(value)
317
317
  return @padding = Array.new(4, 0) if value.nil?
318
- return @padding = Array.new(4, value) if Integer === value
318
+ return @padding = Array.new(4, value) if value.is_a?(Integer)
319
319
  unless value.respond_to?(:map)
320
320
  raise(ArgumentError, 'value must be an Integer, Enumerable or nil')
321
321
  end
@@ -785,7 +785,7 @@ module NattyUI
785
785
  # @return [Boolean] `true` if a cell was removed, `false` otherwise
786
786
  def delete(index)
787
787
  cell =
788
- if Cell === index
788
+ if index.is_a?(Cell)
789
789
  @cells.delete(index)
790
790
  else
791
791
  @cells.delete_at(index.to_int)
@@ -1107,7 +1107,7 @@ module NattyUI
1107
1107
  # @return [Boolean] `true` if a row was removed, `false` otherwise
1108
1108
  def delete(index)
1109
1109
  row =
1110
- if Row === index
1110
+ if index.is_a?(Row)
1111
1111
  @rows.delete(index)
1112
1112
  else
1113
1113
  @rows.delete_at(index.to_int)
@@ -16,7 +16,7 @@ module NattyUI
16
16
  #
17
17
  # @example Manual close with left spacing
18
18
  # m = ui.margin left: 2
19
- # ui.puts 'Indented content.'
19
+ # m.puts 'Indented content.'
20
20
  # m.end
21
21
  class Margin < Element
22
22
  # @private
@@ -63,8 +63,8 @@ module NattyUI
63
63
  )
64
64
  end
65
65
 
66
- top = [0, Float === top ? top.round : top.to_int].max
67
- @bottom = [0, Float === @bottom ? @bottom.round : @bottom.to_int].max
66
+ top = [0, top.is_a?(Float) ? top.round : top.to_int].max
67
+ @bottom = [0, @bottom.is_a?(Float) ? @bottom.round : @bottom.to_int].max
68
68
 
69
69
  width = parent.columns
70
70
  if width < 2
@@ -72,8 +72,8 @@ module NattyUI
72
72
  return parent.space(top)
73
73
  end
74
74
 
75
- right = (width * right).round if Float === right
76
- left = (width * left).round if Float === left
75
+ right = (width * right).round if right.is_a?(Float)
76
+ left = (width * left).round if left.is_a?(Float)
77
77
 
78
78
  parent.space(top)
79
79
  @prefix = StrConst.spacer(left.to_int.clamp(0, width))
@@ -126,7 +126,7 @@ module NattyUI
126
126
  end
127
127
 
128
128
  def simple
129
- v = Float === @value ? @value.round(2) : @value
129
+ v = @value.is_a?(Float) ? @value.round(2) : @value
130
130
  "[bright_green]#{'•' * @value}▏[/fg]#{v}"
131
131
  end
132
132
 
@@ -59,13 +59,13 @@ module NattyUI
59
59
 
60
60
  value = [cells.map(&:natural_width).max, cmin].max
61
61
 
62
- cmax = cells.map(&:content_max).compact.min
62
+ cmax = cells.filter_map(&:content_max).min
63
63
  if cmax
64
64
  value = [value, cmax].min
65
65
  max = cmax + pad
66
66
  end
67
67
 
68
- ColWidth.new(cmin, pad, max, value, has_min)
68
+ ColWidth.new(value, pad, has_min, cmin, max)
69
69
  end
70
70
  adjust(cols, @content_width)
71
71
  @rows.each do |row|
@@ -77,33 +77,34 @@ module NattyUI
77
77
  end
78
78
 
79
79
  def adjust(cols, target)
80
- delta = cols.sum(&:value) - target
80
+ delta = (cols.sum(&:value) - target).nonzero? or return
81
81
  # 1) wrap content by shrinking columns down to their content minimum.
82
82
  # Columns without a min_width are shrunk first; a column with a min_width
83
83
  # is only reduced once no unconstrained column can give up more room.
84
84
  while delta > 0
85
- cand = cols.select(&:content_shrinkable?)
85
+ cand = []
86
+ free = []
87
+ cols.each do |c|
88
+ next unless c.shrinkable?
89
+ cand << c
90
+ free << c unless c.has_min
91
+ end
86
92
  break if cand.empty?
87
- free = cand.reject(&:has_min)
88
- (free.empty? ? cand : free).max_by(&:value).value -= 1
93
+ (free.empty? ? cand : free).max_by(&:value).shrink!
89
94
  delta -= 1
90
95
  end
91
96
  # 2) still too wide: trim padding, widest-padded column first
92
97
  while delta > 0
93
- cand = cols.select(&:pad_reducible?)
94
- break if cand.empty?
95
- col = cand.max_by(&:pad)
96
- col.pad -= 1
97
- col.value -= 1
98
+ break if (cand = cols.select(&:pad_reducible?)).empty?
99
+ cand.max_by(&:pad).reduce_pad!
98
100
  delta -= 1
99
101
  end
100
102
  # 3) room to spare: grow columns to fill the width, but only on request
101
103
  # and never past a column's configured max_width
102
104
  return unless @expand
103
105
  while delta < 0
104
- cand = cols.select(&:expandable?)
105
- break if cand.empty?
106
- cand.min_by(&:value).value += 1
106
+ break if (cand = cols.select(&:expandable?)).empty?
107
+ cand.min_by(&:value).expand!
107
108
  delta += 1
108
109
  end
109
110
  end
@@ -113,7 +114,7 @@ module NattyUI
113
114
  row.each(&:reflow!)
114
115
  cells = row.map(&:height)
115
116
  h = [cells.map(&:value).max, cells.map(&:min).max].max
116
- mx = cells.map(&:max).compact.min
117
+ mx = cells.filter_map(&:max).min
117
118
  mx ? [h, mx].min : h
118
119
  end
119
120
  end
@@ -296,13 +297,29 @@ module NattyUI
296
297
  end
297
298
 
298
299
  # @private
299
- ColWidth =
300
- Struct.new(:content_min, :pad, :max, :value, :has_min) do
301
- def content_shrinkable? = value > content_min + pad
302
- def pad_reducible? = pad > 0
303
- def expandable? = max.nil? || value < max
300
+ class ColWidth
301
+ attr_reader :value, :pad, :has_min
302
+
303
+ def shrinkable? = @value > @min
304
+ def expandable? = @max.nil? || @value < @max
305
+ def pad_reducible? = @pad > 0
306
+ def shrink! = (@value -= 1)
307
+ def expand! = (@value += 1)
308
+
309
+ def reduce_pad!
310
+ @value -= 1
311
+ @pad -= 1
304
312
  end
305
313
 
314
+ def initialize(value, pad, has_min, min, max)
315
+ @value = value
316
+ @pad = pad
317
+ @min = min + pad
318
+ @has_min = has_min
319
+ @max = max
320
+ end
321
+ end
322
+
306
323
  # @private
307
324
  class Size
308
325
  attr_reader :min, :max
@@ -311,8 +328,8 @@ module NattyUI
311
328
  def initialize(min, max, value)
312
329
  @min = min
313
330
  @max = max
314
- value = [value, min].max
315
- @value = max && value > max ? max : value
331
+ @value = [value, min].max
332
+ @value = max if max && @value > max
316
333
  end
317
334
  end
318
335
 
@@ -420,7 +437,7 @@ module NattyUI
420
437
 
421
438
  def dim(value, default, max)
422
439
  return default unless value
423
- Float === value ? (value * max).round : value
440
+ value.is_a?(Float) ? (value * max).round : value
424
441
  end
425
442
  end
426
443
  end
@@ -20,7 +20,7 @@ module NattyUI
20
20
  #
21
21
  # @example Manual close
22
22
  # sec = ui.warning 'Disk space low'
23
- # ui.puts 'Only 500 MB remaining.'
23
+ # sec.puts 'Only 500 MB remaining.'
24
24
  # sec.end
25
25
  class Section < Element
26
26
  # @private
@@ -148,7 +148,7 @@ module NattyUI
148
148
  information: [:bright_blue, '[bright_blue]𝒊[/fg] '],
149
149
  warning: [:yellow, '[bright_yellow]![/fg] '],
150
150
  error: [:red, '[bright_red]𝙓[/fg] '],
151
- fatal: [:red, '[bright_red i]𝒊[/fg /i] ']
151
+ fatal: [:red, '[bright_red i]![/fg /i] ']
152
152
  }.compare_by_identity
153
153
  @kind.default = @kind[:default]
154
154
 
@@ -19,10 +19,10 @@ module NattyUI
19
19
  # end # "Thinking…" is erased here
20
20
  #
21
21
  # @example Manual close
22
- # ui.temporary
23
- # ui.puts 'Loading…'
22
+ # temp = ui.temporary
23
+ # temp.puts 'Loading…'
24
24
  # do_work
25
- # ui.end # erases "Loading…"
25
+ # temp.end # erases "Loading…"
26
26
  class Temporary < Element
27
27
  # @private
28
28
  def puts(*objects, **opts)
@@ -12,12 +12,10 @@ module NattyUI
12
12
  end
13
13
  alias [] from_str
14
14
 
15
- def spacer(size = 1)
16
- size < 1 ? @empty : new((' ' * size).freeze, size)
17
- end
15
+ def spacer(size = 1) = @spacer[size]
18
16
 
19
17
  def join(*other)
20
- other.map! { ::StrConst === it ? it : from_str(it) }
18
+ other.map! { is.is_a?(::StrConst) ? it : from_str(it) }
21
19
  new(other.join.freeze, other.sum(&:width))
22
20
  end
23
21
  end
@@ -38,7 +36,7 @@ module NattyUI
38
36
  end
39
37
 
40
38
  def +(other)
41
- other = StrConst[other] unless StrConst === other
39
+ other = StrConst[other] unless other.is_a?(StrConst)
42
40
  StrConst.new((@to_s + other.to_s).freeze, @width + other.width)
43
41
  end
44
42
 
@@ -47,15 +45,16 @@ module NattyUI
47
45
  if str.frozen?
48
46
  str.encoding == ENCODING ? str : str.encode(ENCODING).freeze
49
47
  else
50
- str = str.dup
51
- str.encode!(ENCODING) if str.encoding != ENCODING
52
- str.freeze
48
+ (str.encoding == ENCODING ? str.dup : str.encode(ENCODING)).freeze
53
49
  end
54
50
  @width = width
55
51
  end
56
52
 
57
53
  ENCODING = Encoding::UTF_8
58
- @empty = new('', 0)
54
+ @spacer =
55
+ Hash
56
+ .new { |h, size| h[size] = new((' ' * size).freeze, size) }
57
+ .compare_by_identity
59
58
  end
60
59
 
61
60
  private_constant :StrConst
@@ -14,7 +14,7 @@ module NattyUI
14
14
 
15
15
  def as_width(value, max)
16
16
  return max if value == :auto
17
- (Float === value ? (max * value).round : value.to_int).clamp(0, max)
17
+ (value.is_a?(Float) ? (value * max).round : value.to_int).clamp(0, max)
18
18
  end
19
19
 
20
20
  def as_mark(mark)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NattyUI
4
4
  # The version number of the gem.
5
- VERSION = '1.0.2'
5
+ VERSION = '1.0.3'
6
6
  end
data/lib/natty-ui.rb CHANGED
@@ -118,6 +118,16 @@ module NattyUI
118
118
  # @private
119
119
  def end = self
120
120
 
121
+ # @private
122
+ def end!
123
+ return self if @element == self
124
+ @stack.shift
125
+ @stack.each { it.__send__(:done) }
126
+ @stack = [@element = self]
127
+ Terminal.show_cursor
128
+ self
129
+ end
130
+
121
131
  private
122
132
 
123
133
  def _begin(element)
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: 1.0.2
4
+ version: 1.0.3
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: 1.0.5
18
+ version: 1.0.6
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: 1.0.5
25
+ version: 1.0.6
26
26
  description: 'This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich,
27
27
  lovely, natty user interface tool you like to have for your command line applications.
28
28
  It contains elegant, simple and beautiful features that enhance your command line
@@ -91,7 +91,7 @@ metadata:
91
91
  yard.run: yard
92
92
  source_code_uri: https://codeberg.org/mblumtritt/natty-ui
93
93
  bug_tracker_uri: https://codeberg.org/mblumtritt/natty-ui/issues
94
- documentation_uri: https://rubydoc.info/gems/natty-ui1.0.2
94
+ documentation_uri: https://rubydoc.info/gems/natty-ui/1.0.3
95
95
  rdoc_options: []
96
96
  require_paths:
97
97
  - lib
@@ -106,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
106
  - !ruby/object:Gem::Version
107
107
  version: '0'
108
108
  requirements: []
109
- rubygems_version: 4.0.13
109
+ rubygems_version: 4.0.16
110
110
  specification_version: 4
111
111
  summary: This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely,
112
112
  natty user interface you like to have for your CLI.