Class: Ronin::Core::CLI::Shell
- Inherits:
-
Object
- Object
- Ronin::Core::CLI::Shell
- Includes:
- CommandKit::Colors, CommandKit::Printing
- Defined in:
- lib/ronin/core/cli/shell.rb
Overview
Base class for all interactive CLI shells.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#prompt_sigil ⇒ String
readonly
The prompt sigil character (ex:
>
). -
#shell_name ⇒ String?
readonly
The shell's name.
Class Method Summary collapse
-
.prompt_sigil(new_sigil = nil) ⇒ String
The default prompt sigil.
-
.shell_name(new_name = nil) ⇒ String
The default shell prompt name.
-
.start(*arguments, **kwargs) ⇒ Object
Starts the shell and processes each line of input.
Instance Method Summary collapse
-
#complete(word, preposing) ⇒ Array<String>?
abstract
The partially input being tab completed.
-
#exec(line) ⇒ Object
abstract
Executes a command.
-
#initialize(shell_name: self.class.shell_name, prompt_sigil: self.class.prompt_sigil, **kwargs) ⇒ Shell
constructor
Initializes the shell instance.
-
#prompt ⇒ String
The shell prompt.
Constructor Details
#initialize(shell_name: self.class.shell_name, prompt_sigil: self.class.prompt_sigil, **kwargs) ⇒ Shell
Initializes the shell instance.
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_sigil ⇒ String (readonly)
The prompt sigil character (ex: >
).
148 149 150 |
# File 'lib/ronin/core/cli/shell.rb', line 148 def prompt_sigil @prompt_sigil end |
#shell_name ⇒ String? (readonly)
The shell's name.
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.
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.
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.
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.
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.
187 188 189 |
# File 'lib/ronin/core/cli/shell.rb', line 187 def exec(line) raise(NotImplementedError,"#{self.class}##{__method__} was not implemented") end |
#prompt ⇒ String
The shell prompt.
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 |