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.



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

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

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



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

def commands
  @commands
end

#delayObject

Returns the value of attribute delay.



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

def delay
  @delay
end

#fileObject

Returns the value of attribute file.



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

def file
  @file
end

#verboseObject

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

#callObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/autoreporter.rb', line 54

def call
  while true
    run_commands!
    display_result!
    wait_for_condition!
  end
rescue Interrupt
  # Allow clean exit for Ctrl-C
  return
end

#clear_terminal!Object



28
29
30
31
# File 'lib/autoreporter.rb', line 28

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



33
34
35
36
37
38
39
40
41
# File 'lib/autoreporter.rb', line 33

def display_result!
  clear_terminal!
  puts *@output
  if @file
    open(@file, "w") do |f|
      f.puts *@output.map{|s| Strings::ANSI.sanitize(s)}
    end
  end
end

#run_command(cmd) ⇒ Object



17
18
19
20
21
22
# File 'lib/autoreporter.rb', line 17

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



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

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



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

def wait_for_condition!
  begin
    Timeout.timeout(@delay) do
      STDIN.readline
    end
  rescue Timeout::Error
  end
end