mysh 0.1.17 → 0.2.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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mysh.rb +28 -25
  3. data/lib/mysh/expression.rb +1 -9
  4. data/lib/mysh/external_ruby.rb +1 -1
  5. data/lib/mysh/internal/action.rb +3 -11
  6. data/lib/mysh/internal/action_pool.rb +6 -20
  7. data/lib/mysh/internal/actions/actions_path.rb +2 -2
  8. data/lib/mysh/internal/actions/cd.rb +14 -23
  9. data/lib/mysh/internal/actions/exit.rb +10 -7
  10. data/lib/mysh/internal/actions/gls.rb +63 -8
  11. data/lib/mysh/internal/actions/help.rb +20 -33
  12. data/lib/mysh/internal/actions/help/help.txt +4 -4
  13. data/lib/mysh/internal/actions/help/help_env.txt +14 -0
  14. data/lib/mysh/internal/actions/help/help_expr.txt +3 -2
  15. data/lib/mysh/internal/actions/help/help_gls.txt +41 -0
  16. data/lib/mysh/internal/actions/help/help_help.txt +7 -4
  17. data/lib/mysh/internal/actions/help/help_math.txt +1 -1
  18. data/lib/mysh/internal/actions/help/help_ruby.txt +21 -0
  19. data/lib/mysh/internal/actions/help/help_show.txt +12 -0
  20. data/lib/mysh/internal/actions/help/history.txt +19 -0
  21. data/lib/mysh/internal/actions/help/quick.txt +25 -0
  22. data/lib/mysh/internal/actions/help/sub_help.rb +41 -0
  23. data/lib/mysh/internal/actions/history.rb +46 -10
  24. data/lib/mysh/internal/actions/pwd.rb +18 -0
  25. data/lib/mysh/internal/actions/show.rb +22 -13
  26. data/lib/mysh/internal/actions/show/env.rb +43 -0
  27. data/lib/mysh/internal/actions/show/ruby.rb +50 -0
  28. data/lib/mysh/internal/actions/type.rb +27 -0
  29. data/lib/mysh/internal/actions/vls.rb +12 -12
  30. data/lib/mysh/internal/decorate.rb +2 -2
  31. data/lib/mysh/internal/format.rb +2 -4
  32. data/lib/mysh/internal/format/array.rb +23 -13
  33. data/lib/mysh/internal/format/bullets.rb +2 -2
  34. data/lib/mysh/internal/format/columns.rb +6 -11
  35. data/lib/mysh/internal/format/nil.rb +13 -0
  36. data/lib/mysh/internal/format/string.rb +23 -9
  37. data/lib/mysh/internal/manage.rb +2 -2
  38. data/lib/mysh/quick.rb +32 -0
  39. data/lib/mysh/user_input.rb +11 -4
  40. data/lib/mysh/user_input/parse.rb +1 -1
  41. data/lib/mysh/user_input/smart_source.rb +1 -1
  42. data/lib/mysh/version.rb +1 -1
  43. data/mysh.gemspec +1 -1
  44. data/samples/show.txt +1 -1
  45. data/tests/my_shell_tests.rb +25 -18
  46. metadata +16 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3c002087bb9289b3a21c696dbec4cf5eb0c2aad
4
- data.tar.gz: 89e63a29c1c26f4f84372cfa34995e820c3cf726
3
+ metadata.gz: 1d9ec79fde72cd177c9caf7f32dbb49170f27fee
4
+ data.tar.gz: c43e22e6ffdda76face828eeba719536c1b4c719
5
5
  SHA512:
