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.



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

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

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#delayObject

Returns the value of attribute delay.



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

def delay
  @delay
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#callObject



44
45
46
47
48
49
50
# File 'lib/autoreporter.rb', line 44

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

#clear_terminal!Object



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

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



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

def display_result!
  clear_terminal!
  puts *@output
end

#run_command(cmd) ⇒ Object



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

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



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

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

#wait_for_condition!Object

Wait for either @delay seconds or for user forcing autorefresh by pressing enter



38
39
40
41
42
# File 'lib/autoreporter.rb', line 38

def wait_for_condition!
  Timeout.timeout(@delay) do
    STDIN.readline
  end
end