Skip to content

Commit dd05578

Browse files
committed
Implement colour and view template support for CPU counter
1 parent db9d1fb commit dd05578

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

scripts/cpu.sh

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,56 @@ source "$CURRENT_DIR/helpers.sh"
88

99
refresh_interval=$(get_tmux_option "status-interval" "5")
1010

11-
print_cpu_usage() {
11+
cpu_view_tmpl=$(get_tmux_option "@sysstat_cpu_view_tmpl" '#[fg=#{cpu.color}]#{cpu.pused}#[default]')
12+
13+
cpu_medium_threshold=$(get_tmux_option "@sysstat_cpu_medium_threshold" "30")
14+
cpu_high_threshold=$(get_tmux_option "@sysstat_cpu_high_threshold" "80")
15+
16+
cpu_color_low=$(get_tmux_option "@sysstat_cpu_color_low" "green")
17+
cpu_color_medium=$(get_tmux_option "@sysstat_cpu_color_medium" "yellow")
18+
cpu_color_high=$(get_tmux_option "@sysstat_cpu_color_high" "red")
19+
20+
get_cpu_color(){
21+
local cpu_used=$1
22+
23+
if fcomp "$cpu_high_threshold" "$cpu_used"; then
24+
echo "$cpu_color_high";
25+
elif fcomp "$cpu_medium_threshold" "$cpu_used"; then
26+
echo "$cpu_color_medium";
27+
else
28+
echo "$cpu_color_low";
29+
fi
30+
}
31+
32+
get_cpu_usage() {
1233
if is_osx; then
1334
if command_exists "iostat"; then
14-
iostat -c 2 -w "$refresh_interval" | tail -n 1 | awk '{ printf "%.1f%%", 100-$6 }'
35+
iostat -c 2 -w "$refresh_interval" | tail -n 1 | awk '{ print 100-$6 }'
1536
else
16-
top -l 2 -s "$refresh_interval" -n 0 | sed -nr '/CPU usage/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)%[[:space:]]*idle.*/\1/p' | tail -n 1 | awk '{ printf "%.1f%%", 100-$0 }'
37+
top -l 2 -s "$refresh_interval" -n 0 | sed -nr '/CPU usage/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)%[[:space:]]*idle.*/\1/p' | tail -n 1 | awk '{ print 100-$0 }'
1738
fi
1839
else
1940
if command_exists "vmstat"; then
20-
vmstat -n "$refresh_interval" 2 | tail -n 1 | awk '{printf "%.1f%%", 100-$(NF-2)}'
41+
vmstat -n "$refresh_interval" 2 | tail -n 1 | awk '{print 100-$(NF-2)}'
2142
else
22-
top -b -n 2 -d "$refresh_interval" | sed -nr '/%Cpu/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)[[:space:]]*id.*/\1/p' | tail -n 1 | awk '{ printf "%.1f%%", 100-$0 }'
43+
top -b -n 2 -d "$refresh_interval" | sed -nr '/%Cpu/s/.*,[[:space:]]*([0-9]+[.,][0-9]*)[[:space:]]*id.*/\1/p' | tail -n 1 | awk '{ print 100-$0 }'
2344
fi
2445
fi
2546
}
2647

48+
print_cpu_usage() {
49+
local cpu_pused=$(get_cpu_usage)
50+
51+
local cpu_color=$(get_cpu_color "$cpu_pused")
52+
53+
local cpu_view="$cpu_view_tmpl"
54+
cpu_view="${cpu_view//'#{cpu.pused}'/$(printf "%.1f%%" "$cpu_pused")}"
55+
cpu_view="${cpu_view//'#{cpu.color}'/$cpu_color}"
56+
57+
echo $cpu_view
58+
}
59+
2760
main(){
28-
echo "$(date)" >> ~/.tmux/sysstat_cpu.log
2961
print_cpu_usage
3062
}
3163

scripts/mem.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
# TODO: add option to choose size unit: M|G
4+
35
set -u
46
set -e
57

0 commit comments

Comments
 (0)