Class: OTerm::Executor

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

Defined Under Namespace

Classes: Cmd

Instance Method Summary collapse

Constructor Details

#initializeExecutor

Returns a new instance of Executor.



6
7
8
9
10
11
12
13
14
# File 'lib/oterm/executor.rb', line 6

def initialize()
  @cmds = {}
  register('help', self, :help, '[<command>] this help screen or help on a command',
           %|Show help on a specific command or a list of all commands if a specific command is not specified.|)
  register('shutdown', self, :shutdown, 'shuts down the application',
           %|Shuts down the application.|)
  register('close', self, :close, 'closes the connection',
           %|Closes the connection to the application.|)
end

Instance Method Details

#best_completion(pre, names) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/oterm/executor.rb', line 112

def best_completion(pre, names)
  plen = pre.size
  # Must make a copy as pre is not frozen.
  pre = String.new(pre)
  target = names[0]
  names = names[1..-1]
  for i in plen..target.size
    c = target[i]
    names.each do |n|
      return pre unless c == n[i]
    end
    pre << c
  end
  pre
end

#close(listener, args) ⇒ Object



51
52
53
54
# File 'lib/oterm/executor.rb', line 51

def close(listener, args)
  listener.out.pl("Closing connection")
  listener.close()
end

#execute(cmd, listener) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/oterm/executor.rb', line 20

def execute(cmd, listener)
  name, args = cmd.split(' ', 2)
  c = @cmds[name]
  if c.nil?
    missing(cmd, listener)
    return
  end
  c.target.send(c.op, listener, args)
end

#greetingObject



16
17
18
# File 'lib/oterm/executor.rb', line 16

def greeting()
  nil
end

#help(listener, arg = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/oterm/executor.rb', line 30

def help(listener, arg=nil)
  listener.out.pl()
  if !arg.nil?
    c = @cmds[arg]
    if !c.nil?
      listener.out.pl("#{arg} - #{c.summary}")
      c.desc.each do |line|
        listener.out.pl("  #{line}")
      end
      return
    end
  end
  max = 1
  @cmds.each do |name,cmd|
    max = name.size if max < name.size
  end
  @cmds.each do |name,cmd|
    listener.out.pl("  %1$*2$s - %3$s" % [name, -max, cmd.summary])
  end
end

#missing(cmd, listener) ⇒ Object

This evaluates cmd as a Ruby expression. This is great for debugging but not a wise move for a public interface. To hide this just create a methid with the same name in the subclass of this one.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/oterm/executor.rb', line 64

def missing(cmd, listener)
  begin
    result = "#{eval(cmd)}".split("\n")
  rescue Exception => e
    result = ["#{e.class}: #{e.message}"]
    e.backtrace.each do |line|
      break if line.include?('oterm/executor.rb')
      result << "\t" + line
    end
  end
  result.each do |line|
    listener.out.pl(line)
  end
end

#register(cmd, target, op, summary, desc) ⇒ Object



108
109
110
# File 'lib/oterm/executor.rb', line 108

def register(cmd, target, op, summary, desc)
  @cmds[cmd] = Cmd.new(target, op, summary, desc)
end

#shutdown(listener, args) ⇒ Object



56
57
58
59
# File 'lib/oterm/executor.rb', line 56

def shutdown(listener, args)
  listener.out.pl("Shutting down")
  listener.server.shutdown()
end

#tab(cmd, listener) ⇒ Object



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
# File 'lib/oterm/executor.rb', line 79

def tab(cmd, listener)
  comp = []
  @cmds.each_key do |name|
    comp << name if name.start_with?(cmd)
  end
  return if 0 == comp.size

  if 1 == comp.size
    listener.move_col(1000)
    listener.insert(comp[0][listener.buf.size..-1])
    listener.out.prompt()
    listener.out.p(listener.buf)
  else
    listener.out.pl()
    comp.each do |name|
      listener.out.pl(name)
    end
    best = best_completion(cmd, comp)
    if best == cmd
      listener.update_cmd(0)
    else
      listener.move_col(1000)
      listener.insert(best[listener.buf.size..-1])
      listener.out.prompt()
      listener.out.p(listener.buf)
    end
  end
end