gloo 0.5.4 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/Gemfile.lock +1 -1
  4. data/lib/gloo/app/args.rb +48 -6
  5. data/lib/gloo/app/engine.rb +60 -38
  6. data/lib/gloo/app/help.rb +137 -16
  7. data/lib/gloo/app/info.rb +3 -1
  8. data/lib/gloo/app/log.rb +23 -0
  9. data/lib/gloo/app/mode.rb +2 -0
  10. data/lib/gloo/app/settings.rb +98 -47
  11. data/lib/gloo/convert/converter.rb +35 -0
  12. data/lib/gloo/convert/string_to_datetime.rb +1 -1
  13. data/lib/gloo/convert/string_to_decimal.rb +1 -1
  14. data/lib/gloo/convert/string_to_integer.rb +1 -1
  15. data/lib/gloo/core/dictionary.rb +99 -22
  16. data/lib/gloo/core/error.rb +19 -8
  17. data/lib/gloo/core/event_manager.rb +4 -0
  18. data/lib/gloo/core/factory.rb +11 -0
  19. data/lib/gloo/core/gloo_system.rb +1 -1
  20. data/lib/gloo/core/heap.rb +11 -3
  21. data/lib/gloo/core/here.rb +36 -0
  22. data/lib/gloo/core/it.rb +6 -0
  23. data/lib/gloo/core/literal.rb +5 -0
  24. data/lib/gloo/core/obj.rb +22 -0
  25. data/lib/gloo/core/op.rb +7 -0
  26. data/lib/gloo/core/parser.rb +6 -0
  27. data/lib/gloo/core/pn.rb +38 -0
  28. data/lib/gloo/core/tokens.rb +66 -22
  29. data/lib/gloo/core/verb.rb +12 -0
  30. data/lib/gloo/exec/action.rb +48 -0
  31. data/lib/gloo/exec/dispatch.rb +15 -5
  32. data/lib/gloo/exec/exec_env.rb +74 -0
  33. data/lib/gloo/exec/runner.rb +4 -2
  34. data/lib/gloo/{core → exec}/script.rb +21 -9
  35. data/lib/gloo/exec/stack.rb +78 -0
  36. data/lib/gloo/expr/expression.rb +26 -4
  37. data/lib/gloo/expr/l_boolean.rb +6 -0
  38. data/lib/gloo/expr/l_decimal.rb +5 -0
  39. data/lib/gloo/expr/l_integer.rb +7 -0
  40. data/lib/gloo/expr/l_string.rb +7 -0
  41. data/lib/gloo/expr/op_div.rb +2 -0
  42. data/lib/gloo/expr/op_minus.rb +2 -0
  43. data/lib/gloo/expr/op_mult.rb +2 -0
  44. data/lib/gloo/expr/op_plus.rb +2 -0
  45. data/lib/gloo/help/app/application.txt +22 -0
  46. data/lib/gloo/help/app/configuration.txt +7 -0
  47. data/lib/gloo/help/app/default_help.txt +14 -0
  48. data/lib/gloo/help/app/logging.txt +16 -0
  49. data/lib/gloo/help/core/color.txt +31 -0
  50. data/lib/gloo/help/core/error.txt +33 -0
  51. data/lib/gloo/help/core/events.txt +21 -0
  52. data/lib/gloo/help/core/gloo_system.txt +54 -0
  53. data/lib/gloo/help/core/here.txt +30 -0
  54. data/lib/gloo/help/core/it.txt +23 -0
  55. data/lib/gloo/help/core/ops.txt +16 -0
  56. data/lib/gloo/help/core/pathname.txt +29 -0
  57. data/lib/gloo/help/objs/basic/alias.txt +36 -0
  58. data/lib/gloo/help/objs/basic/boolean.txt +28 -0
  59. data/lib/gloo/help/objs/basic/container.txt +33 -0
  60. data/lib/gloo/help/objs/basic/decimal.txt +28 -0
  61. data/lib/gloo/help/objs/basic/integer.txt +27 -0
  62. data/lib/gloo/help/objs/basic/script.txt +29 -0
  63. data/lib/gloo/help/objs/basic/string.txt +28 -0
  64. data/lib/gloo/help/objs/basic/text.txt +27 -0
  65. data/lib/gloo/help/objs/basic/untyped.txt +22 -0
  66. data/lib/gloo/help/objs/cli/banner.txt +49 -0
  67. data/lib/gloo/help/objs/cli/bar.txt +34 -0
  68. data/lib/gloo/help/objs/cli/colorize.txt +33 -0
  69. data/lib/gloo/help/objs/cli/confirm.txt +26 -0
  70. data/lib/gloo/help/objs/cli/menu.txt +44 -0
  71. data/lib/gloo/help/objs/cli/menu_item.txt +26 -0
  72. data/lib/gloo/help/objs/cli/pastel.txt +43 -0
  73. data/lib/gloo/help/objs/cli/prompt.txt +27 -0
  74. data/lib/gloo/help/objs/cli/select.txt +34 -0
  75. data/lib/gloo/help/objs/ctrl/each.txt +48 -0
  76. data/lib/gloo/help/objs/ctrl/repeat.txt +38 -0
  77. data/lib/gloo/help/objs/data/markdown.txt +25 -0
  78. data/lib/gloo/help/objs/data/table.txt +46 -0
  79. data/lib/gloo/help/objs/dev/git_repo.txt +35 -0
  80. data/lib/gloo/help/objs/dt/date.txt +23 -0
  81. data/lib/gloo/help/objs/dt/datetime.txt +24 -0
  82. data/lib/gloo/help/objs/dt/time.txt +23 -0
  83. data/lib/gloo/help/objs/ror/erb.txt +37 -0
  84. data/lib/gloo/help/objs/ror/eval.txt +24 -0
  85. data/lib/gloo/help/objs/snd/play.txt +23 -0
  86. data/lib/gloo/help/objs/snd/say.txt +28 -0
  87. data/lib/gloo/help/objs/system/file.txt +41 -0
  88. data/lib/gloo/help/objs/system/system.txt +32 -0
  89. data/lib/gloo/help/objs/web/http_get.txt +35 -0
  90. data/lib/gloo/help/objs/web/http_post.txt +32 -0
  91. data/lib/gloo/help/objs/web/json.txt +34 -0
  92. data/lib/gloo/help/objs/web/slack.txt +33 -0
  93. data/lib/gloo/help/objs/web/teams.txt +30 -0
  94. data/lib/gloo/help/objs/web/uri.txt +37 -0
  95. data/lib/gloo/help/verbs/alert.txt +33 -0
  96. data/lib/gloo/help/verbs/beep.txt +25 -0
  97. data/lib/gloo/help/verbs/cls.txt +24 -0
  98. data/lib/gloo/help/verbs/context.txt +43 -0
  99. data/lib/gloo/help/verbs/create.txt +33 -0
  100. data/lib/gloo/help/verbs/execute.txt +27 -0
  101. data/lib/gloo/help/verbs/help.txt +34 -0
  102. data/lib/gloo/help/verbs/if.txt +37 -0
  103. data/lib/gloo/help/verbs/list.txt +34 -0
  104. data/lib/gloo/help/verbs/load.txt +38 -0
  105. data/lib/gloo/help/verbs/move.txt +42 -0
  106. data/lib/gloo/help/verbs/put.txt +38 -0
  107. data/lib/gloo/help/verbs/quit.txt +25 -0
  108. data/lib/gloo/help/verbs/run.txt +41 -0
  109. data/lib/gloo/help/verbs/save.txt +26 -0
  110. data/lib/gloo/help/verbs/show.txt +30 -0
  111. data/lib/gloo/help/verbs/tell.txt +34 -0
  112. data/lib/gloo/help/verbs/unless.txt +38 -0
  113. data/lib/gloo/help/verbs/version.txt +32 -0
  114. data/lib/gloo/help/verbs/wait.txt +29 -0
  115. data/lib/gloo/objs/basic/alias.rb +0 -33
  116. data/lib/gloo/objs/basic/boolean.rb +6 -27
  117. data/lib/gloo/objs/basic/container.rb +0 -33
  118. data/lib/gloo/objs/basic/decimal.rb +8 -28
  119. data/lib/gloo/objs/basic/integer.rb +5 -26
  120. data/lib/gloo/objs/basic/script.rb +1 -27
  121. data/lib/gloo/objs/basic/string.rb +6 -26
  122. data/lib/gloo/objs/basic/text.rb +0 -25
  123. data/lib/gloo/objs/basic/untyped.rb +0 -26
  124. data/lib/gloo/objs/cli/banner.rb +0 -29
  125. data/lib/gloo/objs/cli/bar.rb +0 -29
  126. data/lib/gloo/objs/cli/colorize.rb +4 -34
  127. data/lib/gloo/objs/cli/confirm.rb +6 -36
  128. data/lib/gloo/objs/cli/menu.rb +6 -36
  129. data/lib/gloo/objs/cli/menu_item.rb +0 -33
  130. data/lib/gloo/objs/cli/pastel.rb +4 -27
  131. data/lib/gloo/objs/cli/prompt.rb +4 -28
  132. data/lib/gloo/objs/cli/select.rb +4 -31
  133. data/lib/gloo/objs/ctrl/each.rb +33 -33
  134. data/lib/gloo/objs/ctrl/repeat.rb +8 -29
  135. data/lib/gloo/objs/data/markdown.rb +0 -25
  136. data/lib/gloo/objs/data/table.rb +4 -32
  137. data/lib/gloo/objs/dev/git.rb +12 -30
  138. data/lib/gloo/objs/dt/date.rb +3 -25
  139. data/lib/gloo/objs/dt/datetime.rb +4 -26
  140. data/lib/gloo/objs/dt/time.rb +3 -25
  141. data/lib/gloo/objs/ror/erb.rb +11 -45
  142. data/lib/gloo/objs/ror/eval.rb +11 -38
  143. data/lib/gloo/objs/snd/play.rb +2 -25
  144. data/lib/gloo/objs/snd/say.rb +9 -31
  145. data/lib/gloo/objs/system/file_handle.rb +0 -38
  146. data/lib/gloo/objs/system/system.rb +16 -46
  147. data/lib/gloo/objs/web/http_get.rb +57 -83
  148. data/lib/gloo/objs/web/http_post.rb +8 -31
  149. data/lib/gloo/objs/web/json.rb +11 -31
  150. data/lib/gloo/objs/web/slack.rb +7 -34
  151. data/lib/gloo/objs/web/teams.rb +13 -48
  152. data/lib/gloo/objs/web/uri.rb +0 -29
  153. data/lib/gloo/persist/file_loader.rb +8 -0
  154. data/lib/gloo/persist/file_saver.rb +6 -0
  155. data/lib/gloo/persist/file_storage.rb +2 -0
  156. data/lib/gloo/persist/line_splitter.rb +2 -0
  157. data/lib/gloo/persist/persist_man.rb +6 -0
  158. data/lib/gloo/verbs/alert.rb +30 -44
  159. data/lib/gloo/verbs/beep.rb +0 -30
  160. data/lib/gloo/verbs/cls.rb +0 -30
  161. data/lib/gloo/verbs/context.rb +9 -39
  162. data/lib/gloo/verbs/create.rb +17 -32
  163. data/lib/gloo/verbs/execute.rb +52 -0
  164. data/lib/gloo/verbs/help.rb +151 -115
  165. data/lib/gloo/verbs/if.rb +46 -44
  166. data/lib/gloo/verbs/list.rb +36 -54
  167. data/lib/gloo/verbs/load.rb +8 -37
  168. data/lib/gloo/verbs/move.rb +12 -51
  169. data/lib/gloo/verbs/put.rb +28 -52
  170. data/lib/gloo/verbs/quit.rb +0 -31
  171. data/lib/gloo/verbs/run.rb +23 -39
  172. data/lib/gloo/verbs/save.rb +0 -33
  173. data/lib/gloo/verbs/show.rb +6 -31
  174. data/lib/gloo/verbs/tell.rb +31 -31
  175. data/lib/gloo/verbs/unless.rb +47 -46
  176. data/lib/gloo/verbs/version.rb +0 -30
  177. data/lib/gloo/verbs/wait.rb +0 -31
  178. metadata +79 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20f45224b6b8b6007e5f99d8552be6ba63b8f2295c0ad348442a24cf7fe22d03
