natty-ui 0.12.1 → 0.25.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.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/README.md +22 -26
  4. data/examples/24bit-colors.rb +4 -7
  5. data/examples/3bit-colors.rb +28 -6
  6. data/examples/8bit-colors.rb +18 -21
  7. data/examples/attributes.rb +30 -22
  8. data/examples/cols.rb +40 -0
  9. data/examples/elements.rb +31 -0
  10. data/examples/examples.rb +45 -0
  11. data/examples/illustration.rb +56 -54
  12. data/examples/ls.rb +16 -16
  13. data/examples/named-colors.rb +23 -0
  14. data/examples/sections.rb +29 -0
  15. data/examples/tables.rb +62 -0
  16. data/examples/tasks.rb +52 -0
  17. data/lib/natty-ui/attributes.rb +604 -0
  18. data/lib/natty-ui/choice.rb +56 -0
  19. data/lib/natty-ui/dumb_choice.rb +45 -0
  20. data/lib/natty-ui/element.rb +78 -0
  21. data/lib/natty-ui/features.rb +798 -0
  22. data/lib/natty-ui/framed.rb +51 -0
  23. data/lib/natty-ui/ls_renderer.rb +93 -0
  24. data/lib/natty-ui/progress.rb +187 -0
  25. data/lib/natty-ui/section.rb +69 -0
  26. data/lib/natty-ui/table.rb +241 -0
  27. data/lib/natty-ui/table_renderer.rb +147 -0
  28. data/lib/natty-ui/task.rb +44 -0
  29. data/lib/natty-ui/temporary.rb +38 -0
  30. data/lib/natty-ui/theme.rb +303 -0
  31. data/lib/natty-ui/utils.rb +79 -0
  32. data/lib/natty-ui/version.rb +1 -1
  33. data/lib/natty-ui/width_finder.rb +125 -0
  34. data/lib/natty-ui.rb +89 -147
  35. metadata +44 -53
  36. data/examples/animate.rb +0 -42
  37. data/examples/attributes_list.rb +0 -12
  38. data/examples/demo.rb +0 -51
  39. data/examples/message.rb +0 -30
  40. data/examples/progress.rb +0 -66
  41. data/examples/query.rb +0 -39
  42. data/examples/read_key.rb +0 -13
  43. data/examples/table.rb +0 -39
  44. data/lib/natty-ui/animation/binary.rb +0 -36
  45. data/lib/natty-ui/animation/default.rb +0 -38
  46. data/lib/natty-ui/animation/matrix.rb +0 -51
  47. data/lib/natty-ui/animation/rainbow.rb +0 -28
  48. data/lib/natty-ui/animation/type_writer.rb +0 -44
  49. data/lib/natty-ui/animation.rb +0 -69
  50. data/lib/natty-ui/ansi/constants.rb +0 -75
  51. data/lib/natty-ui/ansi.rb +0 -530
  52. data/lib/natty-ui/ansi_wrapper.rb +0 -232
  53. data/lib/natty-ui/frame.rb +0 -53
  54. data/lib/natty-ui/glyph.rb +0 -64
  55. data/lib/natty-ui/key_map.rb +0 -142
  56. data/lib/natty-ui/preload.rb +0 -12
  57. data/lib/natty-ui/spinner.rb +0 -120
  58. data/lib/natty-ui/text/east_asian_width.rb +0 -2529
  59. data/lib/natty-ui/text.rb +0 -203
  60. data/lib/natty-ui/wrapper/animate.rb +0 -17
  61. data/lib/natty-ui/wrapper/ask.rb +0 -78
  62. data/lib/natty-ui/wrapper/element.rb +0 -79
  63. data/lib/natty-ui/wrapper/features.rb +0 -21
  64. data/lib/natty-ui/wrapper/framed.rb +0 -45
  65. data/lib/natty-ui/wrapper/heading.rb +0 -64
  66. data/lib/natty-ui/wrapper/horizontal_rule.rb +0 -37
  67. data/lib/natty-ui/wrapper/list_in_columns.rb +0 -138
  68. data/lib/natty-ui/wrapper/message.rb +0 -109
  69. data/lib/natty-ui/wrapper/mixins.rb +0 -75
  70. data/lib/natty-ui/wrapper/progress.rb +0 -63
  71. data/lib/natty-ui/wrapper/query.rb +0 -89
  72. data/lib/natty-ui/wrapper/quote.rb +0 -25
  73. data/lib/natty-ui/wrapper/request.rb +0 -54
  74. data/lib/natty-ui/wrapper/section.rb +0 -118
  75. data/lib/natty-ui/wrapper/table.rb +0 -550
  76. data/lib/natty-ui/wrapper/task.rb +0 -55
  77. data/lib/natty-ui/wrapper.rb +0 -239