6
- metadata.gz: eab8e85ede1899c6d87a457141a7664421fb9171e5175b5b3c82ac9181d4ce6838db03fe3c9f4334bbf61c9ca190524cfeddf859edefdd70c8171d65d296822a
7
- data.tar.gz: 0a8e2f49879875648bf8435e9011c4188f4f05802b95a20a6045e95b6f5583b33b9cf1f526579f91c90d33871c387ff6796eccf154635710d3a3ad26a04b5130
6
+ metadata.gz: 23a55b9b455a96f51aa0d76d8d9da88f57d7f835bc8efc03ee9f1bb8c562ecd2b669122805acc41284020cd7701df9d6aca72c60afd4ff2aa880fdce8aac385f
7
+ data.tar.gz: e5cd9f361f5c822ff5c97af5946fc6e48091b214e0727733dbced95110f788c15af63dd319baf14567c6dd4a25dee83c61f5f2d1c61e9c885847ca1797fd3d7b
data/lib/mysh.rb CHANGED
@@ -3,52 +3,55 @@
3
3
  # mysh -- MY SHell -- a Ruby/Rails inspired command line shell.
4
4
 
5
5
  require 'English'
6
- require 'mini_readline'
7
- require 'vls'
8
6
  require 'in_array'
9
7
 
10
8
  require_relative 'mysh/user_input'
9
+ require_relative 'mysh/quick'
11
10
  require_relative 'mysh/expression'
12
11
  require_relative 'mysh/internal'
13
12
  require_relative 'mysh/external_ruby'
14
13
  require_relative 'mysh/version'
15
14
 
16
- #The MY SHell module. A container for its functionality.
15
+ #The Mysh (MY SHell) module. A container for mysh and its functionality.
17
16
  module Mysh
18
17
 
19
- class << self
20
- #The input text source.
21
- attr_reader :input
22
- end
23
-
24
18
  #The actual shell method.
25
- #<br>Endemic Code Smells
26
- #* :reek:TooManyStatements
27
19
  def self.run
20
+ setup
21
+
22
+ while @mysh_running do
23
+ execute_a_command
24
+ end
25
+ end
26
+
27
+ #Common initialization tasks.
28
+ def self.setup
28
29
  reset_host
29
30
  init_input
31
+ @mysh_running = true
32
+ end
30
33
 
31
- loop do
32
- begin
33
- input = @exec_host.eval_handlebars(get_command)
34
+ #Execute a single line of input.
35
+ def self.execute_a_command
36
+ try_execute_command(@exec_host.eval_handlebars(get_command))
34
37
 
35
- try_execute_ruby_expression(input) ||
36
- try_execute_internal_command(input) ||
37
- try_execute_external_ruby(input) ||
38
- system(input)
38
+ rescue MiniReadlineEOI
39
+ @mysh_running = false
39
40
 
40
- rescue MiniReadlineEOI
41
- break
41
+ rescue Interrupt, StandardError, ScriptError => err
42
+ puts err, err.backtrace
43
+ end
42
44
 
43
- rescue Interrupt, StandardError, ScriptError => err
44
- puts err, err.backtrace
45
- end
46
- end
45
+ #Try to execute a single line of input.
46
+ def self.try_execute_command(input)
47
+ try_execute_quick_command(input) ||
48
+ try_execute_internal_command(input) ||
49
+ try_execute_external_ruby(input) ||
50
+ system(input)
47
51
  end
48
52
 
49
53
  end
50
54
 
51
- #Some test code to run a shell if this file is run directly.
52
55
  if __FILE__ == $0
53
- Mysh.run
56
+ Mysh.run #Run a shell if this file is run directly.
54
57
  end
@@ -5,7 +5,7 @@ require 'mathn'
5
5
 
6
6
  require_relative 'expression/lineage'
7
7
 
8
- #* expression.rb -- mysh ruby expression processor.
8
+ #* mysh/expression.rb -- The mysh ruby expression processor.
9
9
  module Mysh
10
10
 
11
11
  #The mysh ruby expression processor.
@@ -71,14 +71,6 @@ module Mysh
71
71
  end
72
72
  end
73
73
 
74
- #Try to execute the string as a ruby expression.
75
- def self.try_execute_ruby_expression(str)
76
- if str.start_with?('=')
77
- @exec_host.execute(str)
78
- :expression
79
- end
80
- end
81
-
82
74
  #Reset the state of the execution hosting environment.
