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