Class: Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/commands.rb

Constant Summary collapse

@@oldpwd =
nil

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/commands.rb', line 79

def method_missing(m,*args)
  if ::Rubsh::iscmd? m.to_s
    system(m.to_s)
    return
  end
  raise "rubsh: #{m}: command not found"
end

Instance Method Details

#cd(dir) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/commands.rb', line 52

def cd(dir)
  dir.gsub! /\s*cd\s*/, ''
  dir.gsub! /\s*$/, ''
  dir.gsub! /("|')/, ''
  if dir.empty?
    @@oldpwd = Dir.pwd
    Dir.chdir ENV['HOME']
    return
  end

  if dir == '-'
    if @@oldpwd
      curdir = Dir.pwd
      Dir.chdir @@oldpwd
      @@oldpwd = curdir
    else
      puts "OLDPWD not set"
    end
    return
  end

  if dir =~ /^~\/?/
    dir.gsub!('~',ENV['HOME'])
  end
  @@oldpwd = Dir.pwd
  Dir.chdir dir
end

#get_bindingObject



3
4
5
# File 'lib/commands.rb', line 3

def get_binding
  binding
end

#help(arg) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/commands.rb', line 7

def help(arg)
  case arg
  when /help.*prompt/
    puts %|
    %h  - hostname
    %u  - the username of the current user
    %w  - the current working directory, with $HOME abbreviated with a tilde
    %W  - the basename of the current working directory, with $HOME abbreviated
       with a tilde
    %Cb - blue color
    %Cc - cyan color
    %Cg - green color
    %CC - color reset
    %t  - the current time in 24-hour HH:MM:SS format
    %%  - literal %
    %$  - if the effective UID is 0, a #, otherwise a $

    Example: ENV['PR1'] = "[%u@%h]--(%t)\\n\\r[%w]%$ "
    |
  else
    puts %|
  ralias - alias command
    example: ralias 'ls ls -Gh'
    aliases ls to 'ls -Gh'

  shortcuts:
    '...'.ls - shortcut for Dir.glob(...)
    '...'.ls.du is available to do `du` on the files

  prompt:
    use ENV['PR1'] to set your prompt.
    see 'help prompt' for options

  ~/.rubsh/rc.rb
    you can define your aliases, prompt and define
    your own functions in here.

  commandline functions:
    * it gets sent it's own invocation
    * must be a one liner
    example: def test(*a); p a;end  #  test foo #=> ["test foo"]

  |
  end
end