83
75
  def self.reset_host
84
76
  @exec_host = ExecHost.new(self)
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- #* external_ruby.rb -- Support for executing Ruby files with the ruby interpreter.
3
+ #* mysh/external_ruby.rb -- Support for executing Ruby files with the ruby interpreter.
4
4
  module Mysh
5
5
 
6
6
  #Try to execute as a Ruby program.
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/action.rb -- The framework of mysh internal actions.
3
+ #* mysh/internal/action.rb -- The framework of mysh internal actions.
4
4
  module Mysh
5
5
 
6
6
  #The mysh internal action class.
@@ -11,20 +11,12 @@ module Mysh
11
11
  #The description of the action.
12
12
  attr_reader :description
13
13
 
14
- #The action of the action.
15
- attr_reader :action
16
-
17
14
  #Setup an internal action.
18
- def initialize(name, description, &action)
19
- @name, @description, @action = name, description.in_array, action
15
+ def initialize(name, description)
16
+ @name, @description = name, description.in_array
20
17
  @exec_binding = mysh_binding
21
18
  end
22
19
 
23
- #Execute the action.
24
- def execute(args)
25
- instance_exec(args, &@action)
26
- end
27
-
28
20
  #Get information about the action.
29
21
  def action_info
30
22
  [@name].concat(@description)
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/action_pool.rb -- A managed hash of mysh actions.
3
+ #* mysh/internal/action_pool.rb -- A managed hash of mysh actions.
4
4
  module Mysh
5
5
 
6
6
  #* A managed hash of mysh actions.
@@ -12,10 +12,7 @@ module Mysh
12
12
  #Create a new action pool
13
13
  def initialize(pool_name, &default_action)
14
14
  @pool_name, @pool = pool_name, {}
15
-
16
- if default_action
17
- @pool.default = Action.new("", "", &default_action)
18
- end
15
+ @pool.default = default_action
19
16
  end
20
17
 
21
18
  #Get a action.
@@ -23,24 +20,15 @@ module Mysh
23
20
  @pool[index]
24
21
  end
25
22
 
26
- #Add a action.
27
- def add(name, description, &action)
28
- split_name = name.split[0] || ""
23
+ #Add an action to the pool.
24
+ def add_action(action)
25
+ split_name = action.name.split[0] || ""
29
26
 
30
27
  if @pool.has_key?(split_name)
31
28
  fail "Add error: Action #{split_name.inspect} already exists in #{pool_name}."
32
29
  end
33
30
 
34
- @pool[split_name] = Action.new(name, description, &action)
35
- end
36
-
37
- #Add an alias for an existing action.
38
- def add_alias(new_name, old_name)
39
- unless (action = @pool[old_name.split[0]])
40
- fail "Alias error: Action #{old_name.inspect} not found in #{pool_name}"
41
- end
42
-
43
- add(new_name, action.description, &action.action)
31
+ @pool[split_name] = action
44
32
  end
45
33
 
46
34
  #Get information on all actions.
@@ -53,6 +41,4 @@ module Mysh
53
41
 
54
42
  end
55
43
 
56
-
57
44
  end
58
-
@@ -1,9 +1,9 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/actions/actions_path.rb -- A convenient hook to the actions folder.
3
+ #* mysh/internal/actions/actions_path.rb -- A convenient hook to the actions folder.
4
4
  module Mysh
5
5
 
6
- #* internal/actions/actions_path.rb -- A convenient hook to the actions folder.
6
+ #* mysh/internal/actions/actions_path.rb -- A convenient hook to the actions folder.
7
7
  class Action
8
8
 
9
9
  #Capture this folder's name.
@@ -1,32 +1,23 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/actions/cd.rb -- The mysh internal cd command.
3
+ #* mysh/internal/actions/cd.rb -- The mysh internal cd command.
4
4
  module Mysh
5
5
 
