Linux: Add CPU temperature

This commit is contained in:
Dylan Araps 2016-10-23 09:45:03 +11:00
parent 9838c7cc5b
commit 3ef6aeea42
4 changed files with 45 additions and 6 deletions

View File

@ -434,6 +434,9 @@ alias neofetch2="neofetch \
--cpu_cores type Whether or not to display the number of CPU cores --cpu_cores type Whether or not to display the number of CPU cores
Takes: logical, physical, off Takes: logical, physical, off
Note: 'physical' doesn't work on BSD. Note: 'physical' doesn't work on BSD.
--cpu_speed on/off Hide/Show cpu speed.
--cpu_temp on/off Hide/Show cpu temperature.
NOTE This only works on linux.
--distro_shorthand on/off Shorten the output of distro (tiny, on, off) --distro_shorthand on/off Shorten the output of distro (tiny, on, off)
NOTE: This is only possible on Linux, macOS, and Solaris NOTE: This is only possible on Linux, macOS, and Solaris
--kernel_shorthand on/off Shorten the output of kernel --kernel_shorthand on/off Shorten the output of kernel

View File

@ -106,6 +106,11 @@ cpu_shorthand="off"
# --cpu_display bar, infobar, barinfo, off # --cpu_display bar, infobar, barinfo, off
cpu_display="off" cpu_display="off"
# CPU Speed
# Hide/Show CPU speed.
# --cpu_speed on, off
cpu_speed="on"
# CPU Cores # CPU Cores
# Display CPU cores in output # Display CPU cores in output
# Logical: All virtual cores # Logical: All virtual cores
@ -114,6 +119,12 @@ cpu_display="off"
# Note: 'physical' doesn't work on BSD. # Note: 'physical' doesn't work on BSD.
cpu_cores="logical" cpu_cores="logical"
# CPU Temperature
# Hide/Show CPU temperature.
# --cpu_temp on, off
# Note: Only works on Linux.
cpu_temp="on"
# GPU # GPU

View File

@ -686,10 +686,11 @@ getcpu() {
"Linux" | "Windows") "Linux" | "Windows")
# Get cpu name # Get cpu name
cpu="$(awk -F ': | @' '/model name/ {printf $2; exit}' /proc/cpuinfo)" cpu="$(awk -F ': | @' '/model name/ {printf $2; exit}' /proc/cpuinfo)"
cpu_dir="/sys/devices/system/cpu/cpu0/cpufreq" speed_dir="/sys/devices/system/cpu/cpu0/cpufreq"
temp_dir="/sys/class/hwmon/hwmon0/temp1_input"
# Get cpu speed # Get cpu speed
if [ -d "$cpu_dir" ]; then if [ -d "$speed_dir" ]; then
case "$speed_type" in case "$speed_type" in
current) speed_type="scaling_cur_freq" ;; current) speed_type="scaling_cur_freq" ;;
min) speed_type="scaling_min_freq" ;; min) speed_type="scaling_min_freq" ;;
@ -701,8 +702,8 @@ getcpu() {
esac esac
# Fallback to cpuinfo_max_freq if $speed_type fails # Fallback to cpuinfo_max_freq if $speed_type fails
read -t 1 -r speed < "${cpu_dir}/${speed_type}" || \ read -t 1 -r speed < "${speed_dir}/${speed_type}" || \
read -t 1 -r speed < "${cpu_dir}/cpuinfo_max_freq" read -t 1 -r speed < "${speed_dir}/cpuinfo_max_freq"
speed="$((speed / 100000))" speed="$((speed / 100000))"
else else
@ -710,6 +711,13 @@ getcpu() {
speed="$((speed / 100))" speed="$((speed / 100))"
fi fi
# Get cpu temp
if [ "$cpu_temp" == "on" ] && [ -f "$temp_dir" ]; then
temp="$(< "$temp_dir")"
temp="$((temp * 100 / 10000))"
temp="[${temp/${temp: -1}}.${temp: -1}°C]"
fi
# Show/hide hyperthreaded cores # Show/hide hyperthreaded cores
case "$cpu_cores" in case "$cpu_cores" in
"logical" | "on") cores="$(grep -c ^processor /proc/cpuinfo)" ;; "logical" | "on") cores="$(grep -c ^processor /proc/cpuinfo)" ;;
@ -723,7 +731,7 @@ getcpu() {
speed="${speed:0:1}.${speed:1}" speed="${speed:0:1}.${speed:1}"
fi fi
cpu="$cpu @ ${speed}GHz" cpu="$cpu @ ${speed}GHz $temp"
;; ;;
"Mac OS X") "Mac OS X")
@ -914,6 +922,10 @@ getcpu() {
[ "$cpu_cores" != "off" ] && [ "$cores" ] && \ [ "$cpu_cores" != "off" ] && [ "$cores" ] && \
cpu="${cpu/@/(${cores}) @}" cpu="${cpu/@/(${cores}) @}"
# Remove speed from output
[ "$cpu_speed" == "off" ] && \
cpu="${cpu/@ *GHz}"
# Make the output of cpu shorter # Make the output of cpu shorter
case "$cpu_shorthand" in case "$cpu_shorthand" in
"name") cpu="${cpu/@*}" ;; "name") cpu="${cpu/@*}" ;;
@ -2991,7 +3003,10 @@ usage() { cat << EOF
Possible values: name, speed, tiny, on, off Possible values: name, speed, tiny, on, off
--cpu_cores type Whether or not to display the number of CPU cores --cpu_cores type Whether or not to display the number of CPU cores
Takes: logical, physical, off Takes: logical, physical, off
Note: 'physical' doesn't work on BSD. NOTE: 'physical' doesn't work on BSD.
--cpu_speed on/off Hide/Show cpu speed.
--cpu_temp on/off Hide/Show cpu temperature.
NOTE This only works on linux.
--distro_shorthand on/off Shorten the output of distro (tiny, on, off) --distro_shorthand on/off Shorten the output of distro (tiny, on, off)
NOTE: This is only possible on Linux, macOS, and Solaris NOTE: This is only possible on Linux, macOS, and Solaris
--kernel_shorthand on/off Shorten the output of kernel --kernel_shorthand on/off Shorten the output of kernel
@ -3119,6 +3134,8 @@ getargs() {
# Info # Info
--os_arch) os_arch="$2" ;; --os_arch) os_arch="$2" ;;
--cpu_cores) cpu_cores="$2" ;; --cpu_cores) cpu_cores="$2" ;;
--cpu_speed) cpu_speed="$2" ;;
--cpu_temp) cpu_temp="$2" ;;
--speed_type) speed_type="$2" ;; --speed_type) speed_type="$2" ;;
--distro_shorthand) distro_shorthand="$2" ;; --distro_shorthand) distro_shorthand="$2" ;;
--kernel_shorthand) kernel_shorthand="$2" ;; --kernel_shorthand) kernel_shorthand="$2" ;;

View File

@ -40,6 +40,14 @@ Whether or not to display the number of CPU cores
.br .br
Note: 'physical' doesn't work on BSD. Note: 'physical' doesn't work on BSD.
.TP .TP
.B \--cpu_speed 'on/off'
Hide/Show cpu speed.
.TP
.B \--cpu_temp 'on/off'
Hide/Show cpu temperature.
.br
NOTE This only works on linux.
.TP
.B \--distro_shorthand 'on/off' .B \--distro_shorthand 'on/off'
Shorten the output of distro (tiny, on, off) Shorten the output of distro (tiny, on, off)
.br .br