class Shelley::InteractiveShell

An interactive shell, supporting history and autocompletion

Attributes

prompt[RW]

Public Class Methods

new(command_registry) click to toggle source
# File lib/shelley/shells.rb, line 6
def initialize(command_registry)
  @command_registry = command_registry
  @prompt = '> '
end

Public Instance Methods

autocomplete(line) click to toggle source

Tries to autocomplete a lines

# File lib/shelley/shells.rb, line 23
def autocomplete(line)
  candidate_nodes(*line.split).map(&:name).sort
end
candidate_nodes(*path) click to toggle source

Retrieves candidate nodes for a given path

# File lib/shelley/shells.rb, line 12
def candidate_nodes(*path)
  return @command_registry.commands_tree.children if path.count.zero?
  last_name = path.pop
  node = @command_registry.commands_tree.node_by_path(*path)
  return [] if node.nil?
  last_node = node.node_by_path(last_name)
  return last_node.children unless last_node.nil?
  node.children.select { |n| n.name =~ /^#{Regexp.escape(last_name)}/ }
end
start() click to toggle source

Starts the shell

# File lib/shelley/shells.rb, line 28
def start
  Readline.completion_append_character = ' '
  Readline.completion_proc = lambda do |_line|
    autocomplete(Readline.line_buffer)
  end
  while (line = Readline.readline(@prompt, true))
    begin
      @command_registry.execute_command(line)
    rescue StandardError => msg
      puts msg
    end
  end
end