In Files
Parent
Information
Class Index
![show/hide quicksearch [+]](../assets/icon/find.png)
- Ratch
- Ratch::Batch
- Ratch::CLI
- Ratch::ConfigUtils
- Ratch::Console
- Ratch::EmailUtils
- Ratch::Emailer
- Ratch::FTPUtils
- Ratch::FileList
- Ratch::FileNotFound
- Ratch::Help
- Ratch::POMUtils
- Ratch::RDocUtils
- Ratch::Script
- Ratch::Shell
- Ratch::System
- Ratch::TarUtils
- Ratch::XDGUtils
- Ratch::ZlibUtils
- FileTest
- Hash
- NilClass
- Object
- String
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
print_result(res)
click to toggle source
Nice printing of different return types, particularly Rush::SearchResults.
# File lib/ratch/console.rb, line 81
81: def print_result(res)
82: return if self.suppress_output
83: if res.kind_of? String
84: puts res
85: elsif res.kind_of? Rush::SearchResults
86: widest = res.entries.map { |k| k.full_path.length }.max
87: res.entries_with_lines.each do |entry, lines|
88: print entry.full_path
89: print ' ' * (widest - entry.full_path.length + 2)
90: print "=> "
91: print res.colorize(lines.first.strip.head(30))
92: print "..." if lines.first.strip.length > 30
93: if lines.size > 1
94: print " (plus #{lines.size - 1} more matches)"
95: end
96: print "\n"
97: end
98: puts "#{res.entries.size} matching files with #{res.lines.size} matching lines"
99: elsif res.respond_to? :each
100: counts = {}
101: res.each do |item|
102: puts item
103: counts[item.class] ||= 0
104: counts[item.class] += 1
105: end
106: if counts == {}
107: puts "=> (empty set)"
108: else
109: count_s = counts.map do |klass, count|
110: "#{count} x #{klass}"
111: end.join(', ')
112: puts "=> #{count_s}"
113: end
114: else
115: puts "=> #{res.inspect}"
116: end
117: 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.