class Rush::Shell
Rush::Shell
is used to create an interactive shell. It is invoked by the rush binary.
Attributes
prompt[RW]
config[RW]
history[RW]
pure_binding[RW]
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/rush/shell.rb, line 16 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 @box = Rush::Box.new @pure_binding = @box.instance_eval 'binding' $last_res = nil load_custom_commands set_readline @multiline_cmd = '' # Multiline commands should be stored somewhere $last_backtrace = '' # Backtrace should too. end
Public Instance Methods
execute(cmd)
click to toggle source
Run a single command.
# File lib/rush/shell.rb, line 60 def execute(cmd) res = eval(@multiline_cmd << "\n" << cmd, @pure_binding) $last_res = res eval('_ = $last_res', @pure_binding) @multiline_cmd = '' print_result res rescue SyntaxError => e unless e.message.include? 'unexpected end-of-input' @multiline_cmd = '' puts "Exception #{e.class} -> #{e.message}" end # Else it should be multiline command. rescue Rush::Exception => e puts "Exception #{e.class} -> #{e.message}" @multiline_cmd = '' rescue ::Exception => e puts "Exception #{e.class} -> #{e.message}" $last_backtrace = e.backtrace .map { |t| "\t#{::File.expand_path(t)}" } .join("\n") @multiline_cmd = '' end
finish()
click to toggle source
Save history to ~/.rush/history when the shell exists.
# File lib/rush/shell.rb, line 90 def finish puts exit end
load_custom_commands()
click to toggle source
# File lib/rush/shell.rb, line 41 def load_custom_commands eval config.load_env, @pure_binding commands = config.load_commands [Rush::Dir, Rush::File, Array].each { |x| x.class_eval commands } end
print_result(res)
click to toggle source
Nice printing of different return types, particularly Rush::SearchResults
.
# File lib/rush/shell.rb, line 97 def print_result(res) return if suppress_output if res.is_a? String output = res elsif res.is_a? Rush::SearchResults output = res.to_s << "#{res.entries.size} matching files with #{res.lines.size} lines" elsif res.respond_to? :each output = res.pretty_inspect else output = " = #{res.inspect}" end output.lines.count > 5 ? output.less : puts(output) end
run()
click to toggle source
Run the interactive shell using coolline.
# File lib/rush/shell.rb, line 48 def run loop do prompt = self.class.prompt || "#{`whoami`.chomp} $ " cmd = @readline.readline prompt finish if cmd.nil? || cmd == 'exit' next if cmd.empty? @history << cmd execute cmd end end
set_readline()
click to toggle source
# File lib/rush/shell.rb, line 31 def set_readline @history = Coolline::History.new config.history_file.full_path Coolline::Settings[:word_boundaries] = [' ', "\t"] Coolline::Settings[:completion_word_boundaries] = [' ', "\t"] @readline = Coolline.new do |c| c.transform_proc = proc { syntax_highlight c.line } c.completion_proc = proc { complete c.completed_word } end end
syntax_highlight(input)
click to toggle source
Syntax highlighting with coderay.
# File lib/rush/shell.rb, line 114 def syntax_highlight(input) CodeRay.encode input, :ruby, :term end