natty-ui 0.6.0 → 0.7.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: 666f2716f2f1d017789bbbfef2d640b7c89af6d006ea051e8575282c806b88f6
4
- data.tar.gz: 44922a307f8f62e69d3df00ba656be1164ba20234ac12d621df0eb86fa5ce39e
3
+ metadata.gz: 955176038d4388a0b55468997a267434bede767869071193c3fc45526ebbc3e5
4
+ data.tar.gz: 6fdcd1f82ae1ee09bbd5650050edd3493eedbe584263921e1e6d5abf30900b5c
5
5
  SHA512:
6
- metadata.gz: 74d474a1f3876ae3825cadc289250a8f3fb545b4147c26c20861a90b3e9665b249cde0075ae9813a1d668c69671540cb332aaf75cae667fc4a9fe5c0d272a88e
7
- data.tar.gz: 4a872db3d8b96ddf6edf42b865eb9dffc8ee87994d6ae6617c842055062eeca4c45f4bac8df644b1e1a0a7b0fd5e4cfd6c53cee88c6a66681657b83879f12c19
6
+ metadata.gz: 5104516012022478512ad583b12e5110941013849f074ac3156ef34076654ad86ebd2a723b598c595984f3a5799c831106ec17add1a17da26b7839f19746b957
7
+ data.tar.gz: 6f55cc25b480284da8f77e284b13768b9b84b3dbf1efef15dcec7999227d1fb04f1b9addc570a0ae536d44111b94c685238319c6605a425d42eac8f7613c4e23
data/.yardopts ADDED
@@ -0,0 +1,9 @@
1
+ --readme README.md
2
+ --title 'NattyUI Documentation'
3
+ --charset utf-8
4
+ --markup markdown
5
+ --tag comment
6
+ --hide-tag comment
7
+ --embed-mixins
8
+ --exclude lib/natty-ui/ansi_wrapper.rb
9
+ 'lib/**/*.rb' - 'README.md' 'LICENSE'
data/README.md CHANGED
@@ -92,4 +92,3 @@ Since I did not complete the tests and not all my ideas are already implemented
92
92
 
93
93
  - add more samples to help
94
94
  - add more tests
95
- - password prompt
@@ -4,9 +4,6 @@ require 'natty-ui'
4
4
 
5
5
  UI = NattyUI::StdOut
6
6
 
7
- # helper:
8
- def hex(int) = int.to_s(16).rjust(2, '0')
9
-
10
7
  UI.space
11
8
  UI.h1 'NattyUI ANSI Attributes Demo', <<~TEXT
12
9
 
@@ -28,17 +25,17 @@ UI.h2 'Attributes', <<~TEXT
28
25
 
29
26
  Different font types are very rarely displayed:
30
27
 
31
- - [[primary_font]]primary_font[[/]]
32
- - [[font1]]font1[[/]]
33
- - [[font2]]font2[[/]]
34
- - [[font3]]font3[[/]]
35
- - [[font4]]font4[[/]]
36
- - [[font5]]font5[[/]]
37
- - [[font6]]font6[[/]]
38
- - [[font7]]font7[[/]]
39
- - [[font8]]font8[[/]]
40
- - [[font9]]font9[[/]]
41
- - [[fraktur]]fraktur[[/]]
28
+ [[primary_font]]primary_font[[/]]
29
+ [[font1]]font1[[/]]
30
+ [[font2]]font2[[/]]
31
+ [[font3]]font3[[/]]
32
+ [[font4]]font4[[/]]
33
+ [[font5]]font5[[/]]
34
+ [[font6]]font6[[/]]
35
+ [[font7]]font7[[/]]
36
+ [[font8]]font8[[/]]
37
+ [[font9]]font9[[/]]
38
+ [[fraktur]]fraktur[[/]]
42
39
 
43
40
  TEXT
44
41
 
@@ -56,25 +53,24 @@ UI.h2 '8-bit Colors' do |sec|
56
53
  sec.puts 'There are 256 pre-defined color which can be used by their index:'
57
54
  sec.space
58
55
 
