gloo-lang 0.9.1 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (195) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/lib/VERSION +1 -1
  4. data/lib/dependencies.rb +2 -2
  5. data/lib/{gloo_lang.rb → gloo-lang.rb} +1 -5
  6. data/lib/gloo_lang/app/args.rb +112 -0
  7. data/lib/gloo_lang/app/engine.rb +230 -0
  8. data/lib/gloo_lang/app/help.rb +156 -0
  9. data/lib/gloo_lang/app/info.rb +34 -0
  10. data/lib/gloo_lang/app/log.rb +81 -0
  11. data/lib/gloo_lang/app/mode.rb +27 -0
  12. data/lib/gloo_lang/app/settings.rb +186 -0
  13. data/lib/gloo_lang/convert/converter.rb +35 -0
  14. data/lib/gloo_lang/convert/string_to_datetime.rb +21 -0
  15. data/lib/gloo_lang/convert/string_to_decimal.rb +20 -0
  16. data/lib/gloo_lang/convert/string_to_integer.rb +20 -0
  17. data/lib/gloo_lang/core/baseo.rb +30 -0
  18. data/lib/gloo_lang/core/dictionary.rb +181 -0
  19. data/lib/gloo_lang/core/error.rb +61 -0
  20. data/lib/gloo_lang/core/event_manager.rb +44 -0
  21. data/lib/gloo_lang/core/factory.rb +210 -0
  22. data/lib/gloo_lang/core/gloo_system.rb +266 -0
  23. data/lib/gloo_lang/core/heap.rb +52 -0
  24. data/lib/gloo_lang/core/here.rb +36 -0
  25. data/lib/gloo_lang/core/it.rb +36 -0
  26. data/lib/gloo_lang/core/literal.rb +30 -0
  27. data/lib/gloo_lang/core/obj.rb +303 -0
  28. data/lib/gloo_lang/core/obj_finder.rb +30 -0
  29. data/lib/gloo_lang/core/op.rb +40 -0
  30. data/lib/gloo_lang/core/parser.rb +59 -0
  31. data/lib/gloo_lang/core/pn.rb +188 -0
  32. data/lib/gloo_lang/core/tokens.rb +165 -0
  33. data/lib/gloo_lang/core/verb.rb +86 -0
  34. data/lib/gloo_lang/exec/action.rb +48 -0
  35. data/lib/gloo_lang/exec/dispatch.rb +40 -0
  36. data/lib/gloo_lang/exec/exec_env.rb +74 -0
  37. data/lib/gloo_lang/exec/runner.rb +45 -0
  38. data/lib/gloo_lang/exec/script.rb +49 -0
  39. data/lib/gloo_lang/exec/stack.rb +78 -0
  40. data/lib/gloo_lang/expr/expression.rb +118 -0
  41. data/lib/gloo_lang/expr/l_boolean.rb +36 -0
  42. data/lib/gloo_lang/expr/l_decimal.rb +39 -0
  43. data/lib/gloo_lang/expr/l_integer.rb +37 -0
  44. data/lib/gloo_lang/expr/l_string.rb +58 -0
  45. data/lib/gloo_lang/expr/op_div.rb +22 -0
  46. data/lib/gloo_lang/expr/op_minus.rb +22 -0
  47. data/lib/gloo_lang/expr/op_mult.rb +22 -0
  48. data/lib/gloo_lang/expr/op_plus.rb +24 -0
  49. data/lib/gloo_lang/help/app/application.txt +22 -0
  50. data/lib/gloo_lang/help/app/configuration.txt +7 -0
  51. data/lib/gloo_lang/help/app/default_help.txt +14 -0
  52. data/lib/gloo_lang/help/app/logging.txt +16 -0
  53. data/lib/gloo_lang/help/core/color.txt +31 -0
  54. data/lib/gloo_lang/help/core/error.txt +33 -0
  55. data/lib/gloo_lang/help/core/events.txt +21 -0
  56. data/lib/gloo_lang/help/core/gloo_system.txt +57 -0
  57. data/lib/gloo_lang/help/core/here.txt +30 -0
  58. data/lib/gloo_lang/help/core/it.txt +23 -0
  59. data/lib/gloo_lang/help/core/ops.txt +16 -0
  60. data/lib/gloo_lang/help/core/pathname.txt +29 -0
  61. data/lib/gloo_lang/help/objs/basic/alias.txt +36 -0
  62. data/lib/gloo_lang/help/objs/basic/boolean.txt +28 -0
  63. data/lib/gloo_lang/help/objs/basic/container.txt +33 -0
  64. data/lib/gloo_lang/help/objs/basic/decimal.txt +28 -0
  65. data/lib/gloo_lang/help/objs/basic/integer.txt +27 -0
  66. data/lib/gloo_lang/help/objs/basic/script.txt +29 -0
  67. data/lib/gloo_lang/help/objs/basic/string.txt +28 -0
  68. data/lib/gloo_lang/help/objs/basic/text.txt +27 -0
  69. data/lib/gloo_lang/help/objs/basic/untyped.txt +22 -0
  70. data/lib/gloo_lang/help/objs/cli/banner.txt +49 -0
  71. data/lib/gloo_lang/help/objs/cli/bar.txt +37 -0
  72. data/lib/gloo_lang/help/objs/cli/colorize.txt +33 -0
  73. data/lib/gloo_lang/help/objs/cli/confirm.txt +26 -0
  74. data/lib/gloo_lang/help/objs/cli/menu.txt +44 -0
  75. data/lib/gloo_lang/help/objs/cli/menu_item.txt +26 -0
  76. data/lib/gloo_lang/help/objs/cli/pastel.txt +43 -0
  77. data/lib/gloo_lang/help/objs/cli/prompt.txt +27 -0
  78. data/lib/gloo_lang/help/objs/cli/select.txt +34 -0
  79. data/lib/gloo_lang/help/objs/ctrl/each.txt +48 -0
  80. data/lib/gloo_lang/help/objs/ctrl/repeat.txt +38 -0
  81. data/lib/gloo_lang/help/objs/data/markdown.txt +25 -0
  82. data/lib/gloo_lang/help/objs/data/mysql.txt +40 -0
  83. data/lib/gloo_lang/help/objs/data/query.txt +37 -0
  84. data/lib/gloo_lang/help/objs/data/sqlite.txt +26 -0
  85. data/lib/gloo_lang/help/objs/data/table.txt +46 -0
  86. data/lib/gloo_lang/help/objs/dev/git_repo.txt +36 -0
  87. data/lib/gloo_lang/help/objs/dev/stats.txt +36 -0
  88. data/lib/gloo_lang/help/objs/dt/date.txt +23 -0
  89. data/lib/gloo_lang/help/objs/dt/datetime.txt +24 -0
  90. data/lib/gloo_lang/help/objs/dt/time.txt +23 -0
  91. data/lib/gloo_lang/help/objs/ror/erb.txt +37 -0
  92. data/lib/gloo_lang/help/objs/ror/eval.txt +24 -0
  93. data/lib/gloo_lang/help/objs/snd/play.txt +23 -0
  94. data/lib/gloo_lang/help/objs/snd/say.txt +28 -0
  95. data/lib/gloo_lang/help/objs/system/file.txt +48 -0
  96. data/lib/gloo_lang/help/objs/system/ssh_exec.txt +30 -0
  97. data/lib/gloo_lang/help/objs/system/system.txt +32 -0
  98. data/lib/gloo_lang/help/objs/web/http_get.txt +35 -0
  99. data/lib/gloo_lang/help/objs/web/http_post.txt +34 -0
  100. data/lib/gloo_lang/help/objs/web/json.txt +34 -0
  101. data/lib/gloo_lang/help/objs/web/slack.txt +33 -0
  102. data/lib/gloo_lang/help/objs/web/teams.txt +30 -0
  103. data/lib/gloo_lang/help/objs/web/uri.txt +38 -0
  104. data/lib/gloo_lang/help/verbs/alert.txt +33 -0
  105. data/lib/gloo_lang/help/verbs/beep.txt +25 -0
  106. data/lib/gloo_lang/help/verbs/cls.txt +24 -0
  107. data/lib/gloo_lang/help/verbs/context.txt +43 -0
  108. data/lib/gloo_lang/help/verbs/create.txt +33 -0
  109. data/lib/gloo_lang/help/verbs/execute.txt +27 -0
  110. data/lib/gloo_lang/help/verbs/help.txt +34 -0
  111. data/lib/gloo_lang/help/verbs/if.txt +37 -0
  112. data/lib/gloo_lang/help/verbs/list.txt +34 -0
  113. data/lib/gloo_lang/help/verbs/load.txt +38 -0
  114. data/lib/gloo_lang/help/verbs/move.txt +42 -0
  115. data/lib/gloo_lang/help/verbs/put.txt +38 -0
  116. data/lib/gloo_lang/help/verbs/quit.txt +25 -0
  117. data/lib/gloo_lang/help/verbs/run.txt +41 -0
  118. data/lib/gloo_lang/help/verbs/save.txt +26 -0
  119. data/lib/gloo_lang/help/verbs/show.txt +30 -0
  120. data/lib/gloo_lang/help/verbs/tell.txt +34 -0
  121. data/lib/gloo_lang/help/verbs/unless.txt +38 -0
  122. data/lib/gloo_lang/help/verbs/version.txt +32 -0
  123. data/lib/gloo_lang/help/verbs/wait.txt +29 -0
  124. data/lib/gloo_lang/objs/basic/alias.rb +78 -0
  125. data/lib/gloo_lang/objs/basic/boolean.rb +120 -0
  126. data/lib/gloo_lang/objs/basic/container.rb +76 -0
  127. data/lib/gloo_lang/objs/basic/decimal.rb +76 -0
  128. data/lib/gloo_lang/objs/basic/integer.rb +73 -0
  129. data/lib/gloo_lang/objs/basic/script.rb +99 -0
  130. data/lib/gloo_lang/objs/basic/string.rb +77 -0
  131. data/lib/gloo_lang/objs/basic/text.rb +87 -0
  132. data/lib/gloo_lang/objs/basic/untyped.rb +41 -0
  133. data/lib/gloo_lang/objs/cli/banner.rb +108 -0
  134. data/lib/gloo_lang/objs/cli/bar.rb +133 -0
  135. data/lib/gloo_lang/objs/cli/colorize.rb +73 -0
  136. data/lib/gloo_lang/objs/cli/confirm.rb +96 -0
  137. data/lib/gloo_lang/objs/cli/menu.rb +206 -0
  138. data/lib/gloo_lang/objs/cli/menu_item.rb +95 -0
  139. data/lib/gloo_lang/objs/cli/pastel.rb +97 -0
  140. data/lib/gloo_lang/objs/cli/prompt.rb +110 -0
  141. data/lib/gloo_lang/objs/cli/select.rb +126 -0
  142. data/lib/gloo_lang/objs/ctrl/each.rb +279 -0
  143. data/lib/gloo_lang/objs/ctrl/repeat.rb +108 -0
  144. data/lib/gloo_lang/objs/data/markdown.rb +84 -0
  145. data/lib/gloo_lang/objs/data/mysql.rb +192 -0
  146. data/lib/gloo_lang/objs/data/query.rb +176 -0
  147. data/lib/gloo_lang/objs/data/sqlite.rb +159 -0
  148. data/lib/gloo_lang/objs/data/table.rb +140 -0
  149. data/lib/gloo_lang/objs/dev/git.rb +140 -0
  150. data/lib/gloo_lang/objs/dev/stats.rb +120 -0
  151. data/lib/gloo_lang/objs/dt/date.rb +50 -0
  152. data/lib/gloo_lang/objs/dt/datetime.rb +62 -0
  153. data/lib/gloo_lang/objs/dt/time.rb +50 -0
  154. data/lib/gloo_lang/objs/ror/erb.rb +116 -0
  155. data/lib/gloo_lang/objs/ror/eval.rb +107 -0
  156. data/lib/gloo_lang/objs/snd/play.rb +48 -0
  157. data/lib/gloo_lang/objs/snd/say.rb +98 -0
  158. data/lib/gloo_lang/objs/system/file_handle.rb +138 -0
  159. data/lib/gloo_lang/objs/system/ssh_exec.rb +126 -0
  160. data/lib/gloo_lang/objs/system/system.rb +136 -0
  161. data/lib/gloo_lang/objs/web/http_get.rb +159 -0
  162. data/lib/gloo_lang/objs/web/http_post.rb +183 -0
  163. data/lib/gloo_lang/objs/web/json.rb +135 -0
  164. data/lib/gloo_lang/objs/web/slack.rb +130 -0
  165. data/lib/gloo_lang/objs/web/teams.rb +117 -0
  166. data/lib/gloo_lang/objs/web/uri.rb +148 -0
  167. data/lib/gloo_lang/persist/file_loader.rb +191 -0
  168. data/lib/gloo_lang/persist/file_saver.rb +49 -0
  169. data/lib/gloo_lang/persist/file_storage.rb +45 -0
  170. data/lib/gloo_lang/persist/line_splitter.rb +81 -0
  171. data/lib/gloo_lang/persist/persist_man.rb +120 -0
  172. data/lib/gloo_lang/utils/format.rb +21 -0
  173. data/lib/gloo_lang/utils/stats.rb +205 -0
  174. data/lib/gloo_lang/utils/words.rb +19 -0
  175. data/lib/gloo_lang/verbs/alert.rb +79 -0
  176. data/lib/gloo_lang/verbs/beep.rb +40 -0
  177. data/lib/gloo_lang/verbs/cls.rb +37 -0
  178. data/lib/gloo_lang/verbs/context.rb +62 -0
  179. data/lib/gloo_lang/verbs/create.rb +68 -0
  180. data/lib/gloo_lang/verbs/execute.rb +56 -0
  181. data/lib/gloo_lang/verbs/help.rb +264 -0
  182. data/lib/gloo_lang/verbs/if.rb +92 -0
  183. data/lib/gloo_lang/verbs/list.rb +98 -0
  184. data/lib/gloo_lang/verbs/load.rb +45 -0
  185. data/lib/gloo_lang/verbs/move.rb +89 -0
  186. data/lib/gloo_lang/verbs/put.rb +94 -0
  187. data/lib/gloo_lang/verbs/quit.rb +40 -0
  188. data/lib/gloo_lang/verbs/run.rb +75 -0
  189. data/lib/gloo_lang/verbs/save.rb +39 -0
  190. data/lib/gloo_lang/verbs/show.rb +64 -0
  191. data/lib/gloo_lang/verbs/tell.rb +79 -0
  192. data/lib/gloo_lang/verbs/unless.rb +92 -0
  193. data/lib/gloo_lang/verbs/version.rb +37 -0
  194. data/lib/gloo_lang/verbs/wait.rb +42 -0
  195. metadata +191 -3
