Parent

Files

Ratch::Console

Ratch Shell Console

Attributes

suppress_output[RW]

Public Class Methods

new() click to toggle source

Set up the user’s environment, including a pure binding into which env.rb and commands.rb are mixed.

# File lib/path/console.rb, line 13
def initialize
        #root = Rush::Dir.new('/')
        #home = Rush::Dir.new(ENV['HOME']) if ENV['HOME']
        #pwd = Rush::Dir.new(ENV['PWD']) if ENV['PWD']

        #@config = Rush::Config.new

        @config.load_history.each do |item|
                Readline::HISTORY.push(item)
        end

        Readline.basic_word_break_characters = ""
        Readline.completion_append_character = nil
        Readline.completion_proc = completion_proc

        @shell = Ratch::Shell.new

        @pure_binding = @shell.instance_eval("binding")

        $last_res = nil

        #eval @config.load_env, @pure_binding

        #commands = @config.load_commands
        #Rush::Dir.class_eval commands
        #Array.class_eval commands
end

Public Instance Methods

complete_method(receiver, dot, partial_name, pre) click to toggle source
# File lib/path/console.rb, line 132
def complete_method(receiver, dot, partial_name, pre)
        path = eval("#{receiver}.full_path", @pure_binding) rescue nil
        box = eval("#{receiver}.box", @pure_binding) rescue nil
        if path and box
                (box[path].methods - Object.methods).select do |e|
                        e.match(/^#{Regexp.escape(partial_name)}/)
                end.map do |e|
                        (pre || '') + receiver + dot + e
                end
        end
end
complete_variable(partial_name, pre) click to toggle source
# File lib/path/console.rb, line 161
def complete_variable(partial_name, pre)
        lvars = eval('local_variables', @pure_binding)
        gvars = eval('global_variables', @pure_binding)
        ivars = eval('instance_variables', @pure_binding)
        (lvars + gvars + ivars).select do |e|
                e.match(/^#{Regexp.escape(partial_name)}/)
        end.map do |e|
                (pre || '') + e
        end
end
completion_proc() click to toggle source

Try to do tab completion on dir square brackets and slash accessors.

Example:

dir[‘subd # presing tab here will produce dir[’subdir/ if subdir exists dir/‘subd # presing tab here will produce dir/’subdir/ if subdir exists

This isn’t that cool yet, because it can’t do multiple levels of subdirs. It does work remotely, though, which is pretty sweet.

# File lib/path/console.rb, line 181
def completion_proc
        proc do |input|
                receiver, accessor, *rest = path_parts(input)
                if receiver
                        case accessor
                        when /^[\[\/]$/
                                complete_path(receiver, accessor, *rest)
                        when /^\.$/
                                complete_method(receiver, accessor, *rest)
                        when nil
                                complete_variable(receiver, *rest)
                        end
                end
        end
end
execute(cmd) click to toggle source

Run a single command.

# File lib/path/console.rb, line 57
def execute(cmd)
        res = eval(cmd, @pure_binding)
        $last_res = res
        eval("_ = $last_res", @pure_binding)
        print_result(res)
#rescue Rush::Exception => e
#     puts "Exception #{e.class} -> #{e.message}"
rescue ::Exception => e
        puts "Exception #{e.class} -> #{e.message}"
        e.backtrace.each do |t|
                puts "   #{::File.expand_path(t)}"
        end
end
finish() click to toggle source

Save history to ~/.config/ratch/history when the shell exists.

# File lib/path/console.rb, line 72
def finish
  @config.save_history(Readline::HISTORY.to_a)
  puts
  exit
end
run() click to toggle source

Run the interactive shell using readline.

# File lib/path/console.rb, line 42
def run
  loop do
    cmd = Readline.readline('ratch> ')
 
    finish if cmd.nil? or cmd == 'exit'

    next if cmd == ""

    Readline::HISTORY.push(cmd)
 
    execute(cmd)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.