6
- #* cd.rb -- The mysh internal cd command.
7
- class Action
8
- #Add the cd command to the library.
9
- desc = 'Change directory to the optional <dir> parameter ' +
10
- 'and then display the current working directory.'
6
+ #* mysh/internal/actions/cd.rb -- The mysh internal cd command.
7
+ class CdCommand < Action
11
8
 
12
- COMMANDS.add('cd <dir>', desc) do |args|
13
- begin
14
- Dir.chdir(args[0]) unless args.empty?
15
- puts decorate(Dir.pwd)
16
- rescue => err
17
- puts "Error: #{err}"
18
- end
19
- end
20
-
21
- #Add the pwd command to the library.
22
- COMMANDS.add('pwd', 'Display the current working directory.') do |args|
23
- begin
24
- puts decorate(Dir.pwd)
25
- rescue => err
26
- puts "Error: #{err}"
27
- end
9
+ #Execute the cd command.
10
+ def call(args)
11
+ Dir.chdir(args[0]) unless args.empty?
12
+ puts decorate(Dir.pwd)
13
+ rescue => err
14
+ puts "Error: #{err}"
28
15
  end
29
16
 
30
17
  end
31
- end
32
18
 
19
+ #Add the cd command to the library.
20
+ desc = 'Change directory to the optional <dir> parameter ' +
21
+ 'and then display the current working directory.'
22
+ COMMANDS.add_action(CdCommand.new('cd <dir>', desc))
23
+ end
@@ -1,16 +1,19 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/actions/exit.rb -- The mysh internal exit command.
3
+ #* mysh/internal/actions/exit.rb -- The mysh internal exit command.
4
4
  module Mysh
5
5
 
6
- #* exit.rb -- The mysh internal exit command.
7
- class Action
8
- #Add the exit command to the library.
9
- COMMANDS.add('exit', 'Exit mysh.') do |args|
6
+ #* mysh/internal/actions/exit.rb -- The mysh internal exit command.
7
+ class ExitCommand < Action
8
+
9
+ #Execute the exit command.
10
+ def call(_args)
10
11
  raise MiniReadlineEOI
11
12
  end
12
13
 
13
- COMMANDS.add_alias('quit', 'exit')
14
14
  end
15
- end
16
15
 
16
+ #Add the exit command to the library.
17
+ COMMANDS.add_action(ExitCommand.new('exit', 'Exit mysh.'))
18
+ COMMANDS.add_action(ExitCommand.new('quit', 'Exit mysh.'))
19
+ end
@@ -1,19 +1,74 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/actions/gls.rb -- The mysh gem ls command.
3
+ #* mysh/internal/actions/gls.rb -- The mysh gls (gem ls) command.
4
4
  module Mysh
5
5
 
6
- #* internal/actions/gls.rb -- The mysh gem ls command.
7
- class Action
6
+ #* mysh/internal/actions/gls.rb -- The mysh gls (gem ls) command.
7
+ class GlsCommand < Action
8
8
 
9
- desc = 'Display the loaded ruby gems.'
9
+ #Execute the gls command.
10
+ def call(args)
11
+ process_args(args)
12
+ gather_gems
13
+ send(@report)
14
+ end
15
+
16
+ private
17
+
18
+ #Process the gls command's arguments.
19
+ def process_args (args)
20
+ @report, @mask = :short_report, /./
21
+
22
+ args.each do |arg|
23
+ if arg == '-l'
24
+ @report = :long_report
25
+ else
26
+ @mask = arg
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ #Determine which of the loaded gem specs are of interest.
33
+ def gather_gems
34
+ @specs = Gem.loaded_specs
35
+ .values
36
+ .select {|spec| !(spec.name.partition(@mask)[1]).empty?}
37
+ .sort {|first, second| first.name <=> second.name}
38
+ end
39
+
40
+ #The brief gem list report.
41
+ def short_report
42
+ puts @specs.map {|spec| spec.name}.format_mysh_columns, ""
43
+ end
44
+
45
+ #The long-winded gem list report.
46
+ def long_report
47
+ report = @specs.inject([]) do |buffer, spec|
48
+ buffer.concat(info(spec))
49
+ end
10
50
 
