Class: Autoreporter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAutoreporter

Returns a new instance of Autoreporter.



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

def initialize
  @output = nil
  @delay = 60
  @verbose = false
  @commands = []
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#delayObject

Returns the value of attribute delay.



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

def delay
  @delay
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#callObject



39
40
41
42
43
44
45
# File 'lib/autoreporter.rb', line 39

def call
  while true
    run_commands!
    display_result!
    wait_for_condition!
  end
end

#clear_terminal!Object



25
26
27
28
# File 'lib/autoreporter.rb', line 25

def clear_terminal!
  # system("clear") doesn't clear scrollback buffer on iTerm2, we need to do this:
  print "\e[H\e[J\e[3J"
end

#display_result!Object



30
31
32
33
# File 'lib/autoreporter.rb', line 30

def display_result!
  clear_terminal!
  puts *@output
end

#run_command(cmd) ⇒ Object



14
15
16
17
18
19
# File 'lib/autoreporter.rb', line 14

def run_command(cmd)
  output, status = Open3.capture2e(cmd)
  output += "\n" if output != "" and output[-1] != "\n"
  output = "Running: #{cmd}\n" + output if verbose
  output
end

#run_commands!Object



21
22
23
# File 'lib/autoreporter.rb', line 21

def run_commands!
  @output = @commands.map{|cmd| run_command(cmd)}
end

#wait_for_condition!Object



35
36
37
# File 'lib/autoreporter.rb', line 35

def wait_for_condition!
  sleep @delay
end