natty-ui 0.12.1 → 0.26.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 (78) 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/key-codes.rb +28 -0
  13. data/examples/ls.rb +32 -16
  14. data/examples/named-colors.rb +23 -0
  15. data/examples/sections.rb +26 -0
  16. data/examples/tables.rb +62 -0
  17. data/examples/tasks.rb +52 -0
  18. data/lib/natty-ui/attributes.rb +604 -0
  19. data/lib/natty-ui/choice.rb +56 -0
  20. data/lib/natty-ui/dumb_choice.rb +45 -0
  21. data/lib/natty-ui/element.rb +75 -0
  22. data/lib/natty-ui/features.rb +798 -0
  23. data/lib/natty-ui/framed.rb +51 -0
  24. data/lib/natty-ui/ls_renderer.rb +97 -0
  25. data/lib/natty-ui/progress.rb +179 -0
  26. data/lib/natty-ui/section.rb +66 -0
  27. data/lib/natty-ui/table.rb +241 -0
  28. data/lib/natty-ui/table_renderer.rb +155 -0
  29. data/lib/natty-ui/task.rb +47 -0
  30. data/lib/natty-ui/temporary.rb +36 -0
  31. data/lib/natty-ui/theme.rb +303 -0
  32. data/lib/natty-ui/utils.rb +79 -0
  33. data/lib/natty-ui/version.rb +1 -1
  34. data/lib/natty-ui/width_finder.rb +125 -0
  35. data/lib/natty-ui.rb +96 -148
  36. metadata +45 -53
  37. data/examples/animate.rb +0 -42
  38. data/examples/attributes_list.rb +0 -12
  39. data/examples/demo.rb +0 -51
  40. data/examples/message.rb +0 -30
  41. data/examples/progress.rb +0 -66
  42. data/examples/query.rb +0 -39
  43. data/examples/read_key.rb +0 -13
  44. data/examples/table.rb +0 -39
  45. data/lib/natty-ui/animation/binary.rb +0 -36
  46. data/lib/natty-ui/animation/default.rb +0 -38
  47. data/lib/natty-ui/animation/matrix.rb +0 -51
  48. data/lib/natty-ui/animation/rainbow.rb +0 -28
  49. data/lib/natty-ui/animation/type_writer.rb +0 -44
  50. data/lib/natty-ui/animation.rb +0 -69
  51. data/lib/natty-ui/ansi/constants.rb +0 -75
  52. data/lib/natty-ui/ansi.rb +0 -530
  53. data/lib/natty-ui/ansi_wrapper.rb +0 -232
  54. data/lib/natty-ui/frame.rb +0 -53
  55. data/lib/natty-ui/glyph.rb +0 -64
  56. data/lib/natty-ui/key_map.rb +0 -142
  57. data/lib/natty-ui/preload.rb +0 -12
  58. data/lib/natty-ui/spinner.rb +0 -120
  59. data/lib/natty-ui/text/east_asian_width.rb +0 -2529
  60. data/lib/natty-ui/text.rb +0 -203
  61. data/lib/natty-ui/wrapper/animate.rb +0 -17
  62. data/lib/natty-ui/wrapper/ask.rb +0 -78
  63. data/lib/natty-ui/wrapper/element.rb +0 -79
  64. data/lib/natty-ui/wrapper/features.rb +0 -21
  65. data/lib/natty-ui/wrapper/framed.rb +0 -45
  66. data/lib/natty-ui/wrapper/heading.rb +0 -64
  67. data/lib/natty-ui/wrapper/horizontal_rule.rb +0 -37
  68. data/lib/natty-ui/wrapper/list_in_columns.rb +0 -138
  69. data/lib/natty-ui/wrapper/message.rb +0 -109
  70. data/lib/natty-ui/wrapper/mixins.rb +0 -75
  71. data/lib/natty-ui/wrapper/progress.rb +0 -63
  72. data/lib/natty-ui/wrapper/query.rb +0 -89
  73. data/lib/natty-ui/wrapper/quote.rb +0 -25
  74. data/lib/natty-ui/wrapper/request.rb +0 -54
  75. data/lib/natty-ui/wrapper/section.rb +0 -118
  76. data/lib/natty-ui/wrapper/table.rb +0 -550
  77. data/lib/natty-ui/wrapper/task.rb +0 -55
  78. data/lib/natty-ui/wrapper.rb +0 -239
@@ -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,75 @@
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
+ private :_to_s
37
+
38
+ # @!visibility private
39
+ alias to_s inspect
40
+
41
+ private
42
+
43
+ def initialize(parent)
44
+ @parent = parent
45
+ @prefix_width = @suffix_width = 0
46
+ end
47
+ end
48
+
49
+ module WithStatus
50
+ attr_reader :status
51
+
52
+ def active? = @status.nil?
53
+ def closed? = !active?
54
+ def ok? = @state == :ok
55
+ def failed? = @state == :failed
56
+
57
+ def ok(*text)
58
+ return self if @state
59
+ text = [@title] if text.empty? && @title
60
+ _done(text)
61
+ @state = :ok
62
+ self
63
+ end
64
+ alias done ok
65
+
66
+ def failed(*text, &block)
67
+ return self if @state
68
+ _failed
69
+ @state = :failed
70
+ text = [@title] if text.empty? && @title
71
+ @parent.failed(*text, &block)
72
+ self
73
+ end
74
+ end
75
+ end