Module: Mysh

Defined in:
lib/mysh.rb,
lib/mysh/init.rb,
lib/mysh/quick.rb,
lib/mysh/system.rb,
lib/mysh/process.rb,
lib/mysh/version.rb,
lib/mysh/external.rb,
lib/mysh/expression.rb,
lib/mysh/user_input.rb,
lib/mysh/input_wrapper.rb,
lib/mysh/sources/parse.rb,
lib/mysh/sources/string.rb,
lib/mysh/binding_wrapper.rb,
lib/mysh/internal/action.rb,
lib/mysh/internal/format.rb,
lib/mysh/internal/manage.rb,
lib/mysh/shell_variables.rb,
lib/mysh/sources/console.rb,
lib/mysh/internal/actions/cd.rb,
lib/mysh/internal/action_pool.rb,
lib/mysh/internal/actions/gls.rb,
lib/mysh/internal/actions/mls.rb,
lib/mysh/internal/actions/pwd.rb,
lib/mysh/internal/actions/say.rb,
lib/mysh/internal/actions/exit.rb,
lib/mysh/internal/actions/help.rb,
lib/mysh/internal/actions/load.rb,
lib/mysh/internal/actions/quit.rb,
lib/mysh/internal/actions/show.rb,
lib/mysh/internal/actions/type.rb,
lib/mysh/internal/actions/vars.rb,
lib/mysh/internal/format/bullets.rb,
lib/mysh/internal/format/columns.rb,
lib/mysh/internal/actions/comment.rb,
lib/mysh/internal/actions/elapsed.rb,
lib/mysh/internal/actions/history.rb,
lib/mysh/internal/actions/show/env.rb,
lib/mysh/internal/actions/show/ruby.rb,
lib/mysh/sources/smart_auto_complete.rb,
lib/mysh/internal/actions/actions_path.rb,
lib/mysh/internal/actions/command_line.rb,
lib/mysh/internal/actions/help/sub_help.rb,
lib/mysh/internal/actions/command_line/init.rb,
lib/mysh/internal/actions/command_line/load.rb,
lib/mysh/internal/actions/command_line/quit.rb,
lib/mysh/internal/actions/command_line/debug.rb,
lib/mysh/internal/actions/command_line/usage.rb,
lib/mysh/internal/actions/command_line/prompt.rb,
lib/mysh/shell_variables/shell_variable_store.rb,
lib/mysh/shell_variables/shell_variable_keeper.rb,
lib/mysh/internal/actions/command_line/pre_prompt.rb,
lib/mysh/internal/actions/command_line/post_prompt.rb

Overview

  • mysh/internal/actions/command_line/prompt.rb – The mysh post prompt command.

Defined Under Namespace

Modules: MNV Classes: Action, ActionPool, BindingWrapper, BulletPoints, ColumnizedPage, CommandOption, Console, DebugOption, EnvInfoCommand, GlsCommand, HelpCommand, HelpSubCommand, HistoryCommand, InitOption, InputWrapper, Keeper, LoadOption, NoDebugOption, NoInitOption, NoPostpromptOption, NoPrepromptOption, NoPromptOption, PostpromptOption, PrepromptOption, PromptOption, QuitOption, RubyInfoCommand, SmartSource, StringSource, UsageOption, VarsCommand

Constant Summary collapse

QUICK =

A hash of quick command short cuts and their actions.

Hash.new(lambda {|_input| false })
VERSION =

The version string of MY SHell.

"0.6.3"
SUMMARY =

A brief summary of this gem.

"mysh -- a Ruby inspired command line shell."
E =

Set up some popular constants

Math::E
PI =
Math::PI
PAGE_WIDTH =

Assume an 80 column working area for formatting.

80
COMMANDS =

Set up the command action pool.

ActionPool.new("COMMANDS")
HELP =
ActionPool.new("HELP", default)
HELP_COMMAND =

The help command action object.

