class Pocketwatch::Watcher
Attributes
command[R]
@return [String] the command to run on an interval
options[R]
@return [Hash] the options hash determined by the command-line option
parser
Public Class Methods
new(command, options = {})
click to toggle source
@param command [String] the command to run on an interval @param options [Hash] the options hash determined by the command-line
option parser
# File lib/pocketwatch/watcher.rb, line 13 def initialize(command, options = {}) @command = command if @command.nil? || @command.empty? warn "No command provided." exit 1 end @options = options end
Public Instance Methods
run()
click to toggle source
Begin the watcher task, executing the provided command in a loop. The loop will only break when interrupted in the shell.
@return [void]
# File lib/pocketwatch/watcher.rb, line 27 def run loop do # Double buffer the output by precomputing the results of the system # call. Then, clear the terminal and print the results as quickly as # possible - this should avoid any flickering due to clearing the # terminal. Restrict the output of the command to the current height of # the terminal - scrolling is not supported, and we want to see the # first part of the output, not the last. num_lines = `tput lines`.strip.to_i - 1 num_lines = 1 unless num_lines.positive? output = `#{@command}`.split("\n").first(num_lines) clear_screen puts output sleep(@options[:interval] || 2) end rescue Errno::EPIPE, SystemExit, Interrupt clear_screen exit 0 end
Private Instance Methods
clear_screen()
click to toggle source
Clear the terminal screen by writing a control code to the terminal. This avoids having to shell out to run something like `clear`.
@return [void]
# File lib/pocketwatch/watcher.rb, line 55 def clear_screen puts "\e[H\e[2J" end