Class: Mybot::Command

Inherits:
Object
  • Object
show all
Includes:
Fmt
Defined in:
lib/mybot/command.rb

Constant Summary

Constants included from Fmt

Fmt::WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fmt

#asterisks, #colored, #print_cmd, #print_cmd!, #print_progress, #print_stderr, #print_stdout, #spaces

Constructor Details

#initializeCommand

Returns a new instance of Command.



7
8
9
10
11
12
# File 'lib/mybot/command.rb', line 7

def initialize
  @out = @stdout = @stderr = ""
  @handlers = {}
  @time = Time.now
  @exit = -1
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



5
6
7
# File 'lib/mybot/command.rb', line 5

def channel
  @channel
end

#exitObject

Returns the value of attribute exit.



5
6
7
# File 'lib/mybot/command.rb', line 5

def exit
  @exit
end

Instance Method Details

#handle_closeObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mybot/command.rb', line 32

def handle_close
  @time = Time.now - @time
  print_cmd! "time", @time, :blue, :bold
  print_cmd! "exit", @exit, :blue, :bold

  unless @exit == 0
    print_cmd! "error", "invalid exit status", :red, :bold
    print_stdout @stdout, true
    print_stderr @stderr, true
    abort
  end
end

#handle_stderr(data) ⇒ Object



25
26
27
28
29
30
# File 'lib/mybot/command.rb', line 25

def handle_stderr(data)
  @out = data
  @stderr += data
  print_stderr data
  run_handlers
end

#handle_stdout(data) ⇒ Object



18
19
20
21
22
23
# File 'lib/mybot/command.rb', line 18

def handle_stdout(data)
  @out = data
  @stdout += data
  print_stdout data
  run_handlers
end

#on(s, &block) ⇒ Object



14
15
16
# File 'lib/mybot/command.rb', line 14

def on(s, &block)
  @handlers[s] = block
end

#resultObject



55
56
57
58
59
60
61
62
# File 'lib/mybot/command.rb', line 55

def result
  { 
    :stdout => filter(@stdout),
    :stderr => filter(@stderr),
    :exit => @exit,
    :time => @time
  }
end

#write(data) ⇒ Object



45
46
47
48
# File 'lib/mybot/command.rb', line 45

def write(data)
  print_cmd! "write", data, :blue, :bold
  @channel.send_data "#{data}\n"
end

#write!(data) ⇒ Object



50
51
52
53
# File 'lib/mybot/command.rb', line 50

def write!(data)
  print_cmd! "write", asterisks(data), :blue, :bold
  @channel.send_data "#{data}\n"
end