4
- data.tar.gz: be35cae08d277dc082f450ac3d3bab101e1a4b027ac74288a3a828e5f3608430
3
+ metadata.gz: '08083ccc21ecec108fbfd0addc6a09686462caca7ebb2ce6e17418166a186708'
4
+ data.tar.gz: 575ae047c999595d8f9d79c510df3a19a66910a4b9494c5da3c6a5ce81a1c37b
5
5
  SHA512:
6
- metadata.gz: 992d33279fa29545b34c0b5218dd92a8ef51db57863ead34aaf16161aeae53731586eff1d9b62a361f09572949309a6158a096c88122aa85afd79f5cef688d33
7
- data.tar.gz: 52f91ea8a7966e1a00f21abf8fa6de471540e63ea6199ed07def348b18e98b610ecf9acb0ac2008ca746116dadedd168015fdcf3bec91de74bae4a65c498ed42
6
+ metadata.gz: d320a745b4ba806060e76f72088951bbd348ba9fbd3332509ef81caa710e3afb8a136b29b2cb2a7b1c796a2c8fdcdddd8f8963c8d5b5519905f3d6cd23041c49
7
+ data.tar.gz: 57c9d6cb0509e50bd76e53b218000358697cb0640a2adc3a9f7bff0ec2cf789770efc885cf56d2911b7e9ff6b77374e11f2de4f1062118ee52434a8831854aa5
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /tmp/
9
9
  *.gem
