Class: AmdgpuFan::Cli

Inherits:
Thor
  • Object
show all
Includes:
CliOutputFormat
Defined in:
lib/amdgpu_fan/cli.rb

Overview

The command-line interface class

Constant Summary collapse

ICONS =
YAML.load(File.read(File.join(__dir__, '../../config/icons.yml')))
.transform_keys(&:to_sym).freeze
WATCH_FIELD_SEPARATOR =
' | '

Constants included from CliOutputFormat

AmdgpuFan::CliOutputFormat::METER_CHAR, AmdgpuFan::CliOutputFormat::TIME_FORMAT

Instance Method Summary collapse

Instance Method Details

#connectorsObject



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

def connectors
  amdgpu_service.connectors.each do |connector|
    puts "#{connector.type} #{connector.index}:\t" +
         (connector.connected? ? connector.display_name : connector.status)
  end
end

#fanObject



40
41
42
# File 'lib/amdgpu_fan/cli.rb', line 40

def fan
  puts fan_status
end

#fan_set(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/amdgpu_fan/cli.rb', line 45

def fan_set(value)
  if value.strip.casecmp('auto').zero?
    amdgpu_service.fan_mode = :auto
  else
    return puts 'Invalid percentage' unless (0..100).cover?(value.to_i)

    amdgpu_service.fan_speed = value
  end
  puts fan_status
end

#profileObject



23
24
25
# File 'lib/amdgpu_fan/cli.rb', line 23

def profile
  puts amdgpu_service.profile_summary
end

#profile_autoObject



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

def profile_auto
  amdgpu_service.profile_auto
  puts amdgpu_service.profile_summary
end

#profile_force(state) ⇒ Object



34
35
36
37
# File 'lib/amdgpu_fan/cli.rb', line 34

def profile_force(state)
  amdgpu_service.profile_force = state
  puts amdgpu_service.profile_summary
end

#status(option = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/amdgpu_fan/cli.rb', line 57

def status(option = nil)
  puts  if option == '--logo'
  puts ICONS[:gpu] + ' GPU:'.ljust(9) + amdgpu_service.name,
       ICONS[:vbios]+ ' vBIOS:'.ljust(9) + amdgpu_service.vbios_version,
       ICONS[:display]  + ' Displays:' + amdgpu_service.display_names.join(', '),
       ICONS[:clock] + ' Clocks:'.ljust(9) + clock_status,
       ICONS[:memory] + ' Memory:'.ljust(9) + mem_total_mibibyes,
       ICONS[:fan] + ' Fan:'.ljust(9) + fan_status,
       ICONS[:temp] + ' Temp:'.ljust(9) + "#{amdgpu_service.temperature}°C",
       ICONS[:power] + ' Power:'.ljust(9) +  "#{amdgpu_service.profile_mode} profile in " \
        "#{amdgpu_service.power_dpm_state} mode using " \
        "#{amdgpu_service.power_draw} / #{amdgpu_service.power_max} Watts "\
        "(#{amdgpu_service.power_draw_percent}%)",
       ICONS[:load] + ' Load:'.ljust(9) + percent_meter(amdgpu_service.busy_percent, 20)
end

#versionObject



74
75
76
# File 'lib/amdgpu_fan/cli.rb', line 74

def version
  puts AmdgpuFan::VERSION
end

#watch(seconds = 1) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/amdgpu_fan/cli.rb', line 80

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

#watch_avgObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/amdgpu_fan/cli.rb', line 107

def watch_avg
  puts "Watching #{amdgpu_service.name} min, max and averges since #{Time.now}...",
       '  <Press Ctrl-C to exit>',
       "\n\n\n\n\n"

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

  watcher = Watcher.new amdgpu_service

  loop do
    watcher.measure
    5.times { print "\033[K\033[A" } # move up a line and clear to end of line

    puts ICONS[:clock] +  ' Core clock  ' + watcher.core_clock.to_s,
         ICONS[:memory] + ' Memory clk  ' + watcher.mem_clock.to_s,
         ICONS[:fan] +    ' Fan speed   ' + watcher.fan_speed.to_s,
         ICONS[:power] +  ' Power usage ' + watcher.power.to_s,
         ICONS[:temp] +   ' Temperature ' + watcher.temp.to_s
    sleep 1
  end
end

#watch_csv(seconds = 1) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/amdgpu_fan/cli.rb', line 134

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

  puts 'Timestamp, Core Clock (Mhz),Memory Clock (Mhz),Fan speed (rpm), '\
       'Load (%),Power (Watts),Temp (°C)'

  trap 'SIGINT' do
    exit 0
  end

  loop do
    puts [Time.now.strftime('%F %T'),
          amdgpu_service.core_clock,
          amdgpu_service.memory_clock,
          amdgpu_service.fan_speed_rpm,
          amdgpu_service.busy_percent,
          amdgpu_service.power_draw,
          amdgpu_service.temperature].join(',')
    sleep seconds.to_i
  end
end