Class: Rubsh
- Inherits:
-
Object
- Object
- Rubsh
- Defined in:
- lib/rubsh.rb
Class Method Summary collapse
Instance Method Summary collapse
- #alias(*args) ⇒ Object
- #history ⇒ Object
- #init_readline ⇒ Object
-
#initialize ⇒ Rubsh
constructor
A new instance of Rubsh.
- #log_cmd(cmd) ⇒ Object
- #parse_cmd(cmd, follow = true) ⇒ Object
- #parse_cmd2(cmd, follow = true) ⇒ Object
- #run ⇒ Object
- #source(fname) ⇒ Object
Constructor Details
#initialize ⇒ Rubsh
Returns a new instance of Rubsh.
13 14 15 16 17 18 19 20 |
# File 'lib/rubsh.rb', line 13 def initialize() Signal.trap('INT') { puts "Enter exit/quit to exit shell" } Signal.trap('SIGKILL') { puts "Enter exit/quit to exit shell" } end |
Class Method Details
.iscmd?(cmd = nil) ⇒ Boolean
22 23 24 25 26 27 28 29 |
# File 'lib/rubsh.rb', line 22 def self.iscmd?(cmd = nil) return nil unless cmd return true if File.exists? cmd ENV["PATH"].split(':').each do |dir| return true if File.exists? "#{dir}/#{cmd.split[0]}" end return false end |
Instance Method Details
#alias(*args) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/rubsh.rb', line 39 def alias(*args) if args.size == 0 Alias.show return end first = args.shift Alias[first] = args.join(' ') end |
#history ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/rubsh.rb', line 31 def history history_file = ENV['HOME'] + '/.rubsh/history' return unless File.exists? history_file IO.readlines("#{ENV['HOME']}/.rubsh/history").each do |line| puts line end end |
#init_readline ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/rubsh.rb', line 102 def init_readline history_file = ENV['HOME'] + '/.rubsh/history' return unless File.exists? history_file IO.readlines(history_file).each do |line| line.chomp! Readline::HISTORY.push line end end |
#log_cmd(cmd) ⇒ Object
94 95 96 97 98 |
# File 'lib/rubsh.rb', line 94 def log_cmd(cmd) @fh = File.open(ENV['HOME'] + '/.rubsh/history','a+') unless @fh @fh.puts cmd unless cmd.nil? or cmd =~ /^\s*$/ @fh.flush end |
#parse_cmd(cmd, follow = true) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rubsh.rb', line 51 def parse_cmd(cmd,follow=true) exit if cmd =~ /exit|quit/ exit if cmd == nil call,*args = cmd.split return unless call if Alias[call] and follow #prevent recursion parse_cmd Alias[call] + ' ' + args.join(' '), false elsif Commands.new.respond_to? call begin Commands.new.send call, cmd rescue puts $! end elsif Rubsh::iscmd? call system(cmd) else begin res = eval(cmd,Commands.new.get_binding) p res rescue Exception puts $! end end end |
#parse_cmd2(cmd, follow = true) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rubsh.rb', line 76 def parse_cmd2(cmd,follow=true) exit if cmd =~ /exit|quit/ exit if cmd == nil call,*args = cmd.split return unless call if Alias[call] and follow #prevent recursion parse_cmd Alias[call] + ' ' + args.join(' '), false elsif ::Rubsh::iscmd? call system(cmd) else begin p eval(cmd, Commands.new.get_binding) rescue Exception puts $! end end end |
#run ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/rubsh.rb', line 111 def run ENV['PR1'] ||= '[%h:%w] %u%% ' if not File.exists? "#{ENV['HOME']}/.rubsh" FileUtils.mkdir "#{ENV['HOME']}/.rubsh" end if File.exists? "#{ENV['HOME']}/.rubsh/rc.rb" source ENV['HOME'] + '/.rubsh/rc.rb' end init_readline loop do prompt = Prompt::parse ENV['PR1'] cmd = Readline::readline(prompt, true) Readline::HISTORY.pop if cmd =~ /^\s*$/ log_cmd cmd parse_cmd cmd end end |
#source(fname) ⇒ Object
47 48 49 |
# File 'lib/rubsh.rb', line 47 def source(fname) load fname end |