59
- colors_std = 0.upto(15).map { |i| hex(i) }
60
- colors216 = 16.upto(231).lazy.map { |i| hex(i) }
61
- colors_gray1 = 232.upto(243).map { |i| hex(i) }
62
- colors_gray2 = 244.upto(255).map { |i| hex(i) }
56
+ # helper:
57
+ colors_std = 0.upto(15)
58
+ colors216 = 16.upto(231).lazy
59
+ colors_gray1 = 232.upto(243)
60
+ colors_gray2 = 244.upto(255)
61
+ as_color = ->(i) { "[[#{i = i.to_s(16).rjust(2, '0')}]] #{i} [[/]]" }
62
+ as_bgcolor = ->(i) { "[[on:#{i.to_s(16).rjust(2, '0')}]] #{i} [[/]]" }
63
63
 
64
- sec.puts colors_std.map { |i| "[[#{i}]] #{i} [[/]]" }.join
65
- colors216.each_slice(18) do |slice|
66
- sec.puts slice.map { |i| "[[#{i}]] #{i} [[/]]" }.join
67
- end
68
- sec.puts colors_gray1.map { |i| "[[#{i}]] #{i} [[/]]" }.join
69
- sec.puts colors_gray2.map { |i| "[[#{i}]] #{i} [[/]]" }.join
64
+ sec.puts colors_std.map(&as_color).join
65
+ colors216.each_slice(18) { |slice| sec.puts slice.map(&as_color).join }
66
+ sec.puts colors_gray1.map(&as_color).join
67
+ sec.puts colors_gray2.map(&as_color).join
70
68
 
71
69
  sec.space
72
- sec.puts colors_std.map { |i| "[[on:#{i}]] #{i} [[/]]" }.join
73
- colors216.each_slice(18) do |slice|
74
- sec.puts slice.map { |i| "[[on:#{i}]] #{i} [[/]]" }.join
75
- end
76
- sec.puts colors_gray1.map { |i| "[[on:#{i}]] #{i} [[/]]" }.join
77
- sec.puts colors_gray2.map { |i| "[[on:#{i}]] #{i} [[/]]" }.join
70
+ sec.puts colors_std.map(&as_bgcolor).join
71
+ colors216.each_slice(18) { |slice| sec.puts slice.map(&as_bgcolor).join }
72
+ sec.puts colors_gray1.map(&as_bgcolor).join
73
+ sec.puts colors_gray2.map(&as_bgcolor).join
78
74
  end
79
75
 
80
76
  UI.space
data/examples/query.rb CHANGED
@@ -33,4 +33,11 @@ unless answer
33
33
  UI.failed 'aborted'
34
34
  exit false
35
35
  end
36
- UI.info "Your answer: #{answer}"
36
+ UI.info "Your answer: #{answer.inspect}"
37
+
38
+ answer = UI.request('What is your current password?', password: true)
39
+ unless answer
40
+ UI.failed 'aborted'
41
+ exit false
42
+ end
43
+ UI.info "I'll keep your secret!"
data/lib/natty-ui/ansi.rb CHANGED
@@ -73,8 +73,8 @@ module NattyUI
73
73
  # @return [String] ANSI code to move the cursor to given column
74
74
  def cursor_column(columns = nil) = "\e[#{columns}G"
75
75
 
76
- # @return [String] ANSI code poition the cursor on right hand side of the
77
- # terminal
76
+ # @return [String] ANSI code positioning the cursor on right hand side of
77
+ # the terminal
78
78
  def cursor_right_aligned = "\e[9999G\e[D\e[C"
79
79
 
80
80
  # @return [String] ANSI code to hide the cursor
@@ -114,13 +114,12 @@ module NattyUI
114
114
  end
115
115
 
116
116
  # Remove ANSI attribtes from given string.
117
- # This will only remove attributes and colors, not other control codes.
118
117
  #
119
118
  # @see embellish
120
119
  #
121
120
  # @param str [#to_s] string to be modified
122
121
  # @return [String] string without ANSI attributes
