class Eggsh::Shell
Constants
- ALIAS
alias hash for command alias
- SHELL_CMD
mapping from shell commands to member function
Public Class Methods
new()
click to toggle source
# File lib/shell.rb, line 14 def initialize @env = ENV.to_hash @pwd = ENV['PWD'] @translator = Eggsh::Translator.new end
Public Instance Methods
exec(line)
click to toggle source
handling a single line
# File lib/shell.rb, line 26 def exec line begin # alias first if line != '' && ALIAS.has_key?(line.split(' ')[0]) splitted = line.split(' ') splitted[0] = ALIAS[splitted[0]] line = splitted.join ' ' end if !line.empty? && SHELL_CMD.has_key?(line.split(' ')[0]) msg = send(SHELL_CMD[line.split(' ')[0]], line) puts msg if msg elsif line.empty? else begin shell_line = @translator.translate(line) unless shell_line.empty? Kernel.spawn(@env, shell_line, :chdir => @pwd) Process.wait end rescue Exception => e puts e.display end end end end
prompt()
click to toggle source
generating prompt
# File lib/shell.rb, line 21 def prompt "#{pwd.to_color(:bold_green)}$ " end
Private Instance Methods
cd(arg = '.')
click to toggle source
# File lib/shell.rb, line 64 def cd arg = '.' new_path = File.expand_path arg.split(' ')[1], @pwd if File.directory? new_path @pwd = new_path return nil else return "cd: Invalid path #{arg.split(' ')[1]}" end end
full_pwd(arg = '')
click to toggle source
# File lib/shell.rb, line 60 def full_pwd arg = '' @pwd end
pwd(arg = '')
click to toggle source
# File lib/shell.rb, line 54 def pwd arg = '' short = @pwd.sub(/^#{ENV['HOME']}/, '~').split '/' (0...(short.size - 1)).each {|i| short[i] = short[i][0..0]} short.join '/' end
quit(arg = '')
click to toggle source
# File lib/shell.rb, line 74 def quit arg = '' exit end