HelpCommand.new('?<topic>', desc)
SHOW =
ActionPool.new("SHOW", default)
SHOW_COMMAND =
Action.new('@<item>', desc, &action)
MYSH_COMMENT =
Action.new('#<stuff>', desc, &action)
TIMED_COMMAND =
Action.new('%<command>', desc, &action)
HISTORY_COMMAND =

The history command action object.

HistoryCommand.new('!<arg>', desc)
COMMAND_LINE =

Action pool of command line options.

ActionPool.new("COMMAND_LINE")
USAGE =
UsageOption.new('--help', desc)

Class Method Summary collapse

Class Method Details

.execute_a_command(source) ⇒ Object

Execute a single line of input and handle exceptions.



30
31
32
33
34
35
36
# File 'lib/mysh/process.rb', line 30

def self.execute_a_command(source)
  try_execute_command(get_command(source))

rescue Interrupt, StandardError, ScriptError => err
  puts "Error #{err.class}: #{err}"
  puts err.backtrace if MNV[:debug].extract_mysh_types || defined?(MiniTest)
end

.get_command(source) ⇒ Object

Get one command from the user.



15
16
17
# File 'lib/mysh/user_input.rb', line 15

def self.get_command(source)
  get_command_extra(source, source.get_command)
end

.get_command_extra(source, str) ⇒ Object

Get any continuations of the inputs



20
21
22
23
24
25
26
# File 'lib/mysh/user_input.rb', line 20

def self.get_command_extra(source, str)
  if /\\\s*$/ =~ str
    get_command_extra(source, $PREMATCH + "\n" + source.get_command_extra(str))
  else
    str
  end
end

.inputObject

Get the user input ready.



29
30
31
32
33
34
# File 'lib/mysh/user_input.rb', line 29

def self.input
  @input ||= MiniReadline::Readline.new(history:       true,
                                        eoi_detect:    true,
                                        auto_complete: true,
                                        auto_source:   SmartSource)
end

.mysh_load_initObject

Perform init phase processing.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mysh/init.rb', line 10

def self.mysh_load_init

  unless $mysh_init_file

    if (home = ENV['HOME'])
      names = [home + '/mysh_init.mysh',
               home + '/mysh_init.rb',
               home + '/mysh_init.txt']

      $mysh_init_file = names.detect {|name| File.file?(name)}
    end

    if $mysh_init_file
      mysh "load #{$mysh_init_file.to_host_spec}"
    else
      $mysh_init_file = '<none found>'
    end
  end

end

.parse_args(input) ⇒ Object

Parse a string into components.
Endemic Code Smells

  • :reek:TooManyStatements



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mysh/sources/parse.rb', line 9

def self.parse_args(input)
  result, read_point = [], input.chars.each

  loop do
    next_parse_char = read_point.next

    if next_parse_char == '"'
      result.concat(get_string(read_point))
    elsif next_parse_char != ' '
      result.concat(get_parameter(next_parse_char, read_point))
    end
  end

  result
end

.process_command_args(args, phase) ⇒ Object

Execute command line options.
Endemic Code Smells

  • :reek:TooManyStatements



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mysh/internal/actions/command_line.rb', line 34

def self.process_command_args(args, phase)
  read_point = args.each

  loop do
    next_option = read_point.next
    next_command = COMMAND_LINE[next_option]
    raise "Error: Invalid option #{next_option.inspect}" unless next_command
    next_command.send(phase, read_point)
  end

rescue => err
  unless (msg = err.to_s).empty?
    puts "", msg, ""
  end

  HELP["usage"].process_command(nil)
  exit
end

.process_consoleObject

Process from the console.



7
8
9
# File 'lib/mysh/process.rb', line 7

def self.process_console
  process_source(Console.new)
end

.process_file(name) ⇒ Object

Process from a file.



17
18
19
# File 'lib/mysh/process.rb', line 17

def self.process_file(name)
  process_source(StringSource.new(IO.read(name)))