10
10
  *.log
11
+ test/gloo/debug/*
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gloo (0.5.4)
4
+ gloo (0.7.1)
5
5
  activesupport (~> 5.2, >= 5.2.4.3)
6
6
  chronic (~> 0.10, >= 0.10.2)
7
7
  colorize (~> 0.8, >= 0.8.1)
@@ -12,8 +12,14 @@ module Gloo
12
12
  module App
13
13
  class Args
14
14
 
15
+ QUIET = 'quiet'.freeze
16
+ GLOO_ENV = 'GLOO_ENV'.freeze
17
+
15
18
  attr_reader :switches, :files
16
19
 
20
+ #
21
+ # Create arguments and setup.
22
+ #
17
23
  def initialize( params = [] )
18
24
  @switches = []
19
25
  @files = []
@@ -22,9 +28,39 @@ module Gloo
22
28
  ARGV.each { |o| process_one_arg( o ) }
23
29
  end
24
30
 
31
+ #
25
32
  # Was the --quiet arg passed?
33
+ #
26
34
  def quiet?
27
- return @switches.include?( 'quiet' )
35
+ return @switches.include?( QUIET )
36
+ end
37
+
38
+ #
39
+ # Is the version switch set?
40
+ #
41
+ def version?
42
+ @switches.include?( Gloo::App::Mode::VERSION.to_s )
43
+ end
44
+
45
+ #
46
+ # Is the help switch set?
47
+ #
48
+ def help?
49
+ @switches.include?( Gloo::App::Mode::HELP.to_s )
50
+ end
51
+
52
+ #
53
+ # Is the cli switch set?
54
+ #
55
+ def cli?
56
+ @switches.include?( Gloo::App::Mode::CLI.to_s )
57
+ end
58
+
59
+ #
60
+ # Is the embed switch set?
61
+ #
62
+ def embed?
63
+ @switches.include?( Gloo::App::Mode::EMBED.to_s )
28
64
  end
29
65
 
30
66
  #
@@ -34,15 +70,15 @@ module Gloo
34
70
  # Then finally use the default: embedded mode.
35
71
  #
36
72
  def detect_mode
37
- mode = if ENV[ 'GLOO_ENV' ] == 'test'
73
+ mode = if ENV[ GLOO_ENV ] == Gloo::App::Mode::TEST.to_s
38
74
  Mode::TEST
39
- elsif @switches.include?( 'version' )
75
+ elsif version?
40
76
  Mode::VERSION
41
- elsif @switches.include?( 'help' )
77
+ elsif help?
42
78
  Mode::HELP
43
- elsif @switches.include?( 'cli' )
79
+ elsif cli?
44
80
  Mode::CLI
45
- elsif @switches.include?( 'embed' )
81
+ elsif embed?
46
82
  Mode::EMBED
47
83
  elsif @files.count.positive?
48
84
  Mode::SCRIPT
@@ -54,9 +90,15 @@ module Gloo
54
90
  return mode
55
91
  end
56
92
 
93
+ # ---------------------------------------------------------------------
94
+ # Private
95
+ # ---------------------------------------------------------------------
96
+
57
97
  private
58
98
 
99
+ #
59
100
  # Process one argument or parameter.
101
+ #
60
102
  def process_one_arg( arg )
61
103
  if arg.start_with? '--'
62
104
  switches << arg[ 2..-1 ]
@@ -2,6 +2,9 @@
2
2
  # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
3
  #
4
4
  # The Gloo Script Engine.
5
+ # The Engine aggregates all the elements needed to run gloo.
6
+ # The Engine runs the main event loop and delegates processing
7
+ # to the relevant element.
5
8
  #
6
9
 
7
10
  require 'tty-prompt'
@@ -15,8 +18,11 @@ module Gloo
15
18
  attr_reader :args, :mode, :running
16
19
  attr_reader :dictionary, :parser, :heap, :factory
17
20
  attr_accessor :last_cmd, :persist_man, :event_manager
21
+ attr_accessor :exec_env, :help, :converter
18
22
 
23
+ #
19
24
  # Set up the engine with basic elements.
25
+ #
20
26
  def initialize( params = [] )
21
27
  $engine = self
22
28
  @args = Args.new( params )
@@ -26,7 +32,10 @@ module Gloo
26
32
  $log.debug 'engine intialized...'
27
33
  end
28
34
 
35
+ #
29
36
  # Start the engine.
37
+ # Load object and verb definitions and setup engine elements.
38
+ #
30
39
  def start
31
40
  $log.debug 'starting the engine...'
32
41
  $log.debug Info.display_title
@@ -41,6 +50,11 @@ module Gloo
41
50
  @persist_man = Gloo::Persist::PersistMan.new
42
51
  @event_manager = Gloo::Core::EventManager.new
43
52
 
53
+ @exec_env = Gloo::Exec::ExecEnv.new
54
+ @help = Gloo::App::Help.new
55
+ @converter = Gloo::Convert::Converter.new
56
+
57
+ $log.debug 'the engine has started'
44
58
  run_mode
45
59
  end
46
60
 
@@ -48,12 +62,16 @@ module Gloo
48
62
  # Run
49
63
  # ---------------------------------------------------------------------
50
64
 
51
- # Run the selected mode.
65
+ #
66
+ # Run gloo in the selected mode.
67
+ #
52
68
  def run_mode
69
+ $log.debug "running gloo in #{@mode} mode"
70
+
53
71
  if @mode == Mode::VERSION
54
72
  run_version
55
73
  elsif @mode == Mode::HELP
56
- run_help
74
+ show_help_and_quit
57
75
  elsif @mode == Mode::SCRIPT
58
76
  run_files
59
77
  else
@@ -61,19 +79,20 @@ module Gloo
61
79
  end
62
80
  end
63
81
 
82
+ #
64
83
  # Run files specified on the CLI.
65
84
  # Then quit.
85
+ #
66
86
  def run_files
67
- @args.files.each do |f|
68
- @persist_man.load( f )
69
- end
70
-
87
+ @args.files.each { |f| @persist_man.load( f ) }
71
88
  quit
72
89
  end
73
90
 
74
- # Run
91
+ #
92
+ # Run in interactive mode.
93
+ #
75
94
  def run
76
- # Open default files
95
+ # Open default file(s)
77
96
  self.open_start_file
78
97
 
79
98
  # TODO: open any files specifed in args
@@ -86,28 +105,44 @@ module Gloo
86
105
  quit
87
106
  end
88
107
 
108
+ #
89
109
  # Get the setting for the start_with file and open it.
110
+ #
90
111
  def open_start_file
91
112
  name = $settings.start_with
92
113
  @persist_man.load( name ) if name
93
114
  end
94
115
 
116
+ #
95
117
  # Prompt for the next command.
118
+ #
96
119
  def prompt_cmd
120
+ @last_cmd = $prompt.ask( default_prompt )
121
+ end
122
+
123
+ #
124
+ # Get the default prompt text.
125
+ #
126
+ def default_prompt
97
127
  dt = DateTime.now
98
128
  d = dt.strftime( '%Y.%m.%d' )
99
129
  t = dt.strftime( '%I:%M:%S' )
100
-
101
- @last_cmd = $prompt.ask( "#{'gloo'.blue} #{d.yellow} #{t.white} >" )
130
+ return "#{'gloo'.blue} #{d.yellow} #{t.white} >"
102
131
  end
103
132
 
133
+ #
104
134
  # Is the last command entered blank?
135
+ #
105
136
  def last_cmd_blank?
106
137
  return true if @last_cmd.nil?
107
138
  return true if @last_cmd.strip.empty?
139
+
140
+ return false
108
141
  end
109
142
 
143
+ #
110
144
  # Prompt, Get input, process.
145
+ #
111
146
  def loop
112
147
  while @running
113
148
  prompt_cmd
@@ -115,7 +150,9 @@ module Gloo
115
150
  end
116
151
  end
117
152
 
153
+ #
118
154
  # Process the command.
155
+ #
119
156
  def process_cmd
120
157
  if last_cmd_blank?
121
158
  clear_screen
@@ -125,12 +162,16 @@ module Gloo
125
162
  @parser.run @last_cmd
126
163
  end
127
164
 
165
+ #
128
166
  # Request the engine to stop running.
167
+ #
129
168
  def stop_running
130
169
  @running = false
131
170
  end
132
171
 
172
+ #
133
173
  # Do any clean up and quit.
174
+ #
134
175
  def quit
135
176
  $log.debug 'quitting...'
136
177
  end
@@ -139,22 +180,25 @@ module Gloo
139
180
  # Helpers
140
181
  # ---------------------------------------------------------------------
141
182
 
183
+ #
142
184
  # Show the version information and then quit.
185
+ #
143
186
  def run_version
144
187
  puts Info.display_title unless @args.quiet?
145
188
  quit
146
189
  end
147
190
 
191
+ #
148
192
  # Show the help information and then quit.
149
- def run_help( keep_running = false )
150
- out = "#{Info.display_title}\n"
151
- out << Help.get_help_text
152
- $engine.heap.it.set_to out
153
- puts out unless @args.quiet?
154
- quit unless keep_running
193
+ #
194
+ def show_help_and_quit
195
+ @help.show_app_help
196
+ quit
155
197
  end
156
198
 
199
+ #
157
200
  # Clear the screen.
201
+ #
158
202
  def clear_screen
159
203
  @cursor ||= TTY::Cursor
160
204
  print @cursor.clear_screen
@@ -181,28 +225,6 @@ module Gloo
181
225
  self.heap.error.set_to msg
182
226
  end
183
227
 
184
- # ---------------------------------------------------------------------
185
- # Convert
186
- # ---------------------------------------------------------------------
187
-
188
- #
189
- # Convert the given value to the specified type,
190
- # or if no conversion is available, revert to default.
191
- #
192
- def convert( value, to_type, default = nil )
193
- begin
194
- name = "Gloo::Convert::#{value.class}To#{to_type}"
195
- clazz = name.split( '::' ).inject( Object ) { |o, c| o.const_get c }
196
- o = clazz.new
197
- return o.convert( value )
198
- rescue => e
199
- $log.error e.message
200
- $engine.heap.error.set_to e.message
201
- end
202
-
203
- return default
204
- end
205
-
206
228
  end
207
229
  end
208
230
  end
@@ -4,30 +4,151 @@
4
4
  # Help system.
5
5
  #
6
6
 
7
+ require 'tty-markdown'
8
+ require 'tty-pager'
9
+
7
10
  module Gloo
8
11
  module App
9
12
  class Help
10
13
 
11
- # Get text to display when the application is run
12
- # in HELP mode.
13
- def self.get_help_text
14
- return <<~TEXT
15
- NAME
16
- \tgloo
14
+ APPLICATION = 'application'.freeze
15
+
16
+ attr_accessor :topics
17
+
18
+ #
19
+ # Set up the help system.
20
+ # We won't load help topics until we know we need them.
21
+ #
22
+ def initialize
23
+ @topics = nil
24
+ end
25
+
26
+ #
27
+ # Show application help.
28
+ # This is the what is shown when help is run from the CLI.
29
+ #
30
+ def show_app_help
31
+ puts Info.display_title unless $engine.args.quiet?
32
+ self.show_topic APPLICATION
33
+ end
34
+
35
+ #
36
+ # Check to see if there is a topic by the given name.
37
+ #
38
+ def topic?( name )
39
+ lazy_load_index
40
+
41
+ return @topics.key?( name )
42
+ end
43
+
44
+ #
45
+ # Is the current topic Markdown?
46
+ #
47
+ def topic_is_md?( name )
48
+ lazy_load_index
49
+
50
+ topic_file = @topics[ name ]
51
+ return topic_file.end_with? '.md'
52
+ end
53
+
54
+ #
55
+ # Show a help topic, optionally paginaged.
56
+ # If the content of the help page fits on a single
57
+ # screen, it won't paginate.
58
+ #
59
+ def page_topic( topic )
60
+ return if $engine.args.quiet?
61
+
62
+ data = self.get_topic_data( topic )
63
+ page = data.lines.count > Settings.page_size
64
+ if topic_is_md?( topic )
65
+ show_markdown_data( data, page )
66
+ else
67
+ show_text_data( data, page )
68
+ end
69
+ end
70
+
71
+ #
72
+ # Show a help topic.
73
+ #
74
+ def show_topic( topic )
75
+ return if $engine.args.quiet?
76
+
77
+ data = self.get_topic_data( topic )
78
+ if topic_is_md?( topic )
79
+ show_markdown_data data
80
+ else
81
+ show_text_data data
82
+ end
83
+ end
84
+
85
+ #
86
+ # Show the markdown data.
87
+ #
88
+ def show_text_data( data, page = false )
89
+ if page
90
+ pager = TTY::Pager.new
91
+ pager.page( data )
92
+ else
93
+ puts data.white
94
+ puts
95
+ end
96
+ end
97
+
98
+ #
99
+ # Show the markdown data.
100
+ #
101
+ def show_markdown_data( data, page = false )
102
+ md = TTY::Markdown.parse data
103
+
104
+ if page
105
+ pager = TTY::Pager.new
106
+ pager.page( md )
107
+ else
108
+ puts md
109
+ puts
110
+ end
111
+ end
112
+
113
+ #
114
+ # Get topic data.
115
+ #
116
+ def get_topic_data( topic )
117
+ lazy_load_index
118
+
119
+ topic_file = @topics[ topic ]
120
+ return nil unless topic_file
121
+
122
+ File.read topic_file
123
+ end
124
+
125
+ #
126
+ # Lazy load topic index.
127
+ # We'll only load the index the first time a help topic
128
+ # is requested. After that we'll use the cached index.
129
+ #
130
+ def lazy_load_index
131
+ return if @topics
132
+
133
+ @topics = {}
134
+ $log.debug 'loading help file index...'
17
135
 
18
- DESCRIPTION
19
- \tGloo scripting language. A scripting language built on ruby.
20
- \tMore information coming soon.
136
+ pn = File.dirname( File.absolute_path( __FILE__ ) )
137
+ pn = File.dirname( pn )
21
138
 
22
- SYNOPSIS
23
- \tgloo [global option] [file]
139
+ root = File.join( pn, 'help', '**/*.txt' )
140
+ Dir.glob( root ).each do |txt_file|
141
+ key = File.basename( txt_file, '.txt' )
142
+ @topics[ key ] = txt_file
143
+ end
24
144
 
25
- GLOBAL OPTIONS
26
- \t--cli \t\t - Run in CLI mode
27
- \t--version \t - Show application version
28
- \t--help \t\t - Show this help page
145
+ root = File.join( pn, 'help', '**/*.md' )
146
+ Dir.glob( root ).each do |md_file|
147
+ key = File.basename( md_file, '.md' )
148
+ @topics[ key ] = md_file
149
+ end
29
150
 
30
- TEXT
151
+ $log.debug "Found #{@topics.count} help files"
31
152
  end
32
153
 
33
154
  end