class Shell

Shell

Constants

CONFIG_FILE
PROMPT

Attributes

commands[R]
console[R]
natas[R]

Public Class Methods

new() click to toggle source
# File lib/shell.rb, line 20
def initialize
  @console = Console.new
  @natas = Natas.new(self)
  @commands = []
  ObjectSpace.each_object(Class).select { |c| c < CmdBase }.each { |c| @commands << c.new(self) }
  @commands.sort! { |a, b| a.name <=> b.name }

  return unless File.exist?(CONFIG_FILE)

  config = YAML.safe_load(File.read(CONFIG_FILE))
  @natas.levels.each do |level|
    level.password = config.fetch(level.level, nil)
  end
end

Public Instance Methods

run() click to toggle source
# File lib/shell.rb, line 35
def run
  Signal.trap('INT') { exit }

  puts @console.cyan.bold(BANNER)
  cmd_help = @commands.detect { |c| c.instance_of?(CmdHelp) }
  puts @console.yellow("Type #{cmd_help.name} or #{cmd_help.aliases.first} for list of commands")

  loop do
    line = Readline.readline(@console.green.bold(PROMPT), true)
    exit if line.nil?
    line.strip!
    next if line.empty?

    Readline::HISTORY.pop if Readline::HISTORY.length > 1 && Readline::HISTORY[-2] == line
    words = line.split(/\s+/)
    cmd = words.first.downcase
    command = @commands.detect { |c| c.name == cmd || c.aliases.include?(cmd) }
    if command.nil?
      puts @console.red('Unrecognized command ') + @console.magenta.bold(cmd)
      next
    end
    command.exec(words[1..-1])
  end
end