123
- def blemish(str) = str.to_s.gsub(/(\x1b\[(?~m)m)/, '')
122
+ def blemish(str) = str.to_s.gsub(/(\x1b\[(?~[a-zA-Z])[a-zA-Z])/, '')
124
123
 
125
124
  # Combine given ANSI `attributes`.
126
125
  #
@@ -228,8 +227,7 @@ module NattyUI
228
227
  # @return [String] combined ANSI attributes
229
228
  # @return [nil] when string does not contain valid attributes
230
229
  def try_convert(attributes)
231
- attributes = attributes.to_s.split
232
- return if attributes.empty?
230
+ return if (attributes = attributes.to_s.split).empty?
233
231
  "\e[#{
234
232
  attributes
235
233
  .map { |arg| ATTRIBUTES[arg] || color(arg) || return }
@@ -247,36 +245,37 @@ module NattyUI
247
245
  )
248
246
  end
249
247
 
250
- def color(value)
251
- case value
252
- when /\A(fg_|fg:|fg)?([[:xdigit:]]{2})\z/
253
- "38;5;#{Regexp.last_match(2).hex}"
254
- when /\A(bg_|bg:|bg|on_|on:|on)([[:xdigit:]]{2})\z/
255
- "48;5;#{Regexp.last_match(2).hex}"
256
- when /\A(ul_|ul:|ul)([[:xdigit:]]{2})\z/
257
- "58;5;#{Regexp.last_match(2).hex}"
258
- when /\A(fg_|fg:|fg)?#?([[:xdigit:]]{3})\z/
259
- hex_rgb_short(38, Regexp.last_match(2))
260
- when /\A(fg_|fg:|fg)?#?([[:xdigit:]]{6})\z/
261
- hex_rgb(38, Regexp.last_match(2))
262
- when /\A(bg_|bg:|bg|on_|on:|on)#?([[:xdigit:]]{3})\z/
263
- hex_rgb_short(48, Regexp.last_match(2))
264
- when /\A(bg_|bg:|bg|on_|on:|on)#?([[:xdigit:]]{6})\z/
265
- hex_rgb(48, Regexp.last_match(2))
266
- when /\A(ul_|ul:|ul)#?([[:xdigit:]]{3})\z/
267
- hex_rgb_short(58, Regexp.last_match(2))
268
- when /\A(ul_|ul:|ul)#?([[:xdigit:]]{6})\z/
269
- hex_rgb(58, Regexp.last_match(2))
248
+ def color(val)
249
+ val = val.to_s.downcase
250
+ base =
251
+ if val.delete_prefix!('fg')
252
+ val.delete_prefix!(':') || val.delete_prefix!('_')
253
+ '38;'
254
+ elsif val.delete_prefix!('ul')
255
+ val.delete_prefix!(':') || val.delete_prefix!('_')
256
+ '58;'
257
+ elsif val.delete_prefix!('bg') || val.delete_prefix!('on')
258
+ val.delete_prefix!(':') || val.delete_prefix!('_')
259
+ '48;'
260
+ else
261
+ '38;'
262
+ end
263
+ val.delete_prefix!('#')
264
+ case val.size
265
+ when 2
266
+ "#{base}5;#{val.hex}" if /\A[[:xdigit:]]+\z/.match?(val)
267
+ when 3
268
+ if /\A[[:xdigit:]]+\z/.match?(val)
269
+ "#{base}2;#{(val[0] * 2).hex};#{(val[1] * 2).hex};#{
270
+ (val[2] * 2).hex
271
+ }"
272
+ end
273
+ when 6
274
+ if /\A[[:xdigit:]]+\z/.match?(val)
275
+ "#{base}2;#{val[0, 2].hex};#{val[2, 2].hex};#{val[4, 2].hex}"
276
+ end
270
277
  end
271
278
  end
272
-
273
- def hex_rgb_short(base, str)
274
- "#{base};2;#{(str[0] * 2).hex};#{(str[1] * 2).hex};#{(str[2] * 2).hex}"
275
- end
276
-
277
- def hex_rgb(base, str)
278
- "#{base};2;#{str[0, 2].hex};#{str[2, 2].hex};#{str[4, 2].hex}"
279
- end
280
279
  end
