mysh 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,7 +7,7 @@ module Mysh
7
7
  class RubyInfoCommand < Action
8
8
 
9
9
  #Execute the ? shell command.
10
- def call(_args)
10
+ def process_command(_input)
11
11
  puts "Key ruby environment information.", ""
12
12
  puts info.format_mysh_bullets, "",
13
13
  path.format_mysh_bullets, ""
@@ -3,26 +3,20 @@
3
3
  #* mysh/internal/actions/type.rb -- The mysh type command.
4
4
  module Mysh
5
5
 
6
- #* mysh/internal/actions/type.rb -- The mysh type command.
7
- class TypeCommand < Action
8
-
9
- #Execute the type command.
10
- def call(args)
11
- file_name = args.shift
6
+ #Add the type command to the library.
7
+ desc = 'Display a text file with support for optional embedded ' +
8
+ 'handlebars and mysh variables.'
12
9
 
13
- @exec_binding = binding
10
+ action = lambda do |input|
11
+ args = input.args
12
+ file_name = args.shift
14
13
 
15
- if file_name
16
- show_handlebar_file(file_name, self)
17
- else
18
- fail "A text file must be specified."
19
- end
14
+ if file_name
15
+ show_handlebar_file(file_name, BindingWrapper.new(binding))
16
+ else
17
+ fail "A text file must be specified."
20
18
  end
21
-
22
19
  end
23
20
 
24
- #Add the type command to the library.
25
- desc = 'Display a text file with support for optional embedded ' +
26
- 'handlebars and mysh variables.'
27
- COMMANDS.add_action(TypeCommand.new('type file', desc))
21
+ COMMANDS.add_action(Action.new('type file', desc, &action))
28
22
  end
@@ -19,8 +19,8 @@ module Mysh
19
19
  end
20
20
 
21
21
  #Execute a command against the internal mysh variables.
22
- def call(str)
23
- match = VAR_EXP.match(str.chomp)
22
+ def process_command(input)
23
+ match = VAR_EXP.match(input.raw)
24
24
  @name, @equals, @value = match[:name], match[:equals], match[:value]
25
25
  do_command
26
26
  :internal
@@ -5,25 +5,20 @@ require 'vls'
5
5
  #* mysh/internal/actions/vls.rb -- The mysh module vls (Version LS) command.
6
6
  module Mysh
7
7
 
8
- #* mysh/internal/actions/vls.rb -- The mysh module vls (Version LS) command.
9
- class VlsCommand < Action
8
+ #Add the vls command to the library.
9
+ desc = 'Display the loaded modules, matching the optional mask, that ' +
10
+ 'have version info.'
10
11
 
11
- #Execute the vls command.
12
- def call(args)
13
- filter = args[0] || /./
14
- results = VersionLS.vls(filter)
12
+ action = lambda do |input|
13
+ filter = input.args[0] || /./
14
+ results = VersionLS.vls(filter)
15
15
 
16
- if results.empty?
17
- puts "No modules found for filter #{filter.inspect}.", ""
18
- else
19
- puts results.format_mysh_bullets, ""
20
- end
16
+ if results.empty?
17
+ puts "No modules found for filter #{filter.inspect}.", ""
18
+ else
19
+ puts results.format_mysh_bullets, ""
21
20
  end
22
-
23
21
  end
24
22
 
25
- #Add the vls command to the library.
26
- desc = 'Display the loaded modules, matching the optional mask, that ' +
27
- 'have version info.'
28
- COMMANDS.add_action(VlsCommand.new('vls <mask>', desc))
23
+ COMMANDS.add_action(Action.new('vls <mask>', desc, &action))
29
24
  end
@@ -6,29 +6,15 @@ module Mysh
6
6
  #Set up the command action pool.
7
7
  COMMANDS = ActionPool.new("COMMANDS")
8
8
 
9
- #Parse a command string for use by commands.
10
- def self.parse_command(str)
11
- result = parse_args(str.chomp)
12
-
13
- [COMMANDS[result.shift], result]
14
- end
15
-
16
- #Execute the action of an internal command.
17
- def self.execute(str)
18
- unless str[0] == ' '
19
- action, args = parse_command(str.chomp)
20
-
21
- if action
22
- action.call(args)
9
+ #Try to execute the string as an internal action.
10
+ def self.try_execute_internal(input)
11
+ unless input.quick_command == ' '
12
+ if (action = COMMANDS[input.raw_command])
13
+ action.process_command(input)
23
14
  :internal
