cpu_info.sh
1 #!/usr/bin/env bash 2 # setting the locale, some users have issues with different locales, this forces the correct one 3 export LC_ALL=en_US.UTF-8 4 5 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 6 source $current_dir/utils.sh 7 8 get_percent() 9 { 10 case $(uname -s) in 11 Linux) 12 percent=$(LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "Cpu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}') 13 normalize_percent_len $percent 14 ;; 15 16 Darwin) 17 cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}') 18 cpucores=$(sysctl -n hw.logicalcpu) 19 cpuusage=$(( cpuvalue / cpucores )) 20 percent="$cpuusage%" 21 normalize_percent_len $percent 22 ;; 23 24 OpenBSD) 25 cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}') 26 cpucores=$(sysctl -n hw.ncpuonline) 27 cpuusage=$(( cpuvalue / cpucores )) 28 percent="$cpuusage%" 29 normalize_percent_len $percent 30 ;; 31 32 CYGWIN*|MINGW32*|MSYS*|MINGW*) 33 # TODO - windows compatability 34 ;; 35 esac 36 } 37 38 get_load() { 39 case $(uname -s) in 40 Linux | Darwin | OpenBSD) 41 loadavg=$(uptime | awk -F'[a-z]:' '{ print $2}' | sed 's/,//g') 42 echo $loadavg 43 ;; 44 45 CYGWIN* | MINGW32* | MSYS* | MINGW*) 46 # TODO - windows compatability 47 ;; 48 esac 49 } 50 51 main() { 52 # storing the refresh rate in the variable RATE, default is 5 53 RATE=$(get_tmux_option "@dracula-refresh-rate" 5) 54 cpu_load=$(get_tmux_option "@dracula-cpu-display-load" false) 55 cpu_label=$(get_tmux_option "@dracula-cpu-usage-label" "CPU") 56 if [ "$cpu_load" = true ]; then 57 echo "$cpu_label $(get_load)" 58 else 59 cpu_percent=$(get_percent) 60 echo "$cpu_label $cpu_percent" 61 fi 62 sleep $RATE 63 } 64 65 # run main driver 66 main