gloo 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/.rubocop.yml +1 -1
  4. data/Gemfile.lock +80 -81
  5. data/Rakefile +1 -0
  6. data/gloo.gemspec +2 -1
  7. data/lib/gloo/app/engine.rb +48 -3
  8. data/lib/gloo/app/info.rb +1 -1
  9. data/lib/gloo/app/settings.rb +12 -5
  10. data/lib/gloo/convert/string_to_datetime.rb +21 -0
  11. data/lib/gloo/convert/string_to_decimal.rb +20 -0
  12. data/lib/gloo/convert/string_to_integer.rb +20 -0
  13. data/lib/gloo/core/event_manager.rb +2 -6
  14. data/lib/gloo/core/factory.rb +58 -1
  15. data/lib/gloo/core/gloo_system.rb +72 -0
  16. data/lib/gloo/core/obj.rb +50 -1
  17. data/lib/gloo/core/parser.rb +3 -1
  18. data/lib/gloo/core/pn.rb +3 -2
  19. data/lib/gloo/exec/dispatch.rb +30 -0
  20. data/lib/gloo/exec/runner.rb +43 -0
  21. data/lib/gloo/expr/expression.rb +1 -0
  22. data/lib/gloo/expr/l_decimal.rb +34 -0
  23. data/lib/gloo/expr/op_div.rb +2 -0
  24. data/lib/gloo/expr/op_minus.rb +2 -0
  25. data/lib/gloo/expr/op_mult.rb +2 -0
  26. data/lib/gloo/expr/op_plus.rb +2 -0
  27. data/lib/gloo/objs/basic/alias.rb +111 -0
  28. data/lib/gloo/objs/basic/container.rb +11 -1
  29. data/lib/gloo/objs/basic/decimal.rb +96 -0
  30. data/lib/gloo/objs/basic/integer.rb +5 -0
  31. data/lib/gloo/objs/basic/string.rb +9 -1
  32. data/lib/gloo/objs/basic/text.rb +27 -2
  33. data/lib/gloo/objs/cli/banner.rb +137 -0
  34. data/lib/gloo/objs/cli/bar.rb +141 -0
  35. data/lib/gloo/objs/cli/colorize.rb +1 -1
  36. data/lib/gloo/objs/cli/menu.rb +236 -0
  37. data/lib/gloo/objs/cli/menu_item.rb +128 -0
  38. data/lib/gloo/objs/cli/pastel.rb +120 -0
  39. data/lib/gloo/objs/cli/prompt.rb +19 -11
  40. data/lib/gloo/objs/cli/select.rb +153 -0
  41. data/lib/gloo/objs/ctrl/each.rb +45 -16
  42. data/lib/gloo/objs/ctrl/repeat.rb +129 -0
  43. data/lib/gloo/objs/data/markdown.rb +109 -0
  44. data/lib/gloo/objs/data/table.rb +168 -0
  45. data/lib/gloo/objs/dt/date.rb +72 -0
  46. data/lib/gloo/objs/dt/datetime.rb +84 -0
  47. data/lib/gloo/objs/dt/time.rb +72 -0
  48. data/lib/gloo/objs/ror/erb.rb +1 -0
  49. data/lib/gloo/objs/system/file_handle.rb +50 -1
  50. data/lib/gloo/objs/web/http_get.rb +24 -4
  51. data/lib/gloo/objs/web/http_post.rb +1 -0
  52. data/lib/gloo/objs/web/json.rb +155 -0
  53. data/lib/gloo/objs/web/uri.rb +160 -0
  54. data/lib/gloo/persist/file_loader.rb +17 -6
  55. data/lib/gloo/persist/line_splitter.rb +7 -2
  56. data/lib/gloo/persist/persist_man.rb +37 -13
  57. data/lib/gloo/verbs/cls.rb +67 -0
  58. data/lib/gloo/verbs/help.rb +9 -0
  59. data/lib/gloo/verbs/if.rb +1 -0
  60. data/lib/gloo/verbs/load.rb +3 -2
  61. data/lib/gloo/verbs/move.rb +128 -0
  62. data/lib/gloo/verbs/run.rb +21 -7
  63. data/lib/gloo/verbs/tell.rb +1 -1
  64. data/lib/gloo/verbs/unless.rb +1 -0
  65. data/lib/gloo/verbs/wait.rb +73 -0
  66. metadata +36 -5
  67. data/lib/gloo/core/runner.rb +0 -26