281
280
 
282
281
  ATTRIBUTES =
@@ -22,10 +22,7 @@ module NattyUI
22
22
 
23
23
  protected
24
24
 
25
- def embellish(obj)
26
- obj = NattyUI.embellish(obj)
27
- obj.empty? ? nil : obj
28
- end
25
+ def embellish(obj) = (obj = NattyUI.embellish(obj)).empty? ? nil : obj
29
26
 
30
27
  def temp_func
31
28
  count = @lines_written
@@ -68,6 +65,7 @@ module NattyUI
68
65
  task: 117
69
66
  }.compare_by_identity.freeze
70
67
  end
68
+ private_constant :Message
71
69
 
72
70
  class Section < Section
73
71
  def temporary
@@ -96,6 +94,7 @@ module NattyUI
96
94
  @prefix = Ansi.embellish(@prefix, *prefix_attr)
97
95
  end
98
96
  end
97
+ private_constant :Section
99
98
 
100
99
  class Heading < Heading
101
100
  protected
@@ -108,13 +107,14 @@ module NattyUI
108
107
  PREFIX = Ansi[39].freeze
109
108
  MSG = Ansi[:bold, 231].freeze
110
109
  end
110
+ private_constant :Heading
111
111
 
112
112
  class Framed < Section
113
113
  protected
114
114
 
115
115
  def initialize(parent, title:, type:, **opts)
116
116
  @parent = parent
117
- title = title.to_s.tr("\r\n", '')
117
+ title = title.to_s.tr("\r\n\t", '')
118
118
  topl, topr, botl, botr, hor, vert = *components(type)
119
119
  width = available_width
120
120
  rcount = [width - _plain_width(title) - 6, 0].max
@@ -152,6 +152,7 @@ module NattyUI
152
152
  double: %w[╔ ╗ ╚ ╝ ═ ║]
153
153
  }.compare_by_identity.freeze
154
154
  end
155
+ private_constant :Framed
155
156
 
156
157
  class Ask < Ask
157
158
  protected
@@ -164,6 +165,7 @@ module NattyUI
164
165
 
165
166
  PREFIX = "#{Ansi[:bold, :italic, 220]}▶︎#{Ansi[:reset, 220]}".freeze
166
167
  end
168
+ private_constant :Ask
167
169
 
168
170
  class Request < Request
169
171
  def prompt(question) = "#{prefix}#{PREFIX} #{question}#{Ansi.reset} "
@@ -172,6 +174,7 @@ module NattyUI
172
174
  PREFIX = "#{Ansi[:bold, :italic, 220]}▶︎#{Ansi[:reset, 220]}".freeze
173
175
  FINISH = (Ansi.cursor_line_up + Ansi.line_erase_to_end).freeze
174
176
  end
177
+ private_constant :Request
175
178
 
176
179
  class Query < Query
177
180
  protected
@@ -183,11 +186,13 @@ module NattyUI
183
186
 
184
187
  PROMPT = Ansi.embellish(':', :bold, 220).freeze
185
188
  end
189
+ private_constant :Query
186
190
 
187
191
  class Task < Message
188
192
  include ProgressAttributes
189
193
  include TaskMethods
190
194
  end
195
+ private_constant :Task
191
196
 
192
197
  class Progress < Progress
193
198
  protected
@@ -196,12 +201,9 @@ module NattyUI
196
201
  @prefix = "#{prefix}#{TITLE_PREFIX}#{title}#{Ansi.reset} "
197
202
  (wrapper.stream << @prefix << Ansi.cursor_hide).flush
198
203
  @prefix = "#{Ansi.line_clear}#{@prefix}"
199
- if @max_value
200
- @prefix << BAR_COLOR
201
- else
202
- @prefix << INDICATOR_ATTRIBUTE
203
- @indicator = 0
204
- end
204
+ return @prefix << BAR_COLOR if @max_value
205
+ @prefix << INDICATOR_ATTRIBUTE
206
+ @indicator = 0
205
207
  end
206
208
 
207
209
  def redraw
