From 62343c215771df9075b218c5a06bce48becd3a50 Mon Sep 17 00:00:00 2001 From: TrudeEH Date: Mon, 7 Apr 2025 11:05:18 +0100 Subject: [PATCH] Update scripts --- scripts/battery | 33 +++++++++++++++++++++++++++++++++ scripts/cpu | 39 +++++++++++++++++++++++++++------------ scripts/fetch | 3 ++- scripts/mem | 23 +++++++++++++++++++++-- scripts/temp | 1 - 5 files changed, 83 insertions(+), 16 deletions(-) create mode 100755 scripts/battery diff --git a/scripts/battery b/scripts/battery new file mode 100755 index 00000000..3a4d5d33 --- /dev/null +++ b/scripts/battery @@ -0,0 +1,33 @@ +#! /bin/bash + +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +MAGENTA="\e[35m" +CYAN="\e[36m" +BOLD="\e[1m" +ENDCOLOR="\e[0m" + +BATTERY_INFO=$(upower -i $(upower -e | grep 'BAT')) +BATTERY_PERCENT=$(echo "$BATTERY_INFO" | grep -oP 'percentage:\s+\K\d+') +BATTERY_STATUS=$(echo "$BATTERY_INFO" | grep -oP 'state:\s+\K\w+') +CHARGE_CYCLES=$(echo "$BATTERY_INFO" | grep -oP 'cycle-count:\s+\K\d+') +WARNING_LEVEL=$(echo "$BATTERY_INFO" | grep -oP 'warning-level:\s+\K\w+') + +if [ "$BATTERY_STATUS" == "charging" ] || [ "$BATTERY_STATUS" == "pending" ]; then + COLOR=$CYAN +elif [ "$BATTERY_PERCENT" -ge 80 ]; then + COLOR=$GREEN +elif [ "$BATTERY_PERCENT" -ge 30 ]; then + COLOR=$YELLOW +else + COLOR=$RED +fi + +echo -e "${BOLD}Battery: ${COLOR}$BATTERY_PERCENT% ($BATTERY_STATUS)${ENDCOLOR}" +if [ -n "$CHARGE_CYCLES" ]; then + echo -e "${BOLD}Charge Cycles: ${MAGENTA}$CHARGE_CYCLES${ENDCOLOR}" +fi +if [ "$WARNING_LEVEL" != "none" ]; then + echo -e "${BOLD}Warning Level: ${RED}$WARNING_LEVEL${ENDCOLOR}" +fi diff --git a/scripts/cpu b/scripts/cpu index 7147b6f7..585ad514 100755 --- a/scripts/cpu +++ b/scripts/cpu @@ -1,6 +1,12 @@ #!/bin/sh # Script to display CPU usage +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +CYAN="\e[36m" +ENDCOLOR="\e[0m" + # Cache in tmpfs to improve speed and reduce SSD load cache=/tmp/cpu-script-cache rm /tmp/cpu-script-cache 2>/dev/null @@ -8,21 +14,30 @@ while true; do sleep 1 cpu_stats=$(awk '/^cpu / {print $2 + $3 + $4 + $5, $5}' /proc/stat) if [ ! -f "$cache" ]; then - printf "  ---%% \r" + echo -n "  ${CYAN}--%${ENDCOLOR} \r" echo "$cpu_stats" >"$cache" continue fi prev_stats=$(cat "$cache") - echo "$cpu_stats $prev_stats" | awk '{ - total_diff = $1 - $3 - idle_diff = $2 - $4 - if (total_diff == 0) { - usage = 0 - } else { - usage = 100 * (1 - idle_diff / total_diff) - } - printf "  %.1f%% \r", usage - exit -}' + total=$(echo "$prev_stats" | awk '{print $1}') + prev_idle=$(echo "$prev_stats" | awk '{print $2}') + curr_total=$(echo "$cpu_stats" | awk '{print $1}') + curr_idle=$(echo "$cpu_stats" | awk '{print $2}') + total_diff=$((curr_total - total)) + idle_diff=$((curr_idle - prev_idle)) + + if [ "$total_diff" -eq 0 ]; then + usage=0 # Avoid division by zero + else + usage=$((100 * (total_diff - idle_diff) / total_diff)) + fi + + if [ "$usage" -lt 50 ]; then + echo -n "  ${GREEN}${usage}%${ENDCOLOR} \r" + elif [ "$usage" -lt 70 ]; then + echo -n "  ${YELLOW}${usage}%${ENDCOLOR} \r" + else + echo -n "  ${RED}${usage}%${ENDCOLOR} \r" + fi echo "$cpu_stats" >"$cache" done diff --git a/scripts/fetch b/scripts/fetch index ef848788..a3aea8db 100755 --- a/scripts/fetch +++ b/scripts/fetch @@ -20,7 +20,8 @@ echo -e "${CYAN}OS:${ENDCOLOR} $OS $ARCH" # Host Model HOST_MODEL=$(cat /sys/class/dmi/id/product_name 2>/dev/null) -echo -e "${CYAN}Host:${ENDCOLOR} ${HOST_MODEL}" +HOST_VERSION=$(cat /sys/class/dmi/id/product_version 2>/dev/null) +echo -e "${CYAN}Host:${ENDCOLOR} ${HOST_VERSION} (${HOST_MODEL})" # Kernel version echo -e "${CYAN}Kernel:${ENDCOLOR} Linux $(uname -r)" diff --git a/scripts/mem b/scripts/mem index 8e3e55ca..59e431fa 100755 --- a/scripts/mem +++ b/scripts/mem @@ -1,3 +1,22 @@ -#!/bin/sh +#!/bin/bash -free --mebi | sed -n '2{p;q}' | awk '{printf (" %.1f%%\n", ($4/$2)*100)}' +RED="\e[31m" +GREEN="\e[32m" +YELLOW="\e[33m" +ENDCOLOR="\e[0m" + +mem_info=$(free | awk '/Mem:/ {print $2, $3}') +read total used <<<"$mem_info" +percent=$(awk "BEGIN {printf \"%.0f\", ($used/$total)*100}") + +if [ "$percent" -le 60 ]; then + percent_color=$GREEN +elif [ "$percent" -le 80 ]; then + percent_color=$YELLOW +else + percent_color=$RED +fi + +mem_total=$(free -h | awk '/Mem:/ {print $2}') +mem_used=$(free -h | awk '/Mem:/ {print $3}') +echo -e " ${mem_used} / ${mem_total} (${percent_color}${percent}%${ENDCOLOR})" diff --git a/scripts/temp b/scripts/temp index 35db14b9..bef09812 100755 --- a/scripts/temp +++ b/scripts/temp @@ -1,4 +1,3 @@ #! /bin/bash paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | column -s $'\t' -t | sed 's/\(.\)..$/.\1°C/' -