natty-ui 0.5.2 → 0.5.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: b9531c1f73a1d9ca8f9a09f279eb3939f8ed0f37bc940d5f50aa479edc1b38ca
4
- data.tar.gz: d380394514fb92fe411cfbfcf1c7ba6bd95c9388ecfcdb31752ea47df8efeb9b
3
+ metadata.gz: 6a717eec681b358c4979684881f16d5217dea7ebacca3baf46f84edfc318d39b
4
+ data.tar.gz: 2829903b6bf15aa81d33259bf77633d6ed6b9e5f88a598bc858b881be94da4cc
5
5
  SHA512:
6
- metadata.gz: '086c5832e6c811a5fab7f2e4866018d4cd6cc898af2866283e19b71330f4d6d05a5f17089bf5ac383cb26e5006bce66ca37ba63baef4e9ff7444bb1e44ca0cc7'
7
- data.tar.gz: 4d13067a100b10d09565fa2cf95bdd39d40c5c31e25fa5971d666e272d2e7ebbfd73b7188f12e6bc41bfc168b1a8864da6705636f70204185b8daf34ef614e00
6
+ metadata.gz: 274f008deb82e73ba4a6c0b93d2a4e4cea3afdd60284d8fdc8e462e56cf04f6e5bda8a25af1c1707cd5a8c92612d48f445f5d127fb5c6531d183b3b8ac424279
7
+ data.tar.gz: 770c07b0085ac1efd565b141fae6f704899a89b7e8b648fd27e687166a13676edd3a38039d8f4aeae7c490849cf39a5d2d22daed25d6a1c0b9540340b21483f4
data/README.md CHANGED
@@ -44,7 +44,7 @@ or use progression displays like progress bars.
44
44
 
