Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bin/omarchy-system-stats
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ case "${1:-}" in
;;
esac

cpu=$(top -bn1 | awk '/^%?Cpu/ {
gsub(/,/, "")
# The C locale keeps top's labels English and its commas mere separators, never "84,3" decimals.
# Commas become spaces because at full idle top writes "ni,100.0 id" with none.
cpu=$(LC_ALL=C top -bn1 | awk '/^%?Cpu/ {
gsub(/,/, " ")
for (i = 1; i <= NF; i++) {
if ($(i + 1) == "id") {
printf "%.0f%%", 100 - $i
Expand Down
34 changes: 34 additions & 0 deletions test/shell.d/system-stats-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
mkdir -p "$work/bin"

# top formats its summary in the caller's locale, as procps does: Spanish keeps
# the labels and writes decimal commas, German also translates "Cpu".
cat >"$work/bin/top" <<'SH'
#!/bin/bash
case ${LC_ALL:-} in
C) echo "%Cpu(s): 5.6 us, 3.4 sy, 0.0 ni,$IDLE id, 4.5 wa, 1.1 hi, 1.1 si, 0.0 st" ;;
es_ES.UTF-8) echo "%Cpu(s): 5,6 us, 3,4 sy, 0,0 ni,${IDLE/./,} id, 4,5 wa, 1,1 hi, 1,1 si, 0,0 st" ;;
*) echo "%CPU(s): 5,6 us, 3,4 sy, 0,0 ni,${IDLE/./,} id, 4,5 wa, 1,1 hi, 1,1 si, 0,0 st" ;;
esac
SH
chmod +x "$work/bin/top"

cpu() {
IDLE="$2" LC_ALL="$1" PATH="$work/bin:$PATH" "$ROOT/bin/omarchy-system-stats" | awk -F'\t' '$1 == "cpu" { print $2 }'
}

[[ $(cpu es_ES.UTF-8 " 84.3") == "16%" ]] || fail 'system stats reads CPU idle the same in a decimal-comma locale' "$(cpu es_ES.UTF-8 " 84.3")"
pass 'system stats reads CPU idle the same in a decimal-comma locale'

[[ $(cpu de_DE.UTF-8 " 84.3") == "16%" ]] || fail 'system stats reads CPU idle where top translates its labels' "$(cpu de_DE.UTF-8 " 84.3")"
pass 'system stats reads CPU idle where top translates its labels'

[[ $(cpu C "100.0") == "0%" ]] || fail 'system stats reads a fully idle CPU as idle' "$(cpu C "100.0")"
pass 'system stats reads a fully idle CPU as idle'