natty-ui 0.25.0 → 0.26.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: 23eed1ba95aaecb2555dfeed5a6fedbcd827299d1e4a64c91952953b3d36d787
4
- data.tar.gz: 53380e5ebe647a96abbc24baecd1f24a7a3290544ac10e34e3aa8b1e8dba1e17
3
+ metadata.gz: 93595f761b9be11457f8c8eac967c87f6c91f55d1145aca2f9aa3f35c81a442c
4
+ data.tar.gz: fe3fa5b6844e6513ed9e03cbfdb5338bebf9a1f955f61611053b0b3807771aab
5
5
  SHA512:
6
- metadata.gz: 56c829ee249c7cecce24bb9b88c131e8fdaaec20aff2f0340b16d701c77907805f7595d38cfe0e122d4f7365d6ae6babfccdd680b9754de283c7e63c944e78ea
7
- data.tar.gz: 9964afa255fe3a76d59727f13edcafa31429768729e3b14408f6142f6af17236c7831836857897a0e860044732ad5a347c6bc4efb06ef277412d73d67167519d
6
+ metadata.gz: f19f64cc3898a51bffe330505ddf015790c456dfc765f078b4798fe06584ae803e20fade81f3981d07a25342598ecfbc4ce1ee525866cbc865a7ab5a21706c96
7
+ data.tar.gz: 96ff67e741b3caf88472547c0c205d403fdaa2bb9f21f01d4d8c0eff5c984cb0c1e6c0a12fc70c17bbf7a5fda5cc764693e15de07093c617e889517c0f57a368
@@ -3,12 +3,12 @@
3
3
  require_relative '../lib/natty-ui'
4
4
 
5
5
  ui.message '[b]​ᓚᕠᗢ NattyUI[/b] [i green]24-bit Color Support[/]' do
6
- ui.space
7
6
  bar = '█' * ui.columns
8
7
  rainbow =
9
8
  (7..17).map do |i|
10
9
  NattyUI::Ansi.rainbow(bar, seed: 3, spread: 3.25, frequency: i / 100.0)
11
10
  end
12
11
 
12
+ ui.space
13
13
  ui.puts(*rainbow)
14
14
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../lib/natty-ui'
4
+
5
+ raw = name = :start
6
+ while name != 'Esc'
7
+ ui.temporary do
8
+ ui.message '[b]​ᓚᕠᗢ NattyUI[/b] [i green]Keyboard Key Codes[/]' do
9
+ ui.puts(
10
+ "\n#{
11
+ case name
12
+ when :start
13
+ "Your terminal uses [i]#{
14
+ Terminal.input_mode
15
+ }[/i] mode. Press a key!"
16
+ when nil
17
+ "[green]#{raw.inspect}[/fg] → [bold bright_green][\\#{raw}]"
18
+ else
19
+ "[green]#{raw.inspect}[/fg] → [bold bright_green]#{
20
+ name.split('+').map! { "[\\#{_1}]" }.join(' ')
21
+ }"
22
+ end
23
+ }\n\n[bright_black](Exit with ESC)"
24
+ )
25
+ end
26
+ raw, name = Terminal.read_key(mode: :both)
27
+ end
28
+ end
data/examples/ls.rb CHANGED
@@ -31,4 +31,20 @@ ui.message '[b]​ᓚᕠᗢ NattyUI[/b] [i green]Print Lists[/]' do
31
31
  ui.space
32
32
  ui.h2 'Compact Display'
33
33
  ui.ls lorem, glyph: 1
34
+
35
+ ui.space
36
+ ui.h2 'Latin Char Glyph'
37
+ ui.ls lorem.take(5), glyph: :a
38
+
39
+ ui.space
40
+ ui.h2 'Hex Numbers'
41
+ ui.ls lorem.take(5), glyph: '0x08'
42
+
43
+ ui.space
44
+ ui.h2 'Chapter Numbering'
45
+ ui.ls lorem.take(5), glyph: :'1.8'
46
+
47
+ ui.space
48
+ ui.h2 'Custom Glyph'
49
+ ui.ls lorem.take(5), glyph: '[b bright_yellow]→[/]'
34
50
  end
@@ -18,6 +18,6 @@ ui.message '[b]​ᓚᕠᗢ NattyUI[/b] [i green]Named Colors Support[/]' do
18
18
  NattyUI::Ansi
19
19
  .named_colors
20
20
  .delete_if { /\d/.match?(_1) }