end

.process_source(source) ⇒ Object

Process commands from a source.



22
23
24
25
26
27
# File 'lib/mysh/process.rb', line 22

def self.process_source(source)
  until source.eoi? do
    execute_a_command(source)
  end
rescue MyshExit
end

.process_string(str) ⇒ Object

Process from a string.



12
13
14
# File 'lib/mysh/process.rb', line 12

def self.process_string(str)
  process_source(StringSource.new(str))
end

.reset_hostObject

Reset the state of the execution hosting environment.
Endemic Code Smells :reek:TooManyStatements – False positive



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mysh/expression.rb', line 18

def self.reset_host
  exec_class = Class.new do

    include Math

    #Set up a new execution environment
    def initialize
      $mysh_exec_result  = nil
      $mysh_exec_binding = mysh_binding
    end

    #Do the actual work of executing an expression.
    #<br>Note:
    #* The expression string always begins with an '=' character.
    def execute(expression)
      pp $mysh_exec_binding.eval("$mysh_exec_result" + expression)
      :expression
    end

    #Return a simple message for less convoluted error messages.
    def inspect
      "exec_host"
    end

    #Evaluate the string in the my shell context.
    def mysh_eval(str)
      $mysh_exec_binding.eval(str)
    end

    private

    #Get the previous result
    def result
       $mysh_exec_result
    end

    #Reset the state of the execution host.
    def reset
      Mysh.reset_host
      nil
    end

    #Create a binding for mysh to execute expressions in.
    def mysh_binding
      binding
    end
  end

  $mysh_exec_host = exec_class.new
end

.run(args = []) ⇒ Object

The actual shell method.



30
31
32
33
34
35
36
# File 'lib/mysh.rb', line 30

def self.run(args=[])
  process_command_args(args, :pre_boot)
  mysh_load_init
  process_command_args(args, :post_boot)

  process_console
end

.try_execute_command(str) ⇒ Object

Try to execute a single line of input. Does not handle exceptions.



39
40
41
42
43
44
45
46
# File 'lib/mysh/process.rb', line 39

def self.try_execute_command(str)
  input = InputWrapper.new(str)

  try_execute_quick(input)    ||
  try_execute_internal(input) ||
  try_execute_external(input) ||
  try_execute_system(input)
end

.try_execute_external(input) ⇒ Object

Try to execute an external file.
Endemic Code Smells

  • :reek:TooManyStatements



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mysh/external.rb', line 9

def self.try_execute_external(input)
  args = input.parsed
  file_name = args.shift

  if (file_name)
    ext = File.extname(file_name)

    if ext == '.rb'
      new_command = "#{RbConfig.ruby} #{input.cooked}"
      puts "=> #{new_command}"  if MNV[:debug].extract_mysh_types
      system(new_command)
      :ruby_exec
    elsif ext == '.mysh'
      Mysh.process_file(file_name)
      :mysh_script
    elsif ext == '.txt'
      show_handlebar_file(file_name, BindingWrapper.new(binding))
      :internal
    end
  end
end

.try_execute_internal(input) ⇒ Object

Try to execute the string as an internal action.



10
11
12
13
14
15
16
17
# File 'lib/mysh/internal/manage.rb', line 10

def self.try_execute_internal(input)
  unless input.quick_command == ' '
    if (action = COMMANDS[input.raw_command])
      action.process_command(input)
      :internal
    end
  end
end

.try_execute_quick(input) ⇒ Object

Try to execute the inputing as a quick command.



17
18
19
# File 'lib/mysh/quick.rb', line 17

def self.try_execute_quick(input)
  QUICK[input.quick_command].call(input)
end

.try_execute_system(input) ⇒ Object

Try to execute as a system program.



7
8
9
# File 'lib/mysh/system.rb', line 7

def self.try_execute_system(input)
  system(input.raw.preprocess.chomp + "\n") ? :system : :error
end