Method: AmdgpuFan::Cli#watch

Defined in:
lib/amdgpu_fan/cli.rb

#watch(seconds = 1) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/amdgpu_fan/cli.rb', line 102

def watch(seconds = 1)
  return puts 'Seconds must be from 1 to 600' unless (1..600).cover?(seconds.to_i)

  puts "Watching #{amdgpu_service.name} every #{seconds} second(s)...",
       '  <Press Ctrl-C to exit>'

  trap 'SIGINT' do
    puts "\nAnd now the watch is ended."
    exit 0
  end

  loop do
    time = Time.now
    puts [time.strftime('%F %T'), summary_clock, summary_fan, summary_load, summary_power,
          summary_temp].join(WATCH_FIELD_SEPARATOR)

    # It can take a second or two to run the above so we remove them from the wait
    # here to get a more consistant watch interval.
    sec_left_to_wait = time.to_i + seconds.to_i - Time.now.to_i
    sleep sec_left_to_wait if sec_left_to_wait.positive?
  end
end