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

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

Overview

Base class for all interactive CLI shells.

Direct Known Subclasses

CommandShell

Constant Summary

Constants included from Banner

Banner::BANNER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Banner

#print_banner

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.



164
165
166
167
168
169
170
171
# File 'lib/ronin/core/cli/shell.rb', line 164

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)


153
154
155
# File 'lib/ronin/core/cli/shell.rb', line 153

def prompt_sigil
  @prompt_sigil
end

#shell_nameString? (readonly)

The shell's name.

Returns:

  • (String, nil)


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

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.



65
66
67
68
69
70
71
72
73
# File 'lib/ronin/core/cli/shell.rb', line 65

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.



46
47
48
49
50
51
52
53
54
# File 'lib/ronin/core/cli/shell.rb', line 46

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.



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
121
122
123
124
125
# File 'lib/ronin/core/cli/shell.rb', line 89

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

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

  shell.print_banner if shell.stdout.tty?

  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.



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

def complete(word,preposing)
end

#exec(line) ⇒ Object

This method is abstract.

Executes a command.

Parameters:

  • line (String)

    The command to execute.

Raises:

  • (NotImplementedError)


192
193
194
# File 'lib/ronin/core/cli/shell.rb', line 192

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

#promptString

The shell prompt.

Returns:

  • (String)


178
179
180
181
182
# File 'lib/ronin/core/cli/shell.rb', line 178

def prompt
  c = colors(stdout)

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