24
15
  end
25
16
  end
26
17
  end
27
18
 
28
- #Try to execute the string as an internal action.
29
- def self.try_execute_internal_command(str)
30
- execute(str)
31
- end
32
-
33
19
  end
34
20
 
@@ -23,7 +23,7 @@ module Mysh
23
23
  until source.eoi? do
24
24
  execute_a_command(get_command(source))
25
25
  end
26
- rescue MyshExit
26
+ rescue Interrupt, MyshExit
27
27
  end
28
28
 
29
29
  #Execute a single line of input and handle exceptions.
@@ -32,21 +32,17 @@ module Mysh
32
32
 
33
33
  rescue Interrupt, StandardError, ScriptError => err
34
34
  puts "Error #{err.class}: #{err}"
35
- puts err.backtrace if MNV[:debug]
35
+ puts err.backtrace if MNV[:debug] || defined?(MiniTest)
36
36
  end
37
37
 
38
38
  #Try to execute a single line of input. Does not handle exceptions.
39
- def self.try_execute_command(input)
40
- unless input.start_with?("$")
41
- input = input.preprocess
42
- end
43
-
44
- puts "=> #{input}" if MNV[:debug]
39
+ def self.try_execute_command(str)
40
+ input = InputWrapper.new(str)
45
41
 
46
- try_execute_quick_command(input) ||
47
- try_execute_internal_command(input) ||
48
- try_execute_external_ruby(input) ||
49
- (system(input) ? :system : :error)
42
+ try_execute_quick(input) ||
43
+ try_execute_internal(input) ||
44
+ try_execute_external(input) ||
45
+ try_execute_system(input)
50
46
  end
51
47
 
52
48
  end
@@ -4,19 +4,19 @@
4
4
  module Mysh
5
5
 
6
6
  #A hash of quick command short cuts and their actions.
7
- QUICK = Hash.new(lambda {|_str| false})
7
+ QUICK = Hash.new(lambda {|_input| false })
8
8
 
9
- QUICK['!'] = lambda {|str| HISTORY_COMMAND.quick_parse_and_call(str) }
10
- QUICK['#'] = lambda {|str| MYSH_COMMENT.call(str) }
11
- QUICK['$'] = lambda {|str| VARS_COMMAND.call(str) }
12
- QUICK['%'] = lambda {|str| TIMED_COMMAND.call(str) }
13
- QUICK['='] = lambda {|str| $mysh_exec_host.execute(str) }
14
- QUICK['?'] = lambda {|str| HELP_COMMAND.quick_parse_and_call(str) }
15
- QUICK['@'] = lambda {|str| SHOW_COMMAND.quick_parse_and_call(str) }
9
+ QUICK['!'] = lambda {|input| HISTORY_COMMAND.process_quick_command(input)}
10
+ QUICK['#'] = lambda {|input| MYSH_COMMENT.process_command(input)}
11
+ QUICK['$'] = lambda {|input| VARS_COMMAND.process_command(input)}
12
+ QUICK['%'] = lambda {|input| TIMED_COMMAND.process_command(input)}
13
+ QUICK['='] = lambda {|input| $mysh_exec_host.execute(input.raw.preprocess)}
14
+ QUICK['?'] = lambda {|input| HELP_COMMAND.process_quick_command(input)}
15
+ QUICK['@'] = lambda {|input| SHOW_COMMAND.process_quick_command(input)}
16
16
 
17
- #Try to execute the string as a quick command.
18
- def self.try_execute_quick_command(str)
19
- QUICK[str[0]].call(str)
17
+ #Try to execute the inputing as a quick command.
18
+ def self.try_execute_quick(input)
19
+ QUICK[input.quick_command].call(input)
20
20
  end
21
21
 
22
22
  end
@@ -0,0 +1,11 @@
1
+ # coding: utf-8
2
+
3
+ #* mysh/system.rb -- Support for executing through the system.
4
+ module Mysh
5
+
6
+ #Try to execute as a system program.
7
+ def self.try_execute_system(input)
8
+ system(input.raw.preprocess + "\n") ? :system : :error
9
+ end
10
+
11
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Mysh
4
4
  #The version string of MY SHell.