@@ -1,239 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'io/console'
4
- require_relative 'ansi'
5
- require_relative 'wrapper/animate'
6
- require_relative 'wrapper/ask'
7
- require_relative 'wrapper/framed'
8
- require_relative 'wrapper/heading'
9
- require_relative 'wrapper/horizontal_rule'
10
- require_relative 'wrapper/list_in_columns'
11
- require_relative 'wrapper/message'
12
- require_relative 'wrapper/progress'
13
- require_relative 'wrapper/query'
14
- require_relative 'wrapper/quote'
15
- require_relative 'wrapper/request'
16
- require_relative 'wrapper/section'
17
- require_relative 'wrapper/table'
18
- require_relative 'wrapper/task'
19
-
20
- module NattyUI
21
- #
22
- # Helper class to wrap an output stream and implement all {Features}.
23
- #
24
- class Wrapper
25
- include Features
26
-
27
- # @return [IO] IO stream used for output
28
- attr_reader :stream
29
-
30
- # @attribute [r] ansi?
31
- # @return [Boolean] whether ANSI is supported
32
- def ansi? = false
33
-
34
- # Cursor visibility
35
- # @attribute [r] cursor
36
- def cursor = @cursor.zero?
37
-
38
- # @attribute [w] cursor
39
- def cursor=(value)
40
- # nop
41
- end
42
-
43
- # @attribute [r] screen_size
44
- # @return [[Integer, Integer]] screen size as rows and columns
45
- def screen_size = (@screen_size ||= determine_screen_size)
46
-
47
- # @attribute [r] screen_rows
48
- # @return [Integer] number of screen rows
49
- def screen_rows = screen_size[0]
50
-
51
- # @attribute [r] screen_columns
52
- # @return [Integer] number of screen columns
53
- def screen_columns = screen_size[1]
54
-
55
- # @attribute [r] wrapper
56
- # @return [Wrapper] self
57
- alias wrapper itself
58
-
59
- # @!group Tool functions
60
-
61
- # Print given arguments line-wise to the output stream.
62
- #
63
- # @param [#to_s] args objects to print
64
- # @option options [:left, :right, :center] :align text alignment
65
- # @return [Wrapper] itself
66
- def puts(*args, **options)
67
- pprint(args, options) do |line|
68
- @stream.puts(line)
69
- @lines_written += 1
70
- end
71
- @stream.flush
72
- self
73
- end
74
-
75
- # Print given arguments to the output stream.
76
- #
77
- # @param [#to_s] args objects to print
78
- # @option options [:left, :right, :center] :align text alignment
79
- # @return [Wrapper] itself
80
- def print(*args, **options)
81
- pprint(args, options) do |line|
82
- @stream.print(line)
83
- @lines_written += 1
84
- end
85
- @lines_written -= 1
86
- @stream.flush
87
- self
88
- end
89
-
90
- # Add at least one empty line
91
- #
92
- # @param [#to_i] lines count of lines
93
- # @return [Wrapper] itself
94
- def space(lines = 1)
95
- lines = [1, lines.to_i].max
96
- (@stream << ("\n" * lines)).flush
97
- @lines_written += lines
98
- self
99
- end
100
-
101
- # Clear Screen
102
- #
103
- # @return [Wrapper] itself
104
- def cls = self
105
-
106
- # @note The screen manipulation is only available in ANSI mode see {#ansi?}
107
- #
108
- # Saves current screen, deletes all screen content and moves the cursor
109
- # to the top left screen corner. It restores the screen after the block.
110
- #
111
- # @example
112
- # UI.page do |page|
113
- # page.info('This message will disappear in 5 seconds!')
114
- # sleep 5
115
- # end
116
- #
117
- # @yield [Wrapper] itself
118
- # @return [Object] block result
119
- def page
120
- block_given? ? yield(self) : self
121
- ensure
122
- @stream.flush
123
- end
124
-
125
- # @note The screen manipulation is only available in ANSI mode see {#ansi?}
126
- #
127
- # Resets the part of the screen written below the current output line when
128
- # the given block ended.
129
- #
130
- # @example
131
- # UI.temporary do |temp|
132
- # temp.info('This message will disappear in 5 seconds!')
133
- # sleep 5
134
- # end
135
- #
136
- # @overload temporary
137
- # @return [Proc] a function to reset the screen
138
- #
139
- # @overload temporary
140
- # @yield [Wrapper] itself
141
- # @return [Object] block result
142
- def temporary
143
- func = temp_func
144
- return func unless block_given?
145
- begin
146
- yield(self)
147
- ensure
148
- func.call
149
- end
150
- end
151
-
152
- # @!endgroup
153
-
154
- # @!visibility private
155
- attr_reader :lines_written
156
-
157
- # @!visibility private
158
- alias inspect to_s
159
-
160
- # @!visibility private
161
- alias available_width screen_columns
162
-
163
- # @!visibility private
164
- alias rcol screen_columns
165
-
166
- # @!visibility private
167
- def prefix = nil
168
-
169
- protected
170
-
171
- def pprint(strs, opts)
172
- prefix = opts[:prefix] and prefix = Text.plain(prefix)
173
- suffix = opts[:suffix] and suffix = Text.plain(suffix)
174
- return yield("#{prefix}#{suffix}") if strs.empty?
175
- max_width =
176
- opts.fetch(:max_width) do
177
- screen_columns - (opts[:prefix_width] || Text.width(prefix)) -
178
- (opts[:suffix_width] || Text.width(suffix))
179
- end
180
- case opts[:align]
181
- when :right
182
- Text.each_line_plain(strs, max_width) do |line, width|
183
- width = max_width - width
184
- yield("#{prefix}#{' ' * width}#{line}#{suffix}")
185
- end
186
- when :center
187
- Text.each_line_plain(strs, max_width) do |line, width|
188
- width = max_width - width
189
- right = width / 2
190
- yield(
191
- "#{prefix}#{' ' * (width - right)}#{line}#{' ' * right}#{suffix}"
192
- )
193
- end
194
- else
195
- Text.each_line_plain(strs, max_width) do |line|
196
- yield("#{prefix}#{line}#{suffix}")
197
- end
198
- end
199
- end
200
-
201
- def temp_func
202
- lambda do
203
- @stream.flush
204
- self
205
- end
206
- end
207
-
208
- def initialize(stream)
209
- @stream = stream
210
- @lines_written = @cursor = 0
211
- end
212
-
213
- private_class_method :new
214
-
215
- private
216
-
217
- def determine_screen_size
218
- return @stream.winsize if @ws
219
- if @ws.nil?
220
- ret = try_fetch_winsize
221
- if ret
222
- @ws = true
223
- Signal.trap('WINCH') { @screen_size = nil }
224
- return ret
225
- end
226
- @ws = false
227
- end
228
- [ENV['LINES'].to_i.nonzero? || 24, ENV['COLUMNS'].to_i.nonzero? || 80]
229
- end
230
-
231
- def try_fetch_winsize
232
- return unless @stream.respond_to?(:winsize)
233
- ret = @stream.winsize
234
- ret&.all?(&:positive?) ? ret : nil
235
- rescue SystemCallError
236
- nil
237
- end
238
- end
239
- end