21
- .map! { "[bg_#{_1}] [/bg] [#{_1}]#{_1.to_s.ljust(22)}[/fg]" }
21
+ .map! { "[bg_#{_1}] [/bg] [#{_1}]#{_1}[/fg]" }
22
22
  )
23
23
  end
data/examples/sections.rb CHANGED
@@ -13,7 +13,7 @@ ui.message '[b]​ᓚᕠᗢ NattyUI[/b] [i green]Sections[/]' do
13
13
  end
14
14
 
15
15
  ui.space
16
- ui.warning 'Warning', 'This is a warning message example.'
16
+ ui.warning "Warning\nThis is a warning message example."
17
17
 
18
18
  ui.space
19
19
  ui.error 'Error Message' do
@@ -23,7 +23,4 @@ ui.message '[b]​ᓚᕠᗢ NattyUI[/b] [i green]Sections[/]' do
23
23
  cc.add 'You can add all other elements to a section.', vertical: :middle
24
24
  end
25
25
  end
26
-
27
- ui.space
28
- ui.failed "Fail Message\nA sad failure message."
29
26
  end
@@ -33,10 +33,10 @@ module NattyUI
33
33
  end
34
34
 
35
35
  alias _to_s to_s
36
+ private :_to_s
36
37
 
37
38
  # @!visibility private
38
39
  alias to_s inspect
39
- private :_to_s
40
40
 
41
41
  private
42
42
 
@@ -46,33 +46,30 @@ module NattyUI
46
46
  end
47
47
  end
48
48
 
49
- module StateMixin
50
- attr_reader :state
49
+ module WithStatus
50
+ attr_reader :status
51
51
 
52
- def closed? = @state ? true : false
52
+ def active? = @status.nil?
53
+ def closed? = !active?
53
54
  def ok? = @state == :ok
54
55
  def failed? = @state == :failed
55
56
 
56
- # @return [Element] itself
57
- def done(*text) = @state ? self : finish_ok(text)
58
- alias ok done
59
-
60
- # @!visibility private
61
- def failed(title, *msg)
62
- return if @state
63
- super
64
- finish_failed
57
+ def ok(*text)
58
+ return self if @state
59
+ text = [@title] if text.empty? && @title
60
+ _done(text)
61
+ @state = :ok
62
+ self
65
63
  end
64
+ alias done ok
66
65
 
67
- # @!visibility private
68
- def inspect = "#{_to_s.chop} state=#{@state.inspect}>"
69
-
70
- protected
71
-
72
- def finish_failed
66
+ def failed(*text, &block)
67
+ return self if @state
68
+ _failed
73
69
  @state = :failed
70
+ text = [@title] if text.empty? && @title
71
+ @parent.failed(*text, &block)
74
72
  self
75
73
  end
76
74
  end
77
- private_constant :StateMixin
78
75
  end
@@ -292,7 +292,7 @@ module NattyUI
292
292
  #
293
293
  # @return (see puts)
294
294
  def space(count = 1)
295
- (count = count.to_i).positive? ? puts(*Array.new(count, "\n")) : self
295
+ puts("\n" * count) if (count = count.to_i).positive?
296
296
  end
297
297
 
298
298
  # Print given items as list (like 'ls' command).
@@ -327,7 +327,7 @@ module NattyUI
327
327
  # Generate and print a table.
328
328
  # See {Table} for much more details about table generation.
329
329
  #
330
- # @example Draw a very simple table with three rows, four columns, complete borders
330
+ # @example Draw a very simple 3x4 table with complete borders
331
331
  # ui.table(border: :default, border_around: true, padding: [0, 1]) do |table|
332
332
  # table.add 1, 2, 3, 4
333
333
  # table.add 5, 6, 7, 8
@@ -444,7 +444,7 @@ module NattyUI
444
444
  # @param pin [true, false]
445
445
  # whether the final progress state should be "pinned" to parent element
446
446
  #
447
- # @return [Progress]
447
+ # @return [ProgressHelper]
448
448
  # itself
449
449
  #
450
450
  # @overload progress(title, max: nil, pin: false, &block)
@@ -455,7 +455,7 @@ module NattyUI
455
455
  # @param pin [true, false]
456
456
  # whether the final progress state should be "pinned" to parent element
457
457
  #
458
- # @yieldparam progress [Progress]
458
+ # @yieldparam progress [ProgressHelper]
459
459
  # itself
460
460
  #
461
461
  # @return [Object]
@@ -555,7 +555,7 @@ module NattyUI
555
555
  # @param align [:left, :right, :centered]
556
556
  # text alignment,
557
557
  # see {Attributes::Align}
558
- # @param border: [Symbol]
558
+ # @param border [Symbol]
559
559
  # kind of border,