@@ -228,6 +230,7 @@ module NattyUI
228
230
  BAR_INK = Ansi[:bold, 255, :on_default].freeze
229
231
  ERASE = (Ansi.line_clear + Ansi.cursor_show).freeze
230
232
  end
233
+ private_constant :Progress
231
234
 
232
235
  PAGE_BEGIN =
233
236
  "#{Ansi.reset}#{Ansi.cursor_save_pos}#{Ansi.screen_save}" \
@@ -23,8 +23,8 @@ module NattyUI
23
23
  def _plain_width(str) = NattyUI.display_width(NattyUI.plain(str))
24
24
  def _blemish_width(str) = NattyUI.display_width(Ansi.blemish(str))
25
25
 
26
- def _element(type, *args)
27
- wrapper.class.const_get(type).__send__(:new, self).__send__(:_call, *args)
26
+ def _element(type, ...)
27
+ wrapper.class.const_get(type).__send__(:new, self).__send__(:_call, ...)
28
28
  end
29
29
 
30
30
  def _section(owner, type, args, **opts, &block)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NattyUI
4
4
  # @return [String] the version number of the gem
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
@@ -59,7 +59,7 @@ module NattyUI
59
59
  def read(yes, no)
60
60
  while true
61
61
  char = NattyUI.in_stream.getch
62
- return if "\u0003\u0004\e".include?(char)
62
+ return if "\3\4\e".include?(char)
63
63
  return true if yes.include?(char)
64
64
  return false if no.include?(char)
65
65
  end
@@ -49,7 +49,7 @@ module NattyUI
49
49
  @wrapper
50
50
  end
51
51
 
52
- def initialize(parent) = (@parent = parent)
52
+ def initialize(parent, **_) = (@parent = parent)
53
53
 
54
54
  def _close(state)
55
55
  return self if @status
@@ -49,9 +49,9 @@ module NattyUI
49
49
  redraw
50
50
  end
51
51
 
52
- # Maximal value.
52
+ # Maximum value.
53
53
  #
54
- # @return [Float] maximal value
54
+ # @return [Float] maximum value
55
55
  # @return [nil] when no max_value was configured
56
56
  attr_reader :max_value
57
57
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'element'
4
- require_relative '../mixins'
4
+ require_relative 'mixins'
5
5
 
6
6
  module NattyUI
7
7
  module Features
@@ -29,8 +29,8 @@ module NattyUI
29
29
 
30
30
  protected
31
31
 
32
- def initialize(parent, title:, max_value:, **_)
33
- super(parent)
32
+ def initialize(parent, title:, max_value:, **opts)
33
+ super(parent, **opts)
34
34
  @final_text = [title]
35
35
  @max_value = [0, max_value.to_f].max if max_value
36
36
  @value = 0
@@ -47,7 +47,7 @@ module NattyUI
47
47
  class Query < Element
48
48
  protected
49
49
 
50
- def _call(question, choices, kw_choices, result_typye)
50
+ def _call(question, choices, kw_choices, result_type)
51
51
  choices = grab(choices, kw_choices)
52
52
  return if choices.empty?
53
53
  wrapper.temporary do
@@ -58,17 +58,17 @@ module NattyUI
58
58
  title: question,
59
59
  symbol: :query
60
60
  )
61
- read(choices, result_typye)
61
+ read(choices, result_type)
62
62
  end
63
63
  end
64
64
 
65
- def read(choices, result_typye)
65
+ def read(choices, result_type)
66
66
  while true
67
67
  char = NattyUI.in_stream.getch
68
68
  return if "\3\4\e".include?(char)
69
69
  next unless choices.key?(char)
70
- return char if result_typye == :char
71
- return choices[char] if result_typye == :choice
70
+ return char if result_type == :char
71
+ return choices[char] if result_type == :choice
72
72
  return char, choices[char]
73
73
  end
74
74
  end
@@ -7,9 +7,12 @@ module NattyUI
7
7
  # Request user input.
8
8
  #
9
9
  # @param question [#to_s] Question to display
10
+ # @param password [Boolean] whether to hide the input
10
11
  # @return [String] the user input