@@ -0,0 +1,41 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # An Untyped Object.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class Untyped < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'untyped'.freeze
12
+ KEYWORD_SHORT = 'un'.freeze
13
+
14
+ #
15
+ # The name of the object type.
16
+ #
17
+ def self.typename
18
+ return KEYWORD
19
+ end
20
+
21
+ #
22
+ # The short name of the object type.
23
+ #
24
+ def self.short_typename
25
+ return KEYWORD_SHORT
26
+ end
27
+
28
+ # ---------------------------------------------------------------------
29
+ # Messages
30
+ # ---------------------------------------------------------------------
31
+
32
+ #
33
+ # Get a list of message names that this object receives.
34
+ #
35
+ def self.messages
36
+ return super # + [ "run" ]
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,108 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # Show a large-text banner.
5
+ #
6
+ require 'tty-font'
7
+ require 'pastel'
8
+
9
+ module GlooLang
10
+ module Objs
11
+ class Banner < GlooLang::Core::Obj
12
+
13
+ KEYWORD = 'banner'.freeze
14
+ KEYWORD_SHORT = 'ban'.freeze
15
+ TEXT = 'text'.freeze
16
+ STYLE = 'style'.freeze
17
+ COLOR = 'color'.freeze
18
+
19
+ #
20
+ # The name of the object type.
21
+ #
22
+ def self.typename
23
+ return KEYWORD
24
+ end
25
+
26
+ #
27
+ # The short name of the object type.
28
+ #
29
+ def self.short_typename
30
+ return KEYWORD_SHORT
31
+ end
32
+
33
+ #
34
+ # Get the banner text from the child object.
35
+ #
36
+ def text_value
37
+ o = find_child TEXT
38
+ return '' unless o
39
+
40
+ return o.value
41
+ end
42
+
43
+ #
44
+ # Get the banner style from the child object.
45
+ #
46
+ def style_value
47
+ o = find_child STYLE
48
+ return '' unless o
49
+
50
+ return o.value
51
+ end
52
+
53
+ #
54
+ # Get the banner color from the child object.
55
+ #
56
+ def color_value
57
+ o = find_child COLOR
58
+ return '' unless o
59
+
60
+ return o.value
61
+ end
62
+
63
+ # ---------------------------------------------------------------------
64
+ # Children
65
+ # ---------------------------------------------------------------------
66
+
67
+ # Does this object have children to add when an object
68
+ # is created in interactive mode?
69
+ # This does not apply during obj load, etc.
70
+ def add_children_on_create?
71
+ return true
72
+ end
73
+
74
+ # Add children to this object.
75
+ # This is used by containers to add children needed
76
+ # for default configurations.
77
+ def add_default_children
78
+ fac = $engine.factory
79
+ fac.create_string TEXT, '', self
80
+ fac.create_string STYLE, '', self
81
+ fac.create_string COLOR, '', self
82
+ end
83
+
84
+ # ---------------------------------------------------------------------
85
+ # Messages
86
+ # ---------------------------------------------------------------------
87
+
88
+ #
89
+ # Get a list of message names that this object receives.
90
+ #
91
+ def self.messages
92
+ return super + %w[show]
93
+ end
94
+
95
+ #
96
+ # Show the banner bar
97
+ #
98
+ def msg_show
99
+ font = TTY::Font.new self.style_value
100
+ t = font.write( self.text_value )
101
+ pastel = ::Pastel.new
102
+ c = self.color_value.split( ' ' ).map( &:to_sym )
103
+ puts pastel.decorate( t, *c )
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,133 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # Show a CLI progress bar.
5
+ #
6
+ require 'tty-progressbar'
7
+
8
+ module GlooLang
9
+ module Objs
10
+ class Bar < GlooLang::Core::Obj
11
+
12
+ KEYWORD = 'bar'.freeze
13
+ KEYWORD_SHORT = 'bar'.freeze
14
+ NAME = 'name'.freeze
15
+ TOTAL = 'total'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the bar's name from the child object.
33
+ #
34
+ def name_value
35
+ o = find_child NAME
36
+ return '' unless o
37
+
38
+ return o.value
39
+ end
40
+
41
+ #
42
+ # Get the bar's total from the child object.
43
+ #
44
+ def total_value
45
+ o = find_child TOTAL
46
+ return 100 unless o
47
+
48
+ return o.value
49
+ end
50
+
51
+ # ---------------------------------------------------------------------
52
+ # Children
53
+ # ---------------------------------------------------------------------
54
+
55
+ # Does this object have children to add when an object
56
+ # is created in interactive mode?
57
+ # This does not apply during obj load, etc.
58
+ def add_children_on_create?
59
+ return true
60
+ end
61
+
62
+ # Add children to this object.
63
+ # This is used by containers to add children needed
64
+ # for default configurations.
65
+ def add_default_children
66
+ fac = $engine.factory
67
+ fac.create_string NAME, '', self
68
+ fac.create_int TOTAL, 100, self
69
+ end
70
+
71
+ # ---------------------------------------------------------------------
72
+ # Messages
73
+ # ---------------------------------------------------------------------
74
+
75
+ #
76
+ # Get a list of message names that this object receives.
77
+ #
78
+ def self.messages
79
+ return super + %w[start advance stop run]
80
+ end
81
+
82
+ #
83
+ # Start the progress bar.
84
+ #
85
+ def msg_start
86
+ msg = "#{name_value} [:bar] :percent"
87
+ @bar = TTY::ProgressBar.new( msg, total: total_value )
88
+ end
89
+
90
+ #
91
+ # Finish the progress bar.
92
+ #
93
+ def msg_stop
94
+ @bar.finish
95
+ end
96
+
97
+ #
98
+ # Advance the progress bar.
99
+ #
100
+ def msg_advance
101
+ x = 1
102
+ if @params&.token_count&.positive?
103
+ expr = GlooLang::Expr::Expression.new( @params.tokens )
104
+ x = expr.evaluate.to_i
105
+ end
106
+
107
+ @bar.advance x
108
+ end
109
+
110
+ #
111
+ # Run for the given number of seconds advancing
112
+ # the bar to the end.
113
+ #
114
+ def msg_run
115
+ msg_start
116
+
117
+ x = 1
118
+ if @params&.token_count&.positive?
119
+ expr = GlooLang::Expr::Expression.new( @params.tokens )
120
+ x = expr.evaluate.to_i
121
+ end
122
+
123
+ 100.times do
124
+ sleep ( 0.0 + x ) / 100.0
125
+ @bar.advance 1
126
+ end
127
+
128
+ msg_stop
129
+ end
130
+
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,73 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # Show colorized output.
5
+ #
6
+ require 'colorized_string'
7
+
8
+ module GlooLang
9
+ module Objs
10
+ class Colorize < GlooLang::Core::Obj
11
+
12
+ KEYWORD = 'colorize'.freeze
13
+ KEYWORD_SHORT = 'color'.freeze
14
+
15
+ #
16
+ # The name of the object type.
17
+ #
18
+ def self.typename
19
+ return KEYWORD
20
+ end
21
+
22
+ #
23
+ # The short name of the object type.
24
+ #
25
+ def self.short_typename
26
+ return KEYWORD_SHORT
27
+ end
28
+
29
+ # ---------------------------------------------------------------------
30
+ # Children
31
+ # ---------------------------------------------------------------------
32
+
33
+ # Does this object have children to add when an object
34
+ # is created in interactive mode?
35
+ # This does not apply during obj load, etc.
36
+ def add_children_on_create?
37
+ return true
38
+ end
39
+
40
+ # Add children to this object.
41
+ # This is used by containers to add children needed
42
+ # for default configurations.
43
+ def add_default_children
44
+ fac = $engine.factory
45
+ fac.create_string 'white', '', self
46
+ end
47
+
48
+ # ---------------------------------------------------------------------
49
+ # Messages
50
+ # ---------------------------------------------------------------------
51
+
52
+ #
53
+ # Get a list of message names that this object receives.
54
+ #
55
+ def self.messages
56
+ return super + [ 'run' ]
57
+ end
58
+
59
+ #
60
+ # Run the colorize command.
61
+ #
62
+ def msg_run
63
+ msg = ''
64
+ children.each do |o|
65
+ msg += ColorizedString[ o.value_display ].colorize( o.name.to_sym )
66
+ end
67
+ $log.show msg
68
+ $engine.heap.it.set_to msg.to_s
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,96 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # Show a CLI confirmation prompt.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class Confirm < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'confirm'.freeze
12
+ KEYWORD_SHORT = 'confirm'.freeze
13
+ PROMPT = 'prompt'.freeze
14
+ DEFAULT_PROMPT = '> '.freeze
15
+ RESULT = 'result'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the URI from the child object.
33
+ # Returns nil if there is none.
34
+ #
35
+ def prompt_value
36
+ o = find_child PROMPT
37
+ return nil unless o
38
+
39
+ return o.value
40
+ end
41
+
42
+ #
43
+ # Set the result of the system call.
44
+ #
45
+ def set_result( data )
46
+ r = find_child RESULT
47
+ return nil unless r
48
+
49
+ r.set_value data
50
+ end
51
+
52
+ # ---------------------------------------------------------------------
53
+ # Children
54
+ # ---------------------------------------------------------------------
55
+
56
+ # Does this object have children to add when an object
57
+ # is created in interactive mode?
58
+ # This does not apply during obj load, etc.
59
+ def add_children_on_create?
60
+ return true
61
+ end
62
+
63
+ # Add children to this object.
64
+ # This is used by containers to add children needed
65
+ # for default configurations.
66
+ def add_default_children
67
+ fac = $engine.factory
68
+ fac.create_string PROMPT, DEFAULT_PROMPT, self
69
+ fac.create_bool RESULT, nil, self
70
+ end
71
+
72
+ # ---------------------------------------------------------------------
73
+ # Messages
74
+ # ---------------------------------------------------------------------
75
+
76
+ #
77
+ # Get a list of message names that this object receives.
78
+ #
79
+ def self.messages
80
+ return super + [ 'run' ]
81
+ end
82
+
83
+ #
84
+ # Run the confirmation command.
85
+ #
86
+ def msg_run
87
+ prompt = prompt_value
88
+ return unless prompt
89
+
90
+ result = $prompt.yes?( prompt )
91
+ set_result result
92
+ end
93
+
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,206 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A CLI menu.
5
+ # The menu contains a collection of menu items, a prompt
6
+ # and an option to loop until done.
7
+ #
8
+
9
+ module GlooLang
10
+ module Objs
11
+ class Menu < GlooLang::Core::Obj
12
+
13
+ KEYWORD = 'menu'.freeze
14
+ KEYWORD_SHORT = 'menu'.freeze
15
+ PROMPT = 'prompt'.freeze
16
+ ITEMS = 'items'.freeze
17
+ LOOP = 'loop'.freeze
18
+ HIDE_ITEMS = 'hide_items'.freeze
19
+ BEFORE_MENU = 'before_menu'.freeze
20
+ DEFAULT = 'default'.freeze
21
+
22
+ #
23
+ # The name of the object type.
24
+ #
25
+ def self.typename
26
+ return KEYWORD
27
+ end
28
+
29
+ #
30
+ # The short name of the object type.
31
+ #
32
+ def self.short_typename
33
+ return KEYWORD_SHORT
34
+ end
35
+
36
+ #
37
+ # Get the value of the prompt child object.
38
+ # Returns nil if there is none.
39
+ #
40
+ def prompt_value
41
+ o = find_child PROMPT
42
+ return '' unless o
43
+
44
+ return o.value
45
+ end
46
+
47
+ #
48
+ # Get the value of the loop child object.
49
+ # Should we keep looping or should we stop?
50
+ #
51
+ def loop?
52
+ o = find_child LOOP
53
+ return false unless o
54
+
55
+ return o.value
56
+ end
57
+
58
+ # ---------------------------------------------------------------------
59
+ # Children
60
+ # ---------------------------------------------------------------------
61
+
62
+ #
63
+ # Does this object have children to add when an object
64
+ # is created in interactive mode?
65
+ # This does not apply during obj load, etc.
66
+ #
67
+ def add_children_on_create?
68
+ return true
69
+ end
70
+
71
+ #
72
+ # Add children to this object.
73
+ # This is used by containers to add children needed
74
+ # for default configurations.
75
+ #
76
+ def add_default_children
77
+ fac = $engine.factory
78
+ fac.create_string PROMPT, '> ', self
79
+ fac.create_can ITEMS, self
80
+ fac.create_bool LOOP, true, self
81
+ fac.create_script DEFAULT, '', self
82
+ end
83
+
84
+ # ---------------------------------------------------------------------
85
+ # Messages
86
+ # ---------------------------------------------------------------------
87
+
88
+ #
89
+ # Get a list of message names that this object receives.
90
+ #
91
+ def self.messages
92
+ return super + [ 'run' ]
93
+ end
94
+
95
+ #
96
+ # Show the menu options, and prompt for user input.
97
+ #
98
+ def msg_run
99
+ loop do
100
+ begin_menu
101
+ if prompt_value.empty?
102
+ dt = DateTime.now
103
+ d = dt.strftime( '%Y.%m.%d' )
104
+ t = dt.strftime( '%I:%M:%S' )
105
+ cmd = $prompt.ask( "#{d.yellow} #{t.white} >" )
106
+ else
107
+ cmd = $prompt.ask( prompt_value )
108
+ end
109
+ cmd ? run_command( cmd ) : run_default
110
+ break unless loop?
111
+ end
112
+ end
113
+
114
+ # ---------------------------------------------------------------------
115
+ # Menu actions
116
+ # ---------------------------------------------------------------------
117
+
118
+ #
119
+ # Begin the menu execution.
120
+ # Run the before menu script if there is one,
121
+ # then show options unless we are hiding them by default.
122
+ #
123
+ def begin_menu
124
+ run_before_menu
125
+
126
+ # Check to see if we should show items at all.
127
+ o = find_child HIDE_ITEMS
128
+ return if o && o.value == true
129
+
130
+ show_options
131
+ end
132
+
133
+ #
134
+ # If there is a before menu script, run it now.
135
+ #
136
+ def run_before_menu
137
+ o = find_child BEFORE_MENU
138
+ return unless o
139
+
140
+ GlooLang::Exec::Dispatch.message 'run', o
141
+ end
142
+
143
+ #
144
+ # Show the list of menu options.
145
+ #
146
+ def show_options
147
+ o = find_child ITEMS
148
+ return unless o
149
+
150
+ o.children.each do |mitem|
151
+ mitem = GlooLang::Objs::Alias.resolve_alias( mitem )
152
+ puts " #{mitem.shortcut_value} - #{mitem.description_value}"
153
+ end
154
+ end
155
+
156
+ #
157
+ # Find the command matching user input.
158
+ #
159
+ def find_cmd( cmd )
160
+ o = find_child ITEMS
161
+ return nil unless o
162
+
163
+ o.children.each do |mitem|
164
+ mitem = GlooLang::Objs::Alias.resolve_alias( mitem )
165
+ return mitem if mitem.shortcut_value.downcase == cmd.downcase
166
+ end
167
+
168
+ return nil
169
+ end
170
+
171
+ #
172
+ # Run the default option.
173
+ #
174
+ def run_default
175
+ obj = find_child DEFAULT
176
+ return unless obj
177
+
178
+ s = GlooLang::Exec::Script.new obj
179
+ s.run
180
+ end
181
+
182
+ #
183
+ # Run the selected command.
184
+ #
185
+ def run_command( cmd )
186
+ obj = find_cmd cmd
187
+
188
+ unless obj
189
+ if cmd == '?'
190
+ show_options
191
+ else
192
+ puts "#{cmd} is not a valid option"
193
+ end
194
+ return
195
+ end
196
+
197
+ script = obj.do_script
198
+ return unless script
199
+
200
+ s = GlooLang::Exec::Script.new script
201
+ s.run
202
+ end
203
+
204
+ end
205
+ end
206
+ end