This is a procedure of one of the items of the status bar on i3 window manager on linux. Is run every second. Basically deals with frequency governors. If temperature reaches a certain number then switch to powersave mode, or if a certain application is running, e.g. steam, or the laptop is running on batteries. If temperature reaches a lower point then it will switch to performance, etc.
The procedure runs very well so far, no issues. However the code aesthetically does not look good to me, because it has some many nested if-else statements, that when I add something new it is hard to follow, maintain and understand.
proc cpu_freq {} {
set app steam
set cpu_power [exec sudo cpupower frequency-info | sed -ne /speed/p]
set cpu_temp [exec sensors | grep Core | sed -n {2p} | awk {{print $3}} | cut -c2-3]
set battery [exec acpi]
if {[string match *performance* $cpu_power]} {set cpu_freq HIGH; set color "$::green"}
if {[string match *powersave* $cpu_power]} {set cpu_freq LOW; set color "$::red"}
if {![file isfile $::i3dir/powersave.freq] && ![file isfile $::i3dir/performance.freq]} {
set switch AUTO
}
# ON BATTERY
if {[string match *Discharging* $battery]} {
# WHEN IN PERFORMANCE MODE
if {[string match *performance* $cpu_power]} {
if {![file isfile $::i3dir/performance.freq]} {
# AND NOT IN MANUAL
# SWITCH TO POWERSAVE
exec sudo cpupower frequency-set -g powersave
set cpu_freq LOW
set switch AUTO
set color "$::red"
set ::on_battery true
} else {
# SWITCH TO MANUAL PERFORMANCE MODE
if {[file isfile $::i3dir/performance.freq]} {
exec sudo cpupower frequency-set -g performance
set cpu_freq HIGH
set switch MAN
set color "$::green"
set ::on_battery true
} else {
if {[file isfile $::i3dir/powersave.freq]} {
# SWITCH TO MANUAL POWERSAVE MODE
exec sudo cpupower frequency-set -g powersave
set cpu_freq LOW
set switch MAN
set color "$::red"
set ::on_battery true
}
}
}
} else {
# WHEN IN POWERSAVE MODE (AUTO)
# SWITCH TO MANUAL POWERSAVE
if {[string match *powersave* $cpu_power]} {
if {[file isfile $::i3dir/powersave.freq]} {
exec sudo cpupower frequency-set -g powersave
set cpu_freq LOW
set switch MAN
set color "$::red"
set ::on_battery true
} else {
# SWITCH TO MANUAL PERFORMANCE
if {[file isfile $::i3dir/performance.freq]} {
exec sudo cpupower frequency-set -g performance
set cpu_freq HIGH
set switch MAN
set color "$::green"
set ::on_battery true
}
}
}
}
# ON MAINS
} else {
# WHEN IN POWERSAVE MODE
if {[string match *powersave* $cpu_power]} {
# RUNNING APP OR MANUAL SWITCH
if {[file isfile $::i3dir/powersave.freq]} {
set cpu_freq LOW
set switch MAN
} else {
if {[isRunning $app]} {
set cpu_freq LOW
set switch AUTO
# DO NOTHING, KEEP RUNNING IN POWERSAVE MODE
} else {
# SWITCH TO PERFORMANCE AFTER RUNNING ON BATTERIES
if {$::on_battery==true} {
exec sudo cpupower frequency-set -g performance
set cpu_freq HIGH
set switch AUTO
set color "$::green"
set ::on_battery false
# SWITCH TO PERFORMANCE WHEN REACHING LOWER TEMPS
} elseif {$cpu_temp <= 55} {
exec sudo cpupower frequency-set -g performance
set cpu_freq HIGH
set switch AUTO
set color "$::green"
}
}
}
# WHEN IN PERFORMANCE MODE
} else {
# MANUAL SWITCH
if {[file isfile $::i3dir/performance.freq]} {
set switch MAN
set cpu_freq HIGH
# DO NOTHING, KEEP RUNNING IN PERFORMANCE MODE
} else {
# HOT TEMPERATURE OR RUNNING APP
# SWITCH TO POWERSAVE
if {$cpu_temp >= 75 || [isRunning $app] } {
exec sudo cpupower frequency-set -g powersave
set cpu_freq LOW
set switch AUTO
set color "$::red"
} else {
set cpu_freq HIGH
set switch AUTO
}
}
}
}
set stdout {{"name":"cpu_freq","full_text":"$switch:$cpu_freq","color":"$color"}}
set stdout [subst -nocommands $stdout]
puts -nonewline $stdout
}
Aucun commentaire:
Enregistrer un commentaire