Skip to content

Commit df07c02

Browse files
committed
Enhance CPU usage script to return usage percentage with corresponding icon; improve visual representation of CPU load
1 parent 6805f00 commit df07c02

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

_posts/2025-05-10-cpu-usage.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,23 @@ script: |
4949
usagePercent = 0;
5050
}
5151
52-
return usagePercent + '%';
52+
let icon;
53+
if (usagePercent < 33) {
54+
icon = 'gauge.with.dots.needle.0percent';
55+
} else if (usagePercent < 50) {
56+
icon = 'gauge.with.dots.needle.33percent';
57+
} else if (usagePercent < 67) {
58+
icon = 'gauge.with.dots.needle.50percent';
59+
} else if (usagePercent < 80) {
60+
icon = 'gauge.with.dots.needle.67percent';
61+
} else {
62+
icon = 'gauge.with.dots.needle.100percent';
63+
}
64+
65+
return { label: usagePercent + '%', icon };
5366
})();
5467
---
5568

56-
This script retrieves the current CPU usage (percent) of the system.
69+
This script retrieves the current CPU usage (percent) of the system by reading the `/proc/stat` file. It calculates the CPU usage based on the difference between the current and previous CPU times. The script uses a simple algorithm to determine the CPU usage percentage and returns it as a label with an appropriate icon.
70+
5771
Hope to be a cross-platform script in the future. Currently only Linux is supported.

0 commit comments

Comments
 (0)