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
@@ -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