Class: RCL::Shell

Inherits:
Base show all
Defined in:
lib/rcl/shell.rb

Defined Under Namespace

Classes: Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#access, #create, #dump, #modify, #update_access, #update_modify

Constructor Details

#initialize(processor, banner, prompt = DefaultPrompt) ⇒ Shell

Returns a new instance of Shell.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rcl/shell.rb', line 15

def initialize(processor, banner, prompt=DefaultPrompt)
  raise ArgumentError, 'nil processor' if processor.nil?
  raise ArgumentError, 'invalid processor class' \
    if processor.class != Symbol
  if !banner.nil?
    raise ArgumentError, 'invalid banner class' if banner.class != Symbol
  end
  raise ArgumentError, 'nil prompt' if prompt.nil?
  raise ArgumentError, 'invalid prompt class' if prompt.class != String

  @banner = banner
  @process = processor
  @commands = {}
  @prompt = (prompt[prompt.length-1].chr != ' ') ? "#{prompt} " : prompt
  @history = []

  nil
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



11
12
13
# File 'lib/rcl/shell.rb', line 11

def history
  @history
end

#promptObject

Returns the value of attribute prompt.



10
11
12
# File 'lib/rcl/shell.rb', line 10

def prompt
  @prompt
end

Instance Method Details

#register(command, *args) ⇒ Object

TODO: Shell#register needs love

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rcl/shell.rb', line 35

def register(command, *args)
  raise ArgumentError, 'nil command' if command.nil?
  raise ArgumentError, 'invalid command class' if command.class != String
  raise ArgumentError, 'empty command' if command.empty?
  if !args.nil?
    raise ArgumentError, 'invalid args class' if args.class != Array
    raise ArgumentError, 'empty args' if args.empty?
    args.each do |arg|
      raise ArgumentError, 'nil arg' if arg.nil?
      raise ArgumentError, 'invalid arg class' if arg.class != String
      raise ArgumentError, 'empty arg' if arg.empty?
    end
  end

  @commands[command] = args

  nil
end

#run(*args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rcl/shell.rb', line 67

def run(*args)
  if !@banner.nil?
    send(@banner)
    puts
  end

  loop do
    buffer = Readline::readline(@prompt, true)
    break if buffer.nil?
    buffer.chomp!
    next if buffer.nil?
    buffer.strip!

    # double bang repeatsw last command
    if buffer =~ /^!!$/
      if !@history.last.nil?
        @history << Command.new(@history.last.command, Time.now)
        exec(@history.last.command)
      else
        puts '!!: event not found'
      end
      next
    end

    # repeat specified command from history buffer
    if buffer =~ /^!(\-)?(\d*)$/
      if $1.nil?
        # forward bang history
        pos = $2.to_i-1
        if pos < 0 || pos > @history.size-1
          puts "#{buffer}: event not found"
          next
        else
          if !@history[pos].command.nil?
            @history << Command.new(@history[pos].command, Time.now)
            exec(@history[pos].command)
          end
        end
      else
        # reverse bang history
        pos = $2.to_i-1
        if pos < 0 || pos > @history.size-1
          puts "#{buffer}: event not found"
          next
        else
          if !@history[pos].command.nil?
            @history << Command.new(@history[-pos].command, Time.now)
            exec(@history[-(pos+1)].command)
          end
        end
      end
      next
    end

    next if buffer.empty?

    @history << Command.new(buffer, Time.now)
    exec(buffer)
  end

  puts
end

#unregister(command) ⇒ Object

TODO: Shell#unregister needs love

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rcl/shell.rb', line 55

def unregister(command)
  raise ArgumentError, 'nil command' if command.nil?
  raise ArgumentError, 'invalid command class' if command.class != String
  raise ArgumentError, 'empty command' if command.empty?

  if @commands.has_key?(command)
    return @commands.delete(command)
  end

  nil
end