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
(Not documented)
# 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
(Not documented)
# 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
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
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
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
Nice printing of different return types, particularly Rush::SearchResults.
# File lib/path/console.rb, line 81 def print_result(res) return if self.suppress_output if res.kind_of? String puts res elsif res.kind_of? Rush::SearchResults widest = res.entries.map { |k| k.full_path.length }.max res.entries_with_lines.each do |entry, lines| print entry.full_path print ' ' * (widest - entry.full_path.length + 2) print "=> " print res.colorize(lines.first.strip.head(30)) print "..." if lines.first.strip.length > 30 if lines.size > 1 print " (plus #{lines.size - 1} more matches)" end print "\n" end puts "#{res.entries.size} matching files with #{res.lines.size} matching lines" elsif res.respond_to? :each counts = {} res.each do |item| puts item counts[item.class] ||= 0 counts[item.class] += 1 end if counts == {} puts "=> (empty set)" else count_s = counts.map do |klass, count| "#{count} x #{klass}" end.join(', ') puts "=> #{count_s}" end else puts "=> #{res.inspect}" end end
Disabled; run with --debug to generate this.