diff --git a/Configs/.config/hypr/scripts/cpuinfo.sh b/Configs/.config/hypr/scripts/cpuinfo.sh index ac5e2d530e..7ebd4c4e78 100755 --- a/Configs/.config/hypr/scripts/cpuinfo.sh +++ b/Configs/.config/hypr/scripts/cpuinfo.sh @@ -1,35 +1,80 @@ -#!/usr/bin/env sh +#!/bin/bash -# CPU model -model=$(lscpu | awk -F ':' '/Model name/ {sub(/^ *| *$/,"",$2); print $2}' | awk '{NF-=3}1') -# CPU utilization -utilization=$(top -bn1 | awk '/^%Cpu/ {print 100 - $8}') +# Thee shalt find the greatest one, +# He who not more than the chosen one +map_floor() { -# Clock speed -freqlist=$(cat /proc/cpuinfo | grep "cpu MHz" | awk '{ print $4 }') -maxfreq=$(lscpu | grep "CPU max MHz" | awk -F: '{ print $2}' | sed -e 's/ //g' -e 's/\.[0-9]*//g') -frequency=$(echo $freqlist | tr ' ' '\n' | awk "{ sum+=\$1 } END {printf \"%.0f/$maxfreq MHz\", sum/NR}") + # From the depths of the string, words arise, + # Keys in pairs, a treasure in disguise. + IFS=', ' read -r -a pairs <<< "$1" -# CPU temp -temp=$(sensors | awk '/Package id 0/ {print $4}' | awk -F '[+.]' '{print $2}') -if [ -z "$temp" ]; then - temp=$(sensors | awk '/Tctl/ {print $2}' | tr -d '+°C') -fi -if [ -z "$temp" ]; then - temp="N/A" -fi + # If the final token stands alone and bold, + # Declare it the default, its worth untold. + if [[ ${pairs[-1]} != *":"* ]]; then + def_val="${pairs[-1]}" + unset 'pairs[${#pairs[@]}-1]' + fi + + # Scans the map, a peak it seeks, + # The highest passed, the value speaks. + for pair in "${pairs[@]}"; do + IFS=':' read -r key value <<< "$pair" + + # Behold! Thou holds the secrets they seek, + # Declare it and silence the whispers unique. + if [ ${2%%.*} -gt $key ]; then + echo "$value" + return + fi + done -# map icons -set_ico="{\"thermo\":{\"0\":\"\",\"45\":\"\",\"65\":\"\",\"85\":\"\"},\"emoji\":{\"0\":\"❄\",\"45\":\"☁\",\"65\":\"\",\"85\":\"\"},\"util\":{\"0\":\"󰾆\",\"30\":\"󰾅\",\"60\":\"󰓅\",\"90\":\"\"}}" -eval_ico() { - map_ico=$(echo "${set_ico}" | jq -r --arg aky "$1" --argjson avl "$2" '.[$aky] | keys_unsorted | map(tonumber) | map(select(. <= $avl)) | max') - echo "${set_ico}" | jq -r --arg aky "$1" --arg avl "$map_ico" '.[$aky] | .[$avl]' + # On this lonely shore, where silence dwells + # Even the waves, echoes words unheard + [ -n "$def_val" ] && echo $def_val || echo " " } -thermo=$(eval_ico thermo $temp) -emoji=$(eval_ico emoji $temp) -speedo=$(eval_ico util $utilization) +# Define glyphs +if [[ $NO_EMOJI -eq 1 ]]; then + temp_lv="85:, 65:, 45:☁, ❄" +else + temp_lv="85:🌋, 65:🔥, 45:☁️, ❄️" +fi +util_lv="90:, 60:󰓅, 30:󰾅, 󰾆" + +# Get static CPU information +model=$(lscpu | awk -F': ' '/Model name/ {gsub(/^ *| *$| CPU.*/,"",$2); print $2}') +maxfreq=$(lscpu | awk '/CPU max MHz/ { sub(/\..*/,"",$4); print $4}') + +# Get CPU stat +statFile=$(cat /proc/stat | head -1) +prevStat=$(awk '{print $2+$3+$4+$6+$7+$8 }' <<< $statFile) +prevIdle=$(awk '{print $5 }' <<< $statFile) + +while true; do + # Get CPU stat + statFile=$(cat /proc/stat | head -1) + currStat=$(awk '{print $2+$3+$4+$6+$7+$8 }' <<< $statFile) + currIdle=$(awk '{print $5 }' <<< $statFile) + diffStat=$((currStat-prevStat)) + diffIdle=$((currIdle-prevIdle)) + + # Get dynamic CPU information + utilization=$(awk -v stat="$diffStat" -v idle="$diffIdle" 'BEGIN {printf "%.1f", (stat/(stat+idle))*100}') + temperature=$(sensors | awk -F': ' '/Package id 0|Tctl/ { gsub(/^ *\+?|\..*/,"",$2); print $2; f=1; exit} END { if (!f) print "N/A"; }') + frequency=$(cat /proc/cpuinfo | awk '/cpu MHz/{ sum+=$4; c+=1 } END { printf "%.0f", sum/c }') + + # Generate glyphs + icons=$(echo "$(map_floor "$util_lv" $utilization)$(map_floor "$temp_lv" $temperature)") + speedo=$(echo ${icons:0:1}) + thermo=$(echo ${icons:1:1}) + emoji=$(echo ${icons:2}) + + # Print the output + echo "{\"text\":\"$thermo $temperature°C\", \"tooltip\":\"$model\n$thermo Temperature: $temperature°C $emoji\n$speedo Utilization: $utilization%\n Clock Speed: $frequency/$maxfreq MHz\"}" -# Print cpu info (json) -echo "{\"text\":\"${thermo} ${temp}°C\", \"tooltip\":\"${model}\n${thermo} Temperature: ${temp}°C ${emoji}\n${speedo} Utilization: ${utilization}%\n Clock Speed: ${frequency}\"}" + # Store state and sleep + prevStat=$currStat + prevIdle=$currIdle + sleep 5 +done diff --git a/Configs/.config/hypr/scripts/gpuinfo.sh b/Configs/.config/hypr/scripts/gpuinfo.sh index ba3ca10c4e..69ff9d5bd9 100755 --- a/Configs/.config/hypr/scripts/gpuinfo.sh +++ b/Configs/.config/hypr/scripts/gpuinfo.sh @@ -4,6 +4,8 @@ gpuQ="/tmp/hyprdots-${UID}-gpuinfo-query" tired=false [[ " $* " =~ " tired " ]] && ! grep -q "tired" "${gpuQ}" && echo "tired=true" >>"${gpuQ}" +[[ " $* " =~ " no_emoji " ]] && ! grep -q "NO_EMOJI" "${gpuQ}" && echo "NO_EMOJI=1" >>"${gpuQ}" + if [[ ! " $* " =~ " startup " ]]; then gpuQ="${gpuQ}$2" fi @@ -124,7 +126,7 @@ map_floor() { # Behold! Thou holds the secrets they seek, # Declare it and silence the whispers unique. - if awk -v num="$2" -v k="$key" 'BEGIN { exit !(num > k) }'; then + if [ ${2%%.*} -gt $key ]; then echo "$value" return fi @@ -135,26 +137,13 @@ map_floor() { [ -n "$def_val" ] && echo $def_val || echo " " } -# generate emoji and icon based on temperature and utilization -get_icons() { - # key-value pairs of temperature and utilization levels - temp_lv="85:&🌋, 65:&🔥, 45:&☁️, &❄️" - util_lv="90:, 60:󰓅, 30:󰾅, 󰾆" - - # return comma seperated emojis/icons - icons=$(map_floor "$temp_lv" $1 | sed "s/&/,/") - icons="$icons,$(map_floor "$util_lv" $2)" - echo $icons -} - generate_json() { - # get emoji and icon based on temperature and utilization - icons=$(get_icons "$temperature" "$utilization") - thermo=$(echo $icons | awk -F, '{print $1}') - emoji=$(echo $icons | awk -F, '{print $2}') - speedo=$(echo $icons | awk -F, '{print $3}') + # Generate glyphs + icons=$(echo "$(map_floor "$util_lv" $utilization)$(map_floor "$temp_lv" $temperature)") + speedo=$(echo ${icons:0:1}) + thermo=$(echo ${icons:1:1}) + emoji=$(echo ${icons:2}) - # emoji=$(get_temperature_emoji "${temperature}") local json="{\"text\":\"${thermo} ${temperature}°C\", \"tooltip\":\"${primary_gpu}\n${thermo} Temperature: ${temperature}°C ${emoji}" #? Soon Add Something incase needed. declare -A tooltip_parts @@ -272,6 +261,7 @@ Avalable GPU: ${gpu_flags//_flag/} [flags] tired * Adding this option will not query nvidia-smi if gpu is in suspend mode startup * Useful if you want a certain GPU to be set at startup +no_emoji * Use glyphs instead of emoji * If ${USER} declared env = WLR_DRM_DEVICES on hyprland then use this as the primary GPU EOF @@ -279,6 +269,14 @@ exit ;; esac +# Define glyphs +if [[ $NO_EMOJI -eq 1 ]]; then + temp_lv="85:, 65:, 45:☁, ❄" +else + temp_lv="85:🌋, 65:🔥, 45:☁️, ❄️" +fi +util_lv="90:, 60:󰓅, 30:󰾅, 󰾆" + nvidia_flag=${nvidia_flag:-0} intel_flag=${intel_flag:-0} amd_flag=${amd_flag:-0} #? Based on the flags, call the corresponding function multi flags means multi GPU. if [[ "${nvidia_flag}" -eq 1 ]]; then diff --git a/Configs/.config/waybar/modules/cpuinfo.jsonc b/Configs/.config/waybar/modules/cpuinfo.jsonc index 390931159a..04dab01acf 100644 --- a/Configs/.config/waybar/modules/cpuinfo.jsonc +++ b/Configs/.config/waybar/modules/cpuinfo.jsonc @@ -1,8 +1,8 @@ "custom/cpuinfo": { - "exec": " ~/.config/hypr/scripts/cpuinfo.sh", + "exec": "NO_EMOJI=1 ~/.config/hypr/scripts/cpuinfo.sh", "return-type": "json", "format": "{}", - "interval": 5, // once every 5 seconds + "restart-interval": 5, // once every 5 seconds "tooltip": true, "max-length": 1000 }, diff --git a/Configs/.config/waybar/modules/gpuinfo.jsonc b/Configs/.config/waybar/modules/gpuinfo.jsonc index a7fd82d10b..057427df68 100644 --- a/Configs/.config/waybar/modules/gpuinfo.jsonc +++ b/Configs/.config/waybar/modules/gpuinfo.jsonc @@ -1,5 +1,5 @@ "custom/gpuinfo": { - "exec": " ~/.config/hypr/scripts/gpuinfo.sh", + "exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh", "return-type": "json", "format": "{}", "interval": 5, // once every 5 seconds @@ -9,7 +9,7 @@ }, "custom/gpuinfo#nvidia": { - "exec": " ~/.config/hypr/scripts/gpuinfo.sh --use nvidia ", + "exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh --use nvidia ", "return-type": "json", "format": "{}", "interval": 5, // once every 5 seconds @@ -18,7 +18,7 @@ }, "custom/gpuinfo#amd": { - "exec": " ~/.config/hypr/scripts/gpuinfo.sh --use amd ", + "exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh --use amd ", "return-type": "json", "format": "{}", "interval": 5, // once every 5 seconds @@ -27,7 +27,7 @@ }, "custom/gpuinfo#intel": { - "exec": " ~/.config/hypr/scripts/gpuinfo.sh --use intel ", + "exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh --use intel ", "return-type": "json", "format": "{}", "interval": 5, // once every 5 seconds