11
12
  # @return [nil] when input was aborted with `^C` or `^D`
12
- def request(question) = _element(:Request, question)
13
+ def request(question, password: false)
14
+ _element(:Request, question, password)
15
+ end
13
16
  end
14
17
 
15
18
  class Wrapper
@@ -20,12 +23,20 @@ module NattyUI
20
23
  class Request < Element
21
24
  protected
22
25
 
23
- def _call(question)
26
+ def _call(question, password)
27
+ return read_password(question) if password
24
28
  NattyUI.readline(prompt(question), stream: wrapper.stream)
25
29
  ensure
26
30
  finish
27
31
  end
28
32
 
33
+ def read_password(question)
34
+ (wrapper.stream << prompt(question)).flush
35
+ NattyUI.in_stream.getpass
36
+ rescue Interrupt
37
+ nil
38
+ end
39
+
29
40
  def prompt(question) = "#{prefix}▶︎ #{question}: "
30
41
  end
31
42
  end
@@ -92,8 +92,8 @@ module NattyUI
92
92
 
93
93
  protected
94
94
 
95
- def initialize(parent, prefix: nil, suffix: nil, **_)
96
- super(parent)
95
+ def initialize(parent, prefix: nil, suffix: nil, **opts)
96
+ super(parent, **opts)
97
97
  @prefix = prefix
98
98
  @suffix = suffix
99
99
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'section'
4
- require_relative '../mixins'
4
+ require_relative 'mixins'
5
5
 
6
6
  module NattyUI
7
7
  module Features
@@ -30,16 +30,14 @@ module NattyUI
30
30
  end
31
31
 
32
32
  def finish
33
- unless failed?
34
- @status = :completed if @status == :closed
35
- @temp.call
36
- end
33
+ return @parent.failed(*@final_text) if failed?
34
+ @temp.call
37
35
  _section(
38
36
  @parent,
39
37
  :Message,
40
38
  @final_text,
41
39
  title: @final_text.shift,
42
- symbol: @status
40
+ symbol: @status = :completed
43
41
  )
44
42
  end
45
43
  end
@@ -77,9 +77,8 @@ module NattyUI
77
77
  # @return [Wrapper] itself
78
78
  def space(lines = 1)
79
79
  lines = [lines.to_i, 1].max
80
- @stream << ("\n" * lines)
81
80
  @lines_written += lines
82
- @stream.flush
81
+ (@stream << ("\n" * lines)).flush
83
82
  self
84
83
  end
85
84
 
@@ -139,11 +138,6 @@ module NattyUI
139
138
 
140
139
  protected
141
140
 
142
- def embellish(obj)
143
- obj = NattyUI.plain(obj)
144
- obj.empty? ? nil : obj
145
- end
146
-
147
141
  def temp_func
148
142
  lambda do
149
143
  @stream.flush
@@ -159,6 +153,8 @@ module NattyUI
159
153
  @ws = false
160
154
  end
161
155
 
156
+ def embellish(obj) = (obj = NattyUI.plain(obj)).empty? ? nil : obj
157
+
162
158
  def wrapper = self
163
159
  def prefix = nil
164
160
  alias suffix prefix
data/lib/natty-ui.rb CHANGED
@@ -25,7 +25,7 @@ module NattyUI
25
25
  class << self
26
26
  # @see .valid_in?
27
27
  # @return [IO] IO stream used to read input
28
- # @raise TypeError when a non-readable stream will be assigned
28
+ # @raise [TypeError] when a non-readable stream will be assigned
29
29
  attr_reader :in_stream
30
30
 
31
31
  # @param [IO] stream to read input
@@ -44,7 +44,7 @@ module NattyUI
44
44
  # @param [Boolean, :auto] ansi whether ANSI should be supported
45
45
  # or automatically selected
46
46
  # @return [Wrapper] wrapper for the given `stream`
47
- # @raise TypeError when `stream` is not a writable stream
47
+ # @raise [TypeError] when `stream` is not a writable stream
48
48
  def new(stream, ansi: :auto)
49
49
  unless valid_out?(stream)
