module CollinsShell::Console

Public Class Methods

launch(options) click to toggle source
# File lib/collins_shell/console.rb, line 49
def launch(options)
  self.options = options
  Pry.config.commands = get_pry_commands
  Pry.config.pager = true
  Pry.custom_completions = get_pry_custom_completions
  Pry.config.exception_handler = get_pry_exception_handler
  target = CollinsShell::Console::Filesystem.new options
  setup_pry_hooks
  Pry.start(target, :prompt => get_pry_prompt)
end
options() click to toggle source
# File lib/collins_shell/console.rb, line 62
def options
  @options
end
options=(options) click to toggle source
# File lib/collins_shell/console.rb, line 59
def options=(options)
  @options = options
end
run_pry_command(command_string, options = {}) click to toggle source
# File lib/collins_shell/console.rb, line 33
def run_pry_command command_string, options = {}
  options = {
    :show_output => true,
    :output => Pry.output,
    :commands => get_pry_commands
  }.merge!(options)
  output = options[:show_output] ? options[:output] : StringIO.new
  pry = Pry.new(
          :output => output, :input => StringIO.new(command_string),
          :commands => options[:commands], :prompt => proc{""}, :hooks => Pry::Hooks.new
  )
  if options[:binding_stack] then
    pry.binding_stack = options[:binding_stack]
  end
  pry.rep(options[:context])
end

Private Class Methods

get_pry_commands() click to toggle source
# File lib/collins_shell/console.rb, line 123
def get_pry_commands
  Pry::CommandSet.new do
    import_from Pry::Commands, "help", "history", "hist", "wtf?", "show-doc", "show-source"
    alias_command "bt", "wtf?"
    import CollinsShell::Console::Commands::Default
  end
end
get_pry_custom_completions() click to toggle source
# File lib/collins_shell/console.rb, line 99
def get_pry_custom_completions
  proc do
    last = binding_stack.last
    last = last.eval('self') unless last.nil?
    if last.is_a?(CollinsShell::Console::Filesystem) then
      (last.available_commands + commands.commands.keys).flatten
    else
      commands.commands.keys
    end
  end
end
get_pry_exception_handler() click to toggle source
# File lib/collins_shell/console.rb, line 110
def get_pry_exception_handler
  proc do |output, exception, _pry_|
    if exception.is_a?(Interrupt) then
      output.puts ""
    else
      cli = CollinsShell::Cli.new [], {}
      cli.print_error exception, "command failed"
      output.puts ""
      bold = Pry::Helpers::Text.bold("type 'bt' or 'wtf?!?' for more context")
      output.puts bold
    end
  end
end
get_pry_prompt() click to toggle source
# File lib/collins_shell/console.rb, line 81
def get_pry_prompt
  get_prompt = proc { |o, waiting|
    sym = waiting ? "*" : ">"
    if o.is_a?(CollinsShell::Console::Filesystem) then
      ext = ""
      if o.asset? then
        ext = "*"
      end
      "collins #{o.path}#{ext} #{sym} "
    else
      "collins-#{o.class} #{sym} "
    end
  }
  [
    proc { |o, *| get_prompt.call(o, false) },
    proc { |o, *| get_prompt.call(o, true) }
  ]
end
setup_pry_hooks() click to toggle source
# File lib/collins_shell/console.rb, line 66
def setup_pry_hooks
  before_message = [
    'Welcome to the collins console. A few notes:',
    ' - collins-shell interacts with real servers (BE CAREFUL).',
    ' - collins-shell operates in contexts, which the prompt tells you about.',
    ' - collins-shell can be customized to your tastes. Read the docs.',
    ' - Type help at any time for help'
  ].join("\n")
  Pry.config.hooks.add_hook(:before_session, :session_start) do |out, *|
    out.puts before_message
  end
  Pry.config.hooks.add_hook(:after_session, :session_end) do |out, *|
    out.puts "Goodbye!"
  end
end