Class: Mysh::InternalCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/mysh/commands/cd.rb,
lib/mysh/commands/exit.rb,
lib/mysh/commands/help.rb,
lib/mysh/internal/klass.rb,
lib/mysh/internal/parse.rb,
lib/mysh/commands/history.rb,
lib/mysh/internal/decorate.rb,
lib/mysh/internal/instance.rb

Overview

The mysh internal command instance data and methods.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, &action) ⇒ InternalCommand

Setup an internal command



18
19
20
21
22
# File 'lib/mysh/internal/instance.rb', line 18

def initialize(name, description, &action)
  @name        = name
  @description = description
  @action      = action
end

Class Attribute Details

.commandsObject (readonly)

The command library, a hash.



14
15
16
# File 'lib/mysh/internal/klass.rb', line 14

def commands
  @commands
end

Instance Attribute Details

#actionObject (readonly)

The action of the command.



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

def action
  @action
end

#descriptionObject (readonly)

The description of the command.



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

def description
  @description
end

#nameObject (readonly)

The name of the command.



9
10
11
# File 'lib/mysh/internal/instance.rb', line 9

def name
  @name
end

Class Method Details

.add(command) ⇒ Object

Add a command to the command library.



18
19
20
# File 'lib/mysh/internal/klass.rb', line 18

def self.add(command)
  @commands[command.name.split[0]] = command
end

.add_alias(new_name, old_name) ⇒ Object

Add an alias for an existing command.



23
24
25
26
27
28
29
30
31
# File 'lib/mysh/internal/klass.rb', line 23

def self.add_alias(new_name, old_name)
  unless (command = @commands[old_name])
    fail "Error adding alias #{new_name} for #{old_name}"
  end

  @commands[new_name] = new(new_name.split[0],
                            command.description,
                            &command.action)
end

.backslash?Boolean

Does this file name use backslashes?

Returns:

  • (Boolean)


25
26
27
# File 'lib/mysh/internal/decorate.rb', line 25

def self.backslash?
  MiniReadline::PLATFORM == :windows
end

.decorate(name) ⇒ Object

Make the file name fit the local system.



10
11
12
# File 'lib/mysh/internal/decorate.rb', line 10

def self.decorate(name)
  dress_up_quotes(dress_up_slashes(name))
end

.dress_up_quotes(name) ⇒ Object

Dress up in quotes if needed.



20
21
22
# File 'lib/mysh/internal/decorate.rb', line 20

def self.dress_up_quotes(name)
  name[' '] ? "\"#{name}\"" : name
end

.dress_up_slashes(name) ⇒ Object

Dress up slashes and backslashes.



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

def self.dress_up_slashes(name)
  backslash? ? name.gsub("/", "\\") : name
end

.execute(str) ⇒ Object

Execute an internal command



34
35
36
37
38
39
40
41
42
43
# File 'lib/mysh/internal/klass.rb', line 34

def self.execute(str)
  unless str[0] == ' '
    command, args = parse(str.chomp)

    if (command)
      command.execute(args)
      :internal
    end
  end
end

.infoObject

Get information on all commands.



46
47
48
# File 'lib/mysh/internal/klass.rb', line 46

def self.info
  @commands.values.map { |command| command.info }
end

.parse(input) ⇒ Object

Parse a command string for use by commands.
Endemic Code Smells

  • :reek:TooManyStatements



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

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

  loop do
    begin
      next_parse_char = read_point.next
    rescue StopIteration
      break
    end

    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

  [@commands[result.shift], result]
end

Instance Method Details

#execute(args) ⇒ Object

Execute the command.



25
26
27
# File 'lib/mysh/internal/instance.rb', line 25

def execute(args)
  @action.call(args)
end

#infoObject

Get information about the command.



30
31
32
# File 'lib/mysh/internal/instance.rb', line 30

def info
  [@name, @description]
end