11
- COMMANDS.add('gls <mask>', desc) do |args|
12
- puts Gem.loaded_specs.keys.sort.join(" ")
13
- puts
51
+ puts report.mysh_bulletize
52
+ end
53
+
54
+ #Get detailed information on a gem specification.
55
+ #<br>Endemic Code Smells
56
+ #* :reek:UtilityFunction
57
+ def info(spec)
58
+ [["name", spec.name],
59
+ ["version", spec.version],
60
+ ["date", spec.date],
61
+ ["summary", spec.summary],
62
+ ["description", spec.description],
63
+ ["executables", spec.executables],
64
+ ["authors", spec.authors],
65
+ ["email", spec.email],
66
+ ["homepage", spec.homepage],
67
+ ["", ""]]
14
68
  end
15
69
 
16
70
  end
17
71
 
72
+ desc = 'Display the loaded ruby gems. See ?gls for more.'
73
+ COMMANDS.add_action(GlsCommand.new('gls <-l> <mask>', desc))
18
74
  end
19
-
@@ -1,47 +1,34 @@
1
1
  # coding: utf-8
2
2
 
3
- #* internal/actions/help.rb -- The mysh internal help command.
3
+ #* mysh/internal/actions/help.rb -- The mysh internal help command.
4
4
  module Mysh
5
5
 
6
- #* help.rb -- The mysh internal help command.
7
- class Action
8
-
9
- # Help topics
10
- HELP = ActionPool.new("HELP") do |args|
11
- puts "No help found for #{args[0].inspect}."
12
- end
13
-
14
- HELP.add("", "General help on mysh.") do |args|
15
- show_handlebar_file(ACTIONS_PATH + 'help/help.txt')
16
- end
17
-
18
- HELP.add("math", "Help on mysh math functions.") do |args|
19
- show_handlebar_file(ACTIONS_PATH + 'help/help_math.txt')
20
- end
21
-
22
- HELP.add("=", "Help on mysh ruby expressions.") do |args|
23
- show_handlebar_file(ACTIONS_PATH + 'help/help_expr.txt')
24
- end
25
-
26
- HELP.add("help", "Help on mysh help.") do |args|
27
- show_handlebar_file(ACTIONS_PATH + 'help/help_help.txt')
28
- end
29
-
30
- HELP.add_alias('?', 'help')
6
+ # Help action pool of topics.
7
+ HELP = ActionPool.new("HELP") do |args|
8
+ puts "No help found for #{args[0].inspect}."
9
+ end
31
10
 
32
- # The base help command.
33
- desc = 'Display help information for mysh with an optional topic.'
11
+ #* mysh/internal/actions/help.rb -- The mysh internal help command.
12
+ class HelpCommand < Action
34
13
 
35
- COMMANDS.add('help <topic>', desc) do |args|
36
- HELP[args[0] || ""].execute(args)
14
+ #Execute a help command by routing it to a sub-command.
15
+ #<br>Endemic Code Smells
16
+ #* :reek:UtilityFunction
17
+ def call(args)
18
+ HELP[args[0] || ""].call(args)
37
19
  end
38
20
 
39
- COMMANDS.add_alias('? <topic>', 'help')
40
-
41
21
  end
42
22
 
23
+ # The base help command.
24
+ desc = 'Display help information for mysh with an optional topic.'
25
+ COMMANDS.add_action(HelpCommand.new('help <topic>', desc))
26
+ #The help command action object.
27
+ HELP_COMMAND = HelpCommand.new('?<topic>', desc)
28
+ COMMANDS.add_action(HELP_COMMAND)
43
29
  end
44
30
 
31
+ require_relative 'help/sub_help'
32
+
45
33
  #Load up the extra help actions!
46
34
  Dir[Mysh::Action::ACTIONS_PATH + 'help/*.rb'].each {|file| require file }
47
-