560
560
  # see {Attributes::Border}
561
561
  # @param border_style [Enumerable<Symbol>]
@@ -19,7 +19,7 @@ module NattyUI
19
19
  def as_items(items, glyph)
20
20
  items.flatten!
21
21
  glyph = as_glyph(glyph, items.size)
22
- items.map! { |item| Item.new(item = glyph[item], Text.width(item)) }
22
+ items.map! { Item.new(glyph[_1]) }
23
23
  end
24
24
 
25
25
  def as_glyph(glyph, size)
@@ -27,30 +27,34 @@ module NattyUI
27
27
  when nil, false
28
28
  lambda(&:itself)
29
29
  when :hex
30
- pad = size.to_s(16).size
30
+ pad = [2, size.to_s(16).size].max
31
31
  glyph = 0
32
32
  ->(s) { "#{(glyph += 1).to_s(16).rjust(pad, '0')} #{s}" }
33
+ when /\A0x(\h+)\z/
34
+ glyph = Regexp.last_match(1).hex - 1
35
+ pad = [2, (glyph + size).to_s(16).size].max
36
+ ->(s) { "0x#{(glyph += 1).to_s(16).rjust(pad, '0')} #{s}" }
33
37
  when Integer
34
38
  pad = (glyph + size).to_s.size
35
39
  glyph -= 1
36
- ->(s) { "#{(glyph += 1).to_s.rjust(pad)} #{s}" }
40
+ ->(s) { "#{(glyph += 1).to_s.rjust(pad, ' ')} #{s}" }
37
41
  when Symbol
38
- lambda do |s|
39
- "#{
40
- t = glyph
41
- glyph = glyph.succ
42
- t
43
- } #{s}[/]"
44
- end
42
+ ->(s) { "#{[glyph, glyph = glyph.succ][0]} #{s}" }
45
43
  else
46
44
  ->(s) { "#{glyph} #{s}" }
47
45
  end
48
46
  end
49
47
 
50
- Item =
51
- Struct.new(:str, :width) do
52
- def to_s(in_width) = "#{str}#{' ' * (in_width - width)}"
48
+ class Item
49
+ attr_reader :width
50
+
51
+ def to_s(in_width) = "#{@str}#{' ' * (in_width - @width)}"
52
+
53
+ def initialize(str)
54
+ @str = str
55
+ @width = Text.width(str)
53
56
  end
57
+ end
54
58
  private_constant :Item
55
59
  end
56
60
  end
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'element'
4
+
3
5
  module NattyUI
4
6
  # @todo This chapter needs more documentation.
5
7
  #
6
8
  # Progress indictaor helper used by {Features.progress}.
7
9
  #
8
10
  module ProgressHelper
11
+ include WithStatus
12
+
9
13
  # @return [String]
10
14
  attr_reader :title
11
15
 
@@ -40,21 +44,12 @@ module NattyUI
40
44
  redraw
41
45
  end
42
46
 
43
- def active? = @status.nil?
44
- def ok? = @status == :ok
45
- def failed? = @status == :failed
46
-
47
47
  def step(count: 1, title: nil)
48
48
  @title = title if title
49
49
  self.value += count
50
50
  self
51
51
  end
52
52
 
53
- def done(title = nil) = finish(:ok, title)
54
- alias ok done
55
-
56
- def failed(title = nil) = finish(:failed, title)
57
-
58
53
  alias _to_s to_s
59
54
  private :_to_s
60
55
 
@@ -80,27 +75,26 @@ module NattyUI
80
75
 
81
76
  private
82
77
 
83
- def finish(status, title)
84
- return if @status
85
- @status = status
78
+ def _done(text)
86
79
  NattyUI.back_to_line(@pin_line)
87
- if status == :failed
88
- @parent.failed(title || @title)
89
- else
90
- cm = Theme.current.mark(:checkmark)
91
- @parent.puts(
92
- title || @title,
93
- pin: @pin,
94
- first_line_prefix: cm,
95
- first_line_prefix_width: cm.width
96
- )
97
- end
98
- self
80
+ @pin_line = nil
81
+ cm = Theme.current.mark(:checkmark)
82
+ @parent.puts(
83
+ *text,
84
+ pin: @pin,
85
+ first_line_prefix: cm,
86
+ first_line_prefix_width: cm.width
87
+ )
88
+ end
89
+
90
+ def _failed
91
+ NattyUI.back_to_line(NattyUI.lines_written - 1) if @last&.size == 2
92
+ @pin_line = nil
99
93
  end
100
94
 
