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
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'element'
|
4
|
+
|
5
|
+
module NattyUI
|
6
|
+
class Choice < Element
|
7
|
+
def select
|
8
|
+
yield(self) if block_given?
|
9
|
+
pin_line = NattyUI.lines_written
|
10
|
+
draw(current = 0)
|
11
|
+
while (key = Terminal.read_key)
|
12
|
+
case key
|
13
|
+
when 'Esc', 'Ctrl+c'
|
14
|
+
break nil if @abortable
|
15
|
+
when 'Enter', ' '
|
16
|
+
break @ret[current]
|
17
|
+
when 'Up', 'Left', 'Back', 'Shift+Tab'
|
18
|
+
current = @texts.size - 1 if (current -= 1) < 0
|
19
|
+
pin_line = NattyUI.back_to_line(pin_line, erase: false)
|
20
|
+
draw(current)
|
21
|
+
when 'Down', 'Right', 'Tab'
|
22
|
+
current = 0 if (current += 1) == @texts.size
|
23
|
+
pin_line = NattyUI.back_to_line(pin_line, erase: false)
|
24
|
+
draw(current)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
ensure
|
28
|
+
NattyUI.back_to_line(@start_line)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def initialize(parent, args, kwargs, abortable)
|
34
|
+
super(parent)
|
35
|
+
@start_line = NattyUI.lines_written
|
36
|
+
@texts = args + kwargs.values
|
37
|
+
@ret = Array.new(args.size, &:itself) + kwargs.keys
|
38
|
+
@abortable = abortable
|
39
|
+
theme = Theme.current
|
40
|
+
@mark = [theme.mark(:choice), theme.choice_style]
|
41
|
+
@mark_current = [theme.mark(:current_choice), theme.choice_current_style]
|
42
|
+
end
|
43
|
+
|
44
|
+
def draw(current)
|
45
|
+
@texts.each_with_index do |str, idx|
|
46
|
+
mark, decor = idx == current ? @mark_current : @mark
|
47
|
+
@parent.puts(
|
48
|
+
"#{decor}#{str}",
|
49
|
+
first_line_prefix: mark,
|
50
|
+
first_line_prefix_width: mark.width
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
private_constant :Choice
|
56
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'element'
|
4
|
+
|
5
|
+
module NattyUI
|
6
|
+
class DumbChoice < Element
|
7
|
+
def select
|
8
|
+
yield(self) if block_given?
|
9
|
+
draw
|
10
|
+
while (code = Terminal.read_key)
|
11
|
+
return if @abortable && (code == 'Esc' || code == 'Ctrl+c')
|
12
|
+
next if code.size > 1
|
13
|
+
code = code[0].upcase
|
14
|
+
if @ret.size <= 9 && ('1'..'9').include?(code)
|
15
|
+
code = @ret[code.ord - 49] and break code
|
16
|
+
elsif ('A'..'Z').include?(code)
|
17
|
+
code = @ret[code.ord - 65] and break code
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def draw
|
25
|
+
glyph = @ret.size <= 9 ? 1 : 'A'
|
26
|
+
@texts.each do |str|
|
27
|
+
@parent.puts(
|
28
|
+
str,
|
29
|
+
first_line_prefix: "[\\#{glyph}] ",
|
30
|
+
first_line_prefix_width: 4
|
31
|
+
)
|
32
|
+
glyph = glyph.succ
|
33
|
+
end
|
34
|
+
@texts = nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(parent, args, kwargs, abortable)
|
38
|
+
super(parent)
|
39
|
+
@ret = Array.new(args.size, &:itself) + kwargs.keys
|
40
|
+
@texts = args + kwargs.values
|
41
|
+
@abortable = abortable
|
42
|
+
end
|
43
|
+
end
|
44
|
+
private_constant :DumbChoice
|
45
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'features'
|
4
|
+
|
5
|
+
module NattyUI
|
6
|
+
# Base element class supporting all {Features}.
|
7
|
+
#
|
8
|
+
class Element
|
9
|
+
include Features
|
10
|
+
|
11
|
+
# @!visibility private
|
12
|
+
def columns = @parent.columns - @prefix_width - @suffix_width
|
13
|
+
|
14
|
+
# @!visibility private
|
15
|
+
def puts(*objects, **options)
|
16
|
+
if @prefix
|
17
|
+
options[:prefix] = "#{@prefix}#{options[:prefix]}"
|
18
|
+
options[:prefix_width] = options[:prefix_width].to_i + @prefix_width
|
19
|
+
if (first_line = options[:first_line_prefix])
|
20
|
+
options[:first_line_prefix] = "#{@prefix}#{first_line}"
|
21
|
+
if (flw = options[:first_line_prefix_width])
|
22
|
+
options[:first_line_prefix_width] = flw + @prefix_width
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
if @suffix
|
27
|
+
options[:suffix] = "#{options[:suffix]}#{@suffix}"
|
28
|
+
options[:suffix_width] = options[:suffix_width].to_i + @suffix_width
|
29
|
+
end
|
30
|
+
# options[:max_width] = @max_width if @max_width
|
31
|
+
@parent.puts(*objects, **options)
|
32
|
+
self
|
33
|
+
end
|
34
|
+
|
35
|
+
alias _to_s to_s
|
36
|
+
|
37
|
+
# @!visibility private
|
38
|
+
alias to_s inspect
|
39
|
+
private :_to_s
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def initialize(parent)
|
44
|
+
@parent = parent
|
45
|
+
@prefix_width = @suffix_width = 0
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module StateMixin
|
50
|
+
attr_reader :state
|
51
|
+
|
52
|
+
def closed? = @state ? true : false
|
53
|
+
def ok? = @state == :ok
|
54
|
+
def failed? = @state == :failed
|
55
|
+
|
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
|
65
|
+
end
|
66
|
+
|
67
|
+
# @!visibility private
|
68
|
+
def inspect = "#{_to_s.chop} state=#{@state.inspect}>"
|
69
|
+
|
70
|
+
protected
|
71
|
+
|
72
|
+
def finish_failed
|
73
|
+
@state = :failed
|
74
|
+
self
|
75
|
+
end
|
76
|
+
end
|
77
|
+
private_constant :StateMixin
|
78
|
+
end
|