@@ -0,0 +1,141 @@
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 Gloo
9
+ module Objs
10
+ class Bar < Gloo::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]
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 = Gloo::Expr::Expression.new( @params.tokens )
104
+ x = expr.evaluate.to_i
105
+ end
106
+
107
+ @bar.advance x
108
+ end
109
+
110
+ # ---------------------------------------------------------------------
111
+ # Help
112
+ # ---------------------------------------------------------------------
113
+
114
+ #
115
+ # Get help for this object type.
116
+ #
117
+ def self.help
118
+ return <<~TEXT
119
+ BAR OBJECT TYPE
120
+ NAME: bar
121
+ SHORTCUT: bar
122
+
123
+ DESCRIPTION
124
+ CLI progress bar
125
+
126
+ CHILDREN
127
+ name - string
128
+ The name of the progress bar.
129
+ total - integer - 100
130
+ The total for the bar.
131
+
132
+ MESSAGES
133
+ start - Start the bar.
134
+ advance <amount> - Advance by the given amount.
135
+ stop - Complete the bar.
136
+ TEXT
137
+ end
138
+
139
+ end
140
+ end
141
+ end
@@ -1,5 +1,5 @@
1
1
  # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
- # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
3
  #
4
4
  # Show colorized output.
5
5
  #
@@ -0,0 +1,236 @@
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 Gloo
10
+ module Objs
11
+ class Menu < Gloo::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
+ # Does this object have children to add when an object
63
+ # is created in interactive mode?
64
+ # This does not apply during obj load, etc.
65
+ def add_children_on_create?
66
+ return true
67
+ end
68
+
69
+ # Add children to this object.
70
+ # This is used by containers to add children needed
71
+ # for default configurations.
72
+ def add_default_children
73
+ fac = $engine.factory
74
+ fac.create_string PROMPT, '> ', self
75
+ fac.create_can ITEMS, self
76
+ fac.create_bool LOOP, true, self
77
+ fac.create_script DEFAULT, '', self
78
+ end
79
+
80
+ # ---------------------------------------------------------------------
81
+ # Messages
82
+ # ---------------------------------------------------------------------
83
+
84
+ #
85
+ # Get a list of message names that this object receives.
86
+ #
87
+ def self.messages
88
+ return super + [ 'run' ]
89
+ end
90
+
91
+ #
92
+ # Show the menu options, and prompt for user input.
93
+ #
94
+ def msg_run
95
+ loop do
96
+ begin_menu
97
+ if prompt_value.empty?
98
+ dt = DateTime.now
99
+ d = dt.strftime( '%Y.%m.%d' )
100
+ t = dt.strftime( '%I:%M:%S' )
101
+ cmd = $prompt.ask( "#{d.yellow} #{t.white} >" )
102
+ else
103
+ cmd = $prompt.ask( prompt_value )
104
+ end
105
+ cmd ? run_command( cmd ) : run_default
106
+ break unless loop?
107
+ end
108
+ end
109
+
110
+ # ---------------------------------------------------------------------
111
+ # Menu actions
112
+ # ---------------------------------------------------------------------
113
+
114
+ #
115
+ # Begin the menu execution.
116
+ # Run the before menu script if there is one,
117
+ # then show options unless we are hiding them by default.
118
+ #
119
+ def begin_menu
120
+ run_before_menu
121
+
122
+ # Check to see if we should show items at all.
123
+ o = find_child HIDE_ITEMS
124
+ return if o && o.value == true
125
+
126
+ show_options
127
+ end
128
+
129
+ #
130
+ # If there is a before menu script, run it now.
131
+ #
132
+ def run_before_menu
133
+ o = find_child BEFORE_MENU
134
+ return unless o
135
+
136
+ Gloo::Exec::Dispatch.message 'run', o
137
+ end
138
+
139
+ #
140
+ # Show the list of menu options.
141
+ #
142
+ def show_options
143
+ o = find_child ITEMS
144
+ return unless o
145
+
146
+ o.children.each do |mitem|
147
+ mitem = Gloo::Objs::Alias.resolve_alias( mitem )
148
+ puts " #{mitem.shortcut_value} - #{mitem.description_value}"
149
+ end
150
+ end
151
+
152
+ #
153
+ # Find the command matching user input.
154
+ #
155
+ def find_cmd( cmd )
156
+ o = find_child ITEMS
157
+ return nil unless o
158
+
159
+ o.children.each do |mitem|
160
+ mitem = Gloo::Objs::Alias.resolve_alias( mitem )
161
+ return mitem if mitem.shortcut_value.downcase == cmd.downcase
162
+ end
163
+
164
+ return nil
165
+ end
166
+
167
+ #
168
+ # Run the default option.
169
+ #
170
+ def run_default
171
+ obj = find_child DEFAULT
172
+ return unless obj
173
+
174
+ s = Gloo::Core::Script.new obj
175
+ s.run
176
+ end
177
+
178
+ #
179
+ # Run the selected command.
180
+ #
181
+ def run_command( cmd )
182
+ obj = find_cmd cmd
183
+
184
+ unless obj
185
+ if cmd == '?'
186
+ show_options
187
+ else
188
+ puts "#{cmd} is not a valid option"
189
+ end
190
+ return
191
+ end
192
+
193
+ script = obj.do_script
194
+ return unless script
195
+
196
+ s = Gloo::Core::Script.new script
197
+ s.run
198
+ end
199
+
200
+ # ---------------------------------------------------------------------
201
+ # Help
202
+ # ---------------------------------------------------------------------
203
+
204
+ #
205
+ # Get help for this object type.
206
+ #
207
+ def self.help
208
+ return <<~TEXT
209
+ MENU OBJECT TYPE
210
+ NAME: menu
211
+ SHORTCUT: menu
212
+
213
+ DESCRIPTION
214
+ A CLI menu.
215
+ This can be used for the main loop of a CLI application.
216
+
217
+ CHILDREN
218
+ prompt - string - '> '
219
+ The shortcut may be used to select the menu item.
220
+ items - container
221
+ A textual description of the menu item action.
222
+ loop - boolean
223
+ The script that will be run if the menu item is selected.
224
+ default - script
225
+ Optional script element. Run this if no other option selected.
226
+
227
+ MESSAGES
228
+ run - Show the options and the the prompt.
229
+ Then run the script for the user's selection.
230
+ Optionally repeat as long as the loop child is true.
231
+ TEXT
232
+ end
233
+
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,128 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A CLI menu item. One element in a CLI menu.
5
+ #
6
+
7
+ module Gloo
8
+ module Objs
9
+ class MenuItem < Gloo::Core::Obj
10
+
11
+ KEYWORD = 'menu_item'.freeze
12
+ KEYWORD_SHORT = 'mitem'.freeze
13
+ SHORTCUT = 'shortcut'.freeze
14
+ DESCRIPTION = 'description'.freeze
15
+ DO = 'do'.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 value of the menu item shortcut.
33
+ # Returns nil if there is none.
34
+ #
35
+ def shortcut_value
36
+ o = find_child SHORTCUT
37
+ return self.name unless o
38
+
39
+ return o.value
40
+ end
41
+
42
+ #
43
+ # Get the action's description.
44
+ # Returns nil if there is none.
45
+ #
46
+ def description_value
47
+ o = find_child DESCRIPTION
48
+ return self.value unless o
49
+
50
+ return o.value
51
+ end
52
+
53
+ #
54
+ # Get the action's script.
55
+ # Returns nil if there is none.
56
+ #
57
+ def do_script
58
+ return find_child DO
59
+ end
60
+
61
+ # ---------------------------------------------------------------------
62
+ # Children
63
+ # ---------------------------------------------------------------------
64
+
65
+ # Does this object have children to add when an object
66
+ # is created in interactive mode?
67
+ # This does not apply during obj load, etc.
68
+ def add_children_on_create?
69
+ return true
70
+ end
71
+
72
+ # Add children to this object.
73
+ # This is used by containers to add children needed
74
+ # for default configurations.
75
+ def add_default_children
76
+ fac = $engine.factory
77
+ fac.create_string SHORTCUT, '', self
78
+ fac.create_string DESCRIPTION, '', self
79
+ fac.create_script DO, '', self
80
+ end
81
+
82
+ # ---------------------------------------------------------------------
83
+ # Messages
84
+ # ---------------------------------------------------------------------
85
+
86
+ #
87
+ # Get a list of message names that this object receives.
88
+ #
89
+ def self.messages
90
+ return super
91
+ end
92
+
93
+ # ---------------------------------------------------------------------
94
+ # Help
95
+ # ---------------------------------------------------------------------
96
+
97
+ #
98
+ # Get help for this object type.
99
+ #
100
+ def self.help
101
+ return <<~TEXT
102
+ MENU_ITEM OBJECT TYPE
103
+ NAME: menu_item
104
+ SHORTCUT: mitem
105
+
106
+ DESCRIPTION
107
+ A CLI menu item. One element in a CLI menu.
108
+
109
+ CHILDREN
110
+ shortcut - string
111
+ The shortcut may be used to select the menu item.
112
+ The shortcut child is optional. If it is not provided,
113
+ the name of the menu item will be used instead.
114
+ description - string
115
+ A textual description of the menu item action.
116
+ The description child is optional. If it is not provided,
117
+ the value of the menu item will be used instead.
118
+ do - script
119
+ The script that will be run if the menu item is selected.
120
+
121
+ MESSAGES
122
+ None
123
+ TEXT
124
+ end
125
+
126
+ end
127
+ end
128
+ end