101
95
  def initialize(parent, title, max, pin)
102
96
  @parent = parent
103
- @value = 0.0
97
+ @value = 0
104
98
  @title = title
105
99
  @pin = pin
106
100
  @pin_line = NattyUI.lines_written
@@ -123,7 +117,7 @@ module NattyUI
123
117
  end
124
118
 
125
119
  def moving_bar
126
- "#{@style}#{'·' * @value}" if @value >= 1
120
+ "#{@style}#{'' * @value}" if @value >= 1
127
121
  end
128
122
 
129
123
  def bar(diff)
@@ -143,20 +137,18 @@ module NattyUI
143
137
 
144
138
  private
145
139
 
146
- def finish(status, title)
147
- return if @status
148
- @status = status
149
- if status == :failed
150
- @parent.failed(title || @title)
151
- else
152
- cm = Theme.current.mark(:checkmark)
153
- @parent.puts(
154
- title || @title,
155
- first_line_prefix: cm,
156
- first_line_prefix_width: cm.width
157
- )
158
- end
159
- self
140
+ def _done(text)
141
+ cm = Theme.current.mark(:checkmark)
142
+ @parent.puts(
143
+ *text,
144
+ pin: @pin,
145
+ first_line_prefix: cm,
146
+ first_line_prefix_width: cm.width
147
+ )
148
+ end
149
+
150
+ def _failed
151
+ # nop
160
152
  end
161
153
 
162
154
  def initialize(parent, title, max)
@@ -3,7 +3,7 @@
3
3
  require_relative 'element'
4
4
 
5
5
  module NattyUI
6
- # {Element} implemting a display section used by
6
+ # Display section used by
7
7
  #
8
8
  # - {Features.section}
9
9
  # - {Features.message}
@@ -13,23 +13,20 @@ module NattyUI
13
13
  # - {Features.failed}
14
14
  #
15
15
  class Section < Element
16
- include StateMixin
16
+ include WithStatus
17
17
 
18
18
  # @!visibility private
19
19
  def puts(*objects, **options) = @state ? self : super
20
20
 
21
21
  private
22
22
 
23
- def finish_ok(text)
23
+ def _done(text)
24
24
  puts(*text) unless text.empty?
25
- @state = :ok
26
25
  @parent.puts(@border.bottom)
27
- self
28
26
  end
29
27
 
30
- def finish_failed
28
+ def _failed
31
29
  @parent.puts(@border.bottom)
32
- super
33
30
  end
34
31
 
35
32
  def show_title(title)
@@ -25,12 +25,19 @@ module NattyUI
25
25
  @max_width, @columns = WidthFinder.find(columns, max_width)
26
26
  init_borders(attributes)
27
27
  @columns = @columns.each.with_index
28
+
28
29
  @lines = render(rows.shift)
29
30
  @lines.unshift(@b_top) if @b_top
30
- rows.each do |row|
31
- @lines << @b_between if @b_between
32
- @lines += render(row)
31
+
32
+ if @b_between
33
+ rows.each do |row|
34
+ @lines << @b_between
35
+ @lines += render(row)
36
+ end
37
+ else
38
+ rows.each { |row| @lines += render(row) }
33
39
  end
40
+
34
41
  @lines << @b_bottom if @b_bottom
35
42
  end
36
43
 
@@ -47,6 +54,7 @@ module NattyUI
47
54
  chars = attributes.border_chars or return
48
55
  style = border_style(attributes)
49
56
  @b_inner = style[chars[9]]
57
+ return if chars[10] == ' '
50
58
  return init_borders_around(chars, style) if attributes.border_around
51
59
  @b_between = chars[10] * (@max_width + @columns.size - 1)
52
60
  i = -1
data/lib/natty-ui/task.rb CHANGED
@@ -3,25 +3,28 @@
3
3
  require_relative 'element'
4
4
 
5
5
  module NattyUI
6
- # {Element} implemting a task display section used by {Features.task}.
6
+ # Task display section used by {Features.task}.
7
7
  #
8
8
  class Task < Temporary
9
- include StateMixin
9
+ include WithStatus
10
10
 
11
11
  private
12
12
 
13
- def finish_ok(text)
14
- NattyUI.back_to_line(@start_line)
13
+ def _done(text)
14
+ NattyUI.back_to_line(@start_line, erase: :all)
15
15
  @start_line = nil
16
16
  cm = Theme.current.mark(:checkmark)
17
- text << @title if text.empty?
18
17
  @parent.puts(
19
18
  *text,
20
19
  first_line_prefix: cm,
21
20
  first_line_prefix_width: cm.width,
22
21
  pin: @pin
23
22
  )