50
50
  raise(TypeError, "writable IO required - #{stream.inspect}")
@@ -77,10 +77,9 @@ module NattyUI
77
77
  # Translate embedded attribute descriptions into ANSI control codes.
78
78
  #
79
79
  # @param [#to_s] str string to edit
80
- # @return ]String] edited string
80
+ # @return [String] edited string
81
81
  def embellish(str)
82
- str = str.to_s
83
- return +'' if str.empty?
82
+ return +'' if (str = str.to_s).empty?
84
83
  reset = false
85
84
  ret =
86
85
  str.gsub(/(\[\[((?~\]\]))\]\])/) do
@@ -105,11 +104,8 @@ module NattyUI
105
104
  .to_s
106
105
  .gsub(/(\[\[((?~\]\]))\]\])/) do
107
106
  match = Regexp.last_match[2]
108
- unless match.delete_prefix!('/')
109
- ansi = Ansi.try_convert(match)
110
- next ansi ? nil : "[[#{match}]]"
111
- end
112
- match.empty? ? nil : "[[#{match}]]"
107
+ next match.empty? ? nil : "[[#{match}]]" if match.delete_prefix!('/')
108
+ Ansi.try_convert(match) ? nil : "[[#{match}]]"
113
109
  end
114
110
  end
115
111
 
@@ -119,8 +115,7 @@ module NattyUI
119
115
  # @param [#to_s] str string to calculate
120
116
  # @return [Integer] the display size
121
117
  def display_width(str)
122
- str = str.to_s
123
- str.empty? ? 0 : Reline::Unicode.calculate_width(str)
118
+ (str = str.to_s).empty? ? 0 : Reline::Unicode.calculate_width(str)
124
119
  end
125
120
 
126
121
  # Convert given arguments into strings and yield each line.
@@ -137,12 +132,11 @@ module NattyUI
137
132
  # @return [Enumerator] line enumerator
138
133
  def each_line(*strs, max_width: nil, &block)
139
134
  return to_enum(__method__, *strs, max_width: max_width) unless block
140
- unless max_width
135
+ if max_width.nil?
141
136
  strs.each { |str| str.to_s.each_line(chomp: true, &block) }
142
137
  return nil
143
138
  end
144
- max_width = max_width.to_i
145
- return if max_width <= 0
139
+ return if (max_width = max_width.to_i) <= 0
146
140
  strs.each do |str|
147
141
  str
148
142
  .to_s
data/lib/natty_ui.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'natty-ui'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: natty-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-17 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  This is the beautiful, nice, nifty, fancy, neat, pretty, cool, lovely,
@@ -23,6 +23,7 @@ extra_rdoc_files:
23
23
  - README.md
24
24
  - LICENSE
25
25
  files:
26
+ - ".yardopts"
26
27
  - LICENSE
27
28
  - README.md
28
29
  - examples/attributes.rb
@@ -35,7 +36,6 @@ files:
35
36
  - lib/natty-ui/ansi.rb
36
37
  - lib/natty-ui/ansi_wrapper.rb
37
38
  - lib/natty-ui/features.rb
38
- - lib/natty-ui/mixins.rb
39
39
  - lib/natty-ui/version.rb
40
40
  - lib/natty-ui/wrapper.rb
41
41
  - lib/natty-ui/wrapper/ask.rb
@@ -44,11 +44,13 @@ files:
44
44
  - lib/natty-ui/wrapper/heading.rb
45
45
  - lib/natty-ui/wrapper/list_in_columns.rb
46
46
  - lib/natty-ui/wrapper/message.rb
47
+ - lib/natty-ui/wrapper/mixins.rb
47
48
  - lib/natty-ui/wrapper/progress.rb
48
49
  - lib/natty-ui/wrapper/query.rb
49
50
  - lib/natty-ui/wrapper/request.rb
50
51
  - lib/natty-ui/wrapper/section.rb
51
52
  - lib/natty-ui/wrapper/task.rb
53
+ - lib/natty_ui.rb
52
54
  homepage: https://github.com/mblumtritt/natty-ui
53
55
  licenses:
54
56
  - BSD-3-Clause