45
45
  🚀 There are much more [features](https://rubydoc.info/gems/natty-ui/NattyUI/Features)!
46
46
 
47
- 📕 See the [online help](https://rubydoc.info/gems/natty-ui/NattyUI) for more details or have a look at the [examples](./examples/) directory to get an impression of the current feature set (see also XXX).
47
+ 📕 See the [online help](https://rubydoc.info/gems/natty-ui/NattyUI) for more details or have a look at the [examples](./examples/) directory to get an impression of the current feature set.
48
48
 
49
49
  ![illustration](https://raw.githubusercontent.com/mblumtritt/natty-ui/main/examples/illustration.svg)
50
50
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  module NattyUI
4
4
  # @return [String] the version number of the gem
5
- VERSION = '0.5.2'
5
+ VERSION = '0.5.3'
6
6
  end
@@ -22,6 +22,8 @@ module NattyUI
22
22
  # sec.failed('You should have an opinion!')
23
23
  # end
24
24
  #
25
+ # @see NattyUI.in_stream
26
+ #
25
27
  # @param question [#to_s] Question to display
26
28
  # @param yes [#to_s] chars which will be used to answer 'Yes'
27
29
  # @param no [#to_s] chars which will be used to answer 'No'
@@ -24,8 +24,7 @@ module NattyUI
24
24
 
25
25
  # Close the element.
26
26
  #
27
- # @return [Element] itself when used without a code block
28
- # @return [nil] when used with a code block
27
+ # @return [Element] itself
29
28
  def close = _close(:closed)
30
29
 
31
30
  alias _to_s to_s
@@ -14,7 +14,7 @@ module NattyUI
14
14
  # @param [Array<#to_s>] args more objects to print
15
15
  # @param [Symbol] type frame type;
16
16
  # valid types are `:rounded`, `:simple`, `:heavy`, `:semi`, `:double`
17
- # @yieldparam [Wrapper::Framed] section the created section
17
+ # @yieldparam [Wrapper::Framed] framed the created section
18
18
  # @return [Object] the result of the code block
19
19
  # @return [Wrapper::Framed] itself, when no code block is given
20
20
  def framed(title, *args, type: :rounded, &block)
@@ -7,7 +7,7 @@ module NattyUI
7
7
  # Creates section with a H1 title.
8
8
  #
9
9
  # @param (see #information)
10
- # @yieldparam [Wrapper::Heading] section the created section
10
+ # @yieldparam [Wrapper::Heading] heading the created section
11
11
  # @return [Object] the result of the code block
12
12
  # @return [Wrapper::Heading] itself, when no code block is given
13
13
  def h1(title, *args, &block)
@@ -10,7 +10,7 @@ module NattyUI
10
10
  # @param [#to_s] title object to print as section title
11
11
  # @param [Array<#to_s>] args more objects to print
12
12
  # @param [#to_s] symbol symbol/prefix used for the title
13
- # @yieldparam [Wrapper::Message] section the created section
13
+ # @yieldparam [Wrapper::Message] message the created section
14
14
  # @return [Object] the result of the code block
15
15
  # @return [Wrapper::Message] itself, when no code block is given
16
16
  def message(title, *args, symbol: :default, &block)
@@ -24,6 +24,8 @@ module NattyUI
24
24
  # )
25
25
  # # => 'a' or 'b' or 'c' or nil if user aborted
26
26
  #
27
+ # @see NattyUI.in_stream
28
+ #
27
29
  # @param question [#to_s] Question to display
28
30
  # @param choices [#to_s] choices selectable via index (0..9)
29
31
  # @param result [Symbol] defines how the result ist returned
@@ -29,7 +29,7 @@ module NattyUI
29
29
 
30
30
  class Wrapper
31
31
  #
32
- # Visual element to keep text lines together.
32
+ # Visual {Element} to keep text lines together.
33
33
  #
34
34
  # A section can contain other elements and sections.
35
35
  #
@@ -38,21 +38,23 @@ module NattyUI
38
38
  class Section < Element
39
39
  # Close the section.
40
40
  #
41
- # @return [Section] itself when used without a code block
42
- # @return [nil] when used with a code block
41
+ # @return [Section] itself
43
42
  def close = _close(:closed)
44
43
 
45
44
  # Print given arguments as lines into the section.
45
+ # Optionally limit the line width to given `max_width`.
46
46
  #
47
- # @overload puts(...)
47
+ # @overload puts(..., max_width: nil)
48
48
  # @param [#to_s] ... objects to print
49
+ # @param [Integer, nil] max_width maximum line width
49
50
  # @comment @param [#to_s, nil] prefix line prefix
50
51
  # @comment @param [#to_s, nil] suffix line suffix
51
52
  # @return [Section] itself
52
- def puts(*args, prefix: nil, suffix: nil)
53
+ def puts(*args, max_width: nil, prefix: nil, suffix: nil)
53
54
  return self if @status
54
55
  @parent.puts(
55
56
  *args,
57
+ max_width: max_width,
56
58
  prefix: prefix ? "#{@prefix}#{prefix}" : @prefix,
57
59
  suffix: suffix ? "#{@suffix}#{suffix}" : @suffix
58
60
  )
@@ -11,7 +11,7 @@ module NattyUI
11
11
  # or {#failed}.
12
12
  #
13
13
  # @param (see #information)
14
- # @yieldparam [Wrapper::Task] section the created section
14
+ # @yieldparam [Wrapper::Task] task the created section
15
15
  # @return [Object] the result of the code block
16
16
  # @return [Wrapper::Task] itself, when no code block is given
17
17
  def task(title, *args, &block)
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'stringio'
4
3
  require_relative 'wrapper/ask'
5
4
  require_relative 'wrapper/framed'
6
5
  require_relative 'wrapper/heading'
@@ -28,13 +27,13 @@ module NattyUI
28
27
  # @return [[Integer, Integer]] screen size as rows and columns
29
28
  def screen_size
30
29
  return @stream.winsize if @ws
31
- [ENV['LINES'].to_i.nonzero? || 25, ENV['COLUMNS'].to_i.nonzero? || 80]
30
+ [ENV['LINES'].to_i.nonzero? || 24, ENV['COLUMNS'].to_i.nonzero? || 80]
32
31
  end
33
32
 
34
33
  # @attribute [r] screen_rows
35
34
  # @return [Integer] number of screen rows
36
35
  def screen_rows
37
- @ws ? @stream.winsize[0] : (ENV['LINES'].to_i.nonzero? || 25)
36
+ @ws ? @stream.winsize[0] : (ENV['LINES'].to_i.nonzero? || 24)
38
37
  end
39
38
 
40
39
  # @attribute [r] screen_columns
@@ -46,24 +45,22 @@ module NattyUI
46
45
  # @!group Tool functions
47
46
 
48
47
  # Print given arguments as lines to the output stream.
48
+ # Optionally limit the line width to given `max_width`.
49
49
  #
50
- # @overload puts(...)
50
+ # @overload puts(..., max_width: nil)
51
51
  # @param [#to_s] ... objects to print
52
+ # @param [Integer, nil] max_width maximum line width
52
53
  # @comment @param [#to_s, nil] prefix line prefix
53
54
  # @comment @param [#to_s, nil] suffix line suffix
54
55
  # @return [Wrapper] itself
55
- def puts(*args, prefix: nil, suffix: nil)
56
+ def puts(*args, max_width: nil, prefix: nil, suffix: nil)
56
57
  if args.empty?
57
58
  @stream.puts(embellish("#{prefix}#{suffix}"))
58
59
  @lines_written += 1
59
60
  else
60
- StringIO.open do |io|
61
- io.puts(*args)
62
- io.rewind
63
- io.each(chomp: true) do |line|
64
- @stream.puts(embellish("#{prefix}#{line}#{suffix}"))
65
- @lines_written += 1
66
- end
61
+ NattyUI.each_line(*args, max_width: max_width) do |line|
62
+ @stream.puts(embellish("#{prefix}#{line}#{suffix}"))
63
+ @lines_written += 1
67
64
  end
68
65
  end
69
66
  @stream.flush
@@ -154,7 +151,7 @@ module NattyUI
154
151
  def initialize(stream)
155
152
  @stream = stream
156
153
  @lines_written = 0
157
- @ws = stream.respond_to?(:winsize) && stream.winsize&.size == 2
154
+ @ws = stream.respond_to?(:winsize) && stream.winsize&.all?(&:positive?)
158
155
  rescue Errno::ENOTTY
159
156
  @ws = false
160
157
  end
data/lib/natty-ui.rb CHANGED
@@ -1,6 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'reline'
3
+ require 'readline'
4
+ unless defined?(Reline)
5
+ # only load the Reline::Unicode part
6
+ # @!visibility private
7
+ module Reline
8
+ def self.ambiguous_width = 1
9
+ end
10
+ require 'reline/unicode'
11
+ end
4
12
  require_relative 'natty-ui/wrapper'
5
13
  require_relative 'natty-ui/ansi_wrapper'
6
14
 
@@ -114,6 +122,55 @@ module NattyUI
114
122
  str.empty? ? 0 : Reline::Unicode.calculate_width(str)
115
123
  end
116
124
 
125
+ # Convert given arguments into strings and yield each line.
126
+ # Optionally limit the line width to given `max_width`.
127
+ #
128
+ # @overload each_line(..., max_width: nil)
129
+ # @param [#to_s] ... objects to print
130
+ # @param [#to_i, nil] max_width maximum line width
131
+ # @yieldparam [String] line string line
132
+ # @return [nil]
133
+ # @overload each_line(..., max_width: nil)
134
+ # @param [#to_s] ... objects to print
135
+ # @param [#to_i, nil] max_width maximum line width
136
+ # @return [Enumerator] line enumerator
137
+ def each_line(*strs, max_width: nil, &block)
138
+ return to_enum(__method__, *strs, max_width: max_width) unless block
139
+ unless max_width
140
+ strs.each { |str| str.to_s.each_line(chomp: true, &block) }
141
+ return nil
142
+ end
143
+ max_width = max_width.to_i
144
+ return if max_width <= 0
145
+ strs.each do |str|
146
+ str
147
+ .to_s
148
+ .each_line(chomp: true) do |line|
149
+ Reline::Unicode.split_by_width(line, max_width)[0].each do |part|
150
+ yield(part) if part
151
+ end
152
+ end
153
+ end
154
+ nil
155
+ end
156
+
157
+ # Read user input line from {.in_stream}.
158
+ #
159
+ # @see .valid_out?
160
+ #
161
+ # @param [#to_s] prompt input prompt
162
+ # @param [IO] stream writeable IO used to display output
163
+ # @return [String] user input line
164
+ # @return [nil] when user interrputed input with `^C` or `^D`
165
+ def readline(prompt = nil, stream: StdOut.stream)
166
+ Readline.output = stream
167
+ Readline.input = @in_stream
168
+ Readline.readline(prompt.to_s)
169
+ rescue Interrupt
170
+ stream.puts
171
+ nil
172
+ end
173
+
117
174
  private
118
175
 
119
176
  def wrapper_class(stream, ansi)
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.5.2
4
+ version: 0.5.3
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-10 00:00:00.000000000 Z
11
+ date: 2023-11-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  This is the beautiful, nice, nifty, fancy, neat, pretty, cool, lovely,