5
- VERSION = "0.4.0"
5
+ VERSION = "0.5.0"
6
6
 
7
7
  #A brief summary of this gem.
8
8
  SUMMARY = "mysh -- a Ruby inspired command line shell."
@@ -11,13 +11,6 @@ class MyShellTester < Minitest::Test
11
11
  #Track mini-test progress.
12
12
  include MinitestVisible
13
13
 
14
- #Evaluate the string in a mysh context.
15
- def mysh_eval(str)
16
- @mysh_binding ||= binding
17
-
18
- @mysh_binding.eval(str)
19
- end
20
-
21
14
  def test_that_module_entities_exists
22
15
  assert_equal(String, Mysh::VERSION.class)
23
16
 
@@ -26,6 +19,8 @@ class MyShellTester < Minitest::Test
26
19
  assert_equal(Class, Mysh::ActionPool.class)
27
20
  assert_equal(Module, Mysh::MNV.class)
28
21
  assert_equal(Class, Mysh::Keeper.class)
22
+ assert_equal(Class, Mysh::BindingWrapper.class)
23
+ assert_equal(Class, Mysh::InputWrapper.class)
29
24
 
30
25
  assert_equal(Mysh::ActionPool, Mysh::COMMANDS.class)
31
26
  assert_equal(Mysh::ActionPool, Mysh::HELP.class)
@@ -197,4 +192,27 @@ class MyShellTester < Minitest::Test
197
192
  assert_equal("99", MNV[:d])
198
193
  end
199
194
 
195
+ def test_the_input_wrapper
196
+ wrapper = Mysh::InputWrapper.new '@last 45 "is finished" {{ 2+2 }} ever'
197
+
198
+ assert_equal('@last 45 "is finished" {{ 2+2 }} ever', wrapper.raw)
199
+ assert_equal('@last 45 "is finished" 4 ever', wrapper.cooked)
200
+
201
+ assert_equal('@last', wrapper.raw_command)
202
+ assert_equal(' 45 "is finished" {{ 2+2 }} ever', wrapper.raw_body)
203
+
204
+ assert_equal('@', wrapper.quick_command)
205
+ assert_equal('last 45 "is finished" {{ 2+2 }} ever', wrapper.quick_body)
206
+
207
+ assert_equal(["@last", "45", "is finished", "4", "ever"], wrapper.parsed)
208
+ assert_equal(["45", "is finished", "4", "ever"], wrapper.args)
209
+
210
+ assert_equal(wrapper, wrapper.quick)
211
+
212
+ assert_equal('@', wrapper.raw_command)
213
+ assert_equal('last 45 "is finished" {{ 2+2 }} ever', wrapper.raw_body)
214
+ assert_equal(["@", "last", "45", "is finished", "4", "ever"], wrapper.parsed)
215
+ assert_equal(["last", "45", "is finished", "4", "ever"], wrapper.args)
216
+ end
217
+
200
218
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Camilleri
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-17 00:00:00.000000000 Z
11
+ date: 2017-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -155,11 +155,12 @@ files:
155
155
  - lib/mysh/exceptions.rb
156
156
  - lib/mysh/expression.rb
157
157
  - lib/mysh/expression/lineage.rb
158
- - lib/mysh/external_ruby.rb
158
+ - lib/mysh/external.rb
159
159
  - lib/mysh/globalize.rb
160
160
  - lib/mysh/handlebars.rb
161
161
  - lib/mysh/handlebars/string.rb
162
162
  - lib/mysh/init.rb
163
+ - lib/mysh/input_wrapper.rb
163
164
  - lib/mysh/internal.rb
164
165
  - lib/mysh/internal/action.rb
165
166
  - lib/mysh/internal/action_pool.rb
@@ -225,6 +226,7 @@ files:
225
226
  - lib/mysh/sources/parse.rb
226
227
  - lib/mysh/sources/smart_auto_complete.rb
227
228
  - lib/mysh/sources/string.rb
229
+ - lib/mysh/system.rb
228
230
  - lib/mysh/user_input.rb
229
231
  - lib/mysh/version.rb
230
232
  - mysh.gemspec