24
- super()
23
+ @pins&.each { |objects, options| puts(*objects, **options) }
24
+ end
25
+
26
+ def _failed
27
+ @start_line = nil
25
28
  end
26
29
 
27
30
  def initialize(parent, title, msg, pin)
@@ -3,7 +3,7 @@
3
3
  require_relative 'element'
4
4
 
5
5
  module NattyUI
6
- # {Element} implemting a temprary display section used by
6
+ # Temprary display section used by
7
7
  # {Features.temporary}.
8
8
  #
9
9
  class Temporary < Element
@@ -20,7 +20,7 @@ module NattyUI
20
20
  # @!visibility private
21
21
  def done
22
22
  return self if @state
23
- NattyUI.back_to_line(@start_line) if @start_line
23
+ NattyUI.back_to_line(@start_line, erase: :all) if @start_line
24
24
  @pins&.each { |objects, options| puts(*objects, **options) }
25
25
  @state = :ok
26
26
  self
@@ -28,8 +28,6 @@ module NattyUI
28
28
 
29
29
  private
30
30
 
31
- alias finish_ok done
32
-
33
31
  def initialize(parent)
34
32
  @start_line = NattyUI.lines_written
35
33
  super
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NattyUI
4
4
  # The version number of the gem.
5
- VERSION = '0.25.0'
5
+ VERSION = '0.26.0'
6
6
  end
data/lib/natty-ui.rb CHANGED
@@ -58,12 +58,12 @@ module NattyUI
58
58
  def title=(value)
59
59
  if value
60
60
  title = Ansi.plain(value).gsub(/\s+/, ' ')
61
- _write(Ansi.tab_title(title)) if Terminal.ansi?
61
+ _write(Ansi.title(title)) if Terminal.ansi?
62
62
  @title_stack << title
63
63
  else
64
64
  @title_stack.pop
65
65
  last = @title_stack[-1]
66
- _write(Ansi.tab_title(last)) if last && Terminal.ansi?
66
+ _write(Ansi.title(last)) if last && Terminal.ansi?
67
67
  end
68
68
  end
69
69
 
@@ -74,7 +74,13 @@ module NattyUI
74
74
  def back_to_line(number, erase: true)
75
75
  return @lines_written if (c = @lines_written - number) <= 0
76
76
  if Terminal.ansi?
77
- _write(erase ? (Ansi::LINE_ERASE_PREV * c) : Ansi.cursor_prev_line(c))
77
+ _write(
78
+ if erase == :all
79
+ Ansi.cursor_prev_line(c) + Ansi::SCREEN_ERASE_BELOW
80
+ else
81
+ erase ? (Ansi::LINE_ERASE_PREV * c) : Ansi.cursor_prev_line(c)
82
+ end
83
+ )
78
84
  end
79
85
  @lines_written = number
80
86
  end
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.25.0
4
+ version: 0.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -13,16 +13,16 @@ dependencies:
13
13
  name: terminal_rb
14
14
  requirement: !ruby/object:Gem::Requirement
15
15
  requirements:
16
- - - ">"
16
+ - - ">="
17
17
  - !ruby/object:Gem::Version
18
- version: 0.9.5
18
+ version: 0.11.0
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.9.5
25
+ version: 0.11.0
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.
@@ -45,6 +45,7 @@ files:
45
45
  - examples/elements.rb
46
46
  - examples/examples.rb
47
47
  - examples/illustration.rb
48
+ - examples/key-codes.rb
48
49
  - examples/ls.rb
49
50
  - examples/named-colors.rb
50
51
  - examples/sections.rb
@@ -76,7 +77,7 @@ metadata:
76
77
  rubygems_mfa_required: 'true'
77
78
  source_code_uri: https://github.com/mblumtritt/natty-ui
78
79
  bug_tracker_uri: https://github.com/mblumtritt/natty-ui/issues
79
- documentation_uri: https://rubydoc.info/gems/natty-ui/0.25.0/NattyUI/
80
+ documentation_uri: https://rubydoc.info/gems/natty-ui/0.26.0/NattyUI
80
81
  rdoc_options: []
81
82
  require_paths:
82
83
  - lib
@@ -91,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
94
  requirements: []
94
- rubygems_version: 3.6.9
95
+ rubygems_version: 3.7.1
95
96
  specification_version: 4
96
97
  summary: This is the beautiful, nice, nifty, fancy, neat, pretty, cool, rich, lovely,
97
98
  natty user interface you like to have for your CLI.