Class: Toast::ConsoleChannel
Instance Attribute Summary
Attributes inherited from Channel
#responder_class, #responders
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Channel
#handle, #next_msg, #responder_for
Constructor Details
#initialize(responder_class) ⇒ ConsoleChannel
Returns a new instance of ConsoleChannel.
3
4
5
6
7
|
# File 'lib/console_channel.rb', line 3
def initialize responder_class
super responder_class
@hear_commands=true
@exit_now=false
end
|
Class Method Details
.run(responder_class) ⇒ Object
21
22
23
24
|
# File 'lib/console_channel.rb', line 21
def self.run responder_class
channel=ConsoleChannel.new responder_class
channel.run
end
|
Instance Method Details
#handle_line(line) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/console_channel.rb', line 37
def handle_line line
if (line[0,1]=='.' && @hear_commands) ||
line=~/\.commands\s+on/
result=special_cmd line
return if @exit_now
send result,'' if result
else
handle Toast::Message.new(line,'console@localhost')
end
end
|
#next_line ⇒ Object
14
15
16
17
18
19
|
# File 'lib/console_channel.rb', line 14
def next_line
line = Readline::readline('> ')
return nil unless line
Readline::HISTORY.push(line)
line
end
|
#run ⇒ Object
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/console_channel.rb', line 26
def run
send "Type .exit or ctrl-D to leave",""
loop do
msg=next_line
break unless msg
handle_line msg
break if @exit_now
end
send nil,nil
end
|
#send(msg, to) ⇒ Object
9
10
11
12
|
# File 'lib/console_channel.rb', line 9
def send msg, to
puts ""
puts msg unless msg.nil?
end
|
#special_cmd(cmd) ⇒ Object
50
51
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/console_channel.rb', line 50
def special_cmd cmd
if cmd=='.exit'
@exit_now=true
return nil
elsif cmd=~/^\.srand\s+([0-9]+)/
srand $1.to_i
return "Random seed is #{$1}"
elsif cmd=~/^\.commands\s+(on|off)/
@hear_commands=($1=='on')
return "Commands are #{$1}"
elsif cmd=~/^.script\s+(\S+)/
old_dir=@current_dir
file=$1
unless file=~/^\//
file=File.expand_path(@current_dir+"/"+file)
end
@current_dir=File.dirname(file)
File.new(file).each do |line|
handle_line line.strip unless line.blank? || line=~/^\s*#/
break if @exit_now
end
@current_dir=old_dir
return nil
elsif cmd=~/^.send\s+(.*)/
args=$1.strip.split(/\s+/)
fn=args.shift.to_sym
begin
response=responder_for('console@localhost').send(fn, *args)
rescue
response=$!.to_s
end
return response
end
return "Bad command #{cmd}"
end
|