Ratch  Ratch::Console

[Validate]
Generated with Newfish 0.1.0

Console

Ratch Shell Console

NOTE: Console class is still a work in progress.

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/ratch/console.rb, line 12
12:                 def initialize
13:       require 'readline'
14:                         #root = Rush::Dir.new('/')
15:                         #home = Rush::Dir.new(ENV['HOME']) if ENV['HOME']
16:                         #pwd = Rush::Dir.new(ENV['PWD']) if ENV['PWD']
17: 
18:                         #@config = Rush::Config.new
19: 
20:                         @config.load_history.each do |item|
21:                                 Readline::HISTORY.push(item)
22:                         end
23: 
24:                         Readline.basic_word_break_characters = ""
25:                         Readline.completion_append_character = nil
26:                         Readline.completion_proc = completion_proc
27: 
28:                         @shell = Ratch::Shell.new
29: 
30:                         @pure_binding = @shell.instance_eval("binding")
31: 
32:                         $last_res = nil
33: 
34:                         #eval @config.load_env, @pure_binding
35: 
36:                         #commands = @config.load_commands
37:                         #Rush::Dir.class_eval commands
38:                         #Array.class_eval commands
39:                 end

Public Instance Methods

complete_method(receiver, dot, partial_name, pre) click to toggle source
     # File lib/ratch/console.rb, line 132
132:                 def complete_method(receiver, dot, partial_name, pre)
133:                         path = eval("#{receiver}.full_path", @pure_binding) rescue nil
134:                         box = eval("#{receiver}.box", @pure_binding) rescue nil
135:                         if path and box
136:                                 (box[path].methods - Object.methods).select do |e|
137:                                         e.match(/^#{Regexp.escape(partial_name)}/)
138:                                 end.map do |e|
139:                                         (pre || '') + receiver + dot + e
140:                                 end
141:                         end
142:                 end
complete_variable(partial_name, pre) click to toggle source
     # File lib/ratch/console.rb, line 161
161:                 def complete_variable(partial_name, pre)
162:                         lvars = eval('local_variables', @pure_binding)
163:                         gvars = eval('global_variables', @pure_binding)
164:                         ivars = eval('instance_variables', @pure_binding)
165:                         (lvars + gvars + ivars).select do |e|
166:                                 e.match(/^#{Regexp.escape(partial_name)}/)
167:                         end.map do |e|
168:                                 (pre || '') + e
169:                         end
170:                 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/ratch/console.rb, line 181
181:                 def completion_proc
182:                         proc do |input|
183:                                 receiver, accessor, *rest = path_parts(input)
184:                                 if receiver
185:                                         case accessor
186:                                         when /^[\[\/]$/
187:                                                 complete_path(receiver, accessor, *rest)
188:                                         when /^\.$/
189:                                                 complete_method(receiver, accessor, *rest)
190:                                         when nil
191:                                                 complete_variable(receiver, *rest)
192:                                         end
193:                                 end
194:                         end
195:                 end
execute(cmd) click to toggle source

Run a single command.

    # File lib/ratch/console.rb, line 57
57:                 def execute(cmd)
58:                         res = eval(cmd, @pure_binding)
59:                         $last_res = res
60:                         eval("_ = $last_res", @pure_binding)
61:                         print_result(res)
62:                 #rescue Rush::Exception => e
63:                 #     puts "Exception #{e.class} -> #{e.message}"
64:                 rescue ::Exception => e
65:                         puts "Exception #{e.class} -> #{e.message}"
66:                         e.backtrace.each do |t|
67:                                 puts "   #{::File.expand_path(t)}"
68:                         end
69:                 end
finish() click to toggle source

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

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

Run the interactive shell using readline.

    # File lib/ratch/console.rb, line 42
42:     def run
43:       loop do
44:         cmd = Readline.readline('ratch> ')
45:      
46:         finish if cmd.nil? or cmd == 'exit'
47: 
48:         next if cmd == ""
49: 
50:         Readline::HISTORY.push(cmd)
51:      
52:         execute(cmd)
53:       end
54:     end

Disabled; run with --debug to generate this.