Class: Ronin::Core::CLI::Shell

Inherits:
Object
  • Object
show all
Includes:
CommandKit::Colors, CommandKit::Printing
Defined in:
lib/ronin/core/cli/shell.rb

Overview

Base class for all interactive CLI shells.

Direct Known Subclasses

CommandShell

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell_name: self.class.shell_name, prompt_sigil: self.class.prompt_sigil, **kwargs) ⇒ Shell

Initializes the shell instance.

Parameters:

  • shell_name (String, nil) (defaults to: self.class.shell_name)

    The optional shell name to override shell_name.

  • prompt_sigil (String) (defaults to: self.class.prompt_sigil)

    The optional prompt sigil to override prompt_sigil.



159
160
161
162
163
164
165
166
# File 'lib/ronin/core/cli/shell.rb', line 159

def initialize(shell_name:   self.class.shell_name,
               prompt_sigil: self.class.prompt_sigil,
               **kwargs)
  super(**kwargs)

  @shell_name   = shell_name
  @prompt_sigil = prompt_sigil
end

Instance Attribute Details

#prompt_sigilString (readonly)

The prompt sigil character (ex: >).

Returns:

  • (String)


148
149
150
# File 'lib/ronin/core/cli/shell.rb', line 148

def prompt_sigil
  @prompt_sigil
end

#shell_nameString? (readonly)

The shell's name.

Returns:

  • (String, nil)


143
144
145
# File 'lib/ronin/core/cli/shell.rb', line 143

def shell_name
  @shell_name
end

Class Method Details

.prompt_sigil(new_sigil = nil) ⇒ String

The default prompt sigil.

Parameters:

  • new_sigil (String, nil) (defaults to: nil)

    The optional new prompt sigil to use.

Returns:

  • (String)

    The prompt sigil.



62
63
64
65
66
67
68
69
70
# File 'lib/ronin/core/cli/shell.rb', line 62

def self.prompt_sigil(new_sigil=nil)
  if new_sigil
    @prompt_sigil = new_sigil
  else
    @prompt_sigil ||= if superclass <= Shell
                        superclass.prompt_sigil
                      end
  end
end

.shell_name(new_name = nil) ⇒ String

The default shell prompt name.

Parameters:

  • new_name (String, nil) (defaults to: nil)

    The optional new shell prompt name to set.

Returns:

  • (String)

    The shell prompt name.



43
44
45
46
47
48
49
50
51
# File 'lib/ronin/core/cli/shell.rb', line 43

def self.shell_name(new_name=nil)
  if new_name
    @shell_name = new_name
  else
    @shell_name ||= if superclass < Shell
                      superclass.shell_name
                    end
  end
end

.start(*arguments, **kwargs) ⇒ Object

Note:

The shell will exit if Ctrl^C or Ctrl^D is pressed.

Starts the shell and processes each line of input.

Parameters:

  • arguments (Array<Object>)

    Additional arguments for initialize.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for initialize.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ronin/core/cli/shell.rb', line 86

def self.start(*arguments,**kwargs)
  shell = new(*arguments,**kwargs)

  prev_completion_proc   = Reline.completion_proc
  Reline.completion_proc = shell.method(:complete)

  begin
    loop do
      line = Reline.readline("#{shell.prompt} ", true)

      if line.nil? # Ctrl^D
        puts
        break
      end

      line.chomp!

      unless line.empty?
        begin
          shell.exec(line)
        rescue Interrupt
          # catch Ctrl^C but keep reading input
        rescue SystemExit
          break
        rescue => error
          shell.print_exception(error)
        end
      end
    end
  rescue Interrupt
    # catch Ctrl^C and return
  ensure
    Reline.completion_proc = prev_completion_proc
  end
end

Instance Method Details

#complete(word, preposing) ⇒ Array<String>?

This method is abstract.

The partially input being tab completed.

Parameters:

  • word (String)

    The partial input being tab completed.

  • preposing (String)

    The optional command name that precedes the argument that's being tab completed.

Returns:

  • (Array<String>, nil)

    The possible completion values.



137
138
# File 'lib/ronin/core/cli/shell.rb', line 137

def complete(word,preposing)
end

#exec(line) ⇒ Object

This method is abstract.

Executes a command.

Parameters:

  • line (String)

    The command to execute.

Raises:

  • (NotImplementedError)


187
188
189
# File 'lib/ronin/core/cli/shell.rb', line 187

def exec(line)
  raise(NotImplementedError,"#{self.class}##{__method__} was not implemented")
end

#promptString

The shell prompt.

Returns:

  • (String)


173
174
175
176
177
# File 'lib/ronin/core/cli/shell.rb', line 173

def prompt
  c = colors(stdout)

  "#{c.red(shell_name)}#{c.bold(c.bright_red(prompt_sigil))}"
end