Skip to content

Commit 3a4a688

Browse files
authored
Merge pull request #497 from iyegoroff/cpu_temp_no_decimal
2 parents 6c40c04 + 30873ba commit 3a4a688

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

lib/cpu_temp_helper.sh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
# shellcheck shell=bash
22

33
tp_cpu_temp_value() {
4+
local temp
5+
46
if tp_shell_is_macos; then
5-
smctemp -c | bc -l
7+
temp=$(smctemp -c | bc -l)
68
elif tp_shell_is_linux; then
7-
sensors \
9+
temp=$(sensors \
810
| grep "$TMUX_POWERLINE_SEG_CPU_TEMP_SENSORS_LINE_MARKER" -m 1 \
9-
| sed -e 's/[^+]*+\([\s0-9\.]*\).*/\1/' | bc -l
11+
| sed -e 's/[^+]*+\([\s0-9\.]*\).*/\1/' | bc -l)
1012
fi
13+
14+
tp_round "$temp" 0
1115
}
1216

1317
tp_cpu_temp_at_least() {
14-
echo "$(tp_cpu_temp_value) >= $1" | bc -l
18+
local threshold_temp="$1"
19+
20+
echo "$(tp_cpu_temp_value) >= $threshold_temp" | bc -l
1521
}
1622

lib/mem_used_helper.sh

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ __tp_mem_used_info() {
4747

4848
tp_mem_used_gigabytes() {
4949
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
50-
__round "$(echo "$mem_used_bytes / 1073741824" | bc -l)" 2
50+
tp_round "$(echo "$mem_used_bytes / 1073741824" | bc -l)" 2
5151
}
5252

5353
tp_mem_used_megabytes() {
5454
read -r mem_used_bytes mem_total_bytes < <(__tp_mem_used_info)
55-
__round "$(echo "$mem_used_bytes / 1048576" | bc -l)" 0
55+
tp_round "$(echo "$mem_used_bytes / 1048576" | bc -l)" 0
5656
}
5757

5858
tp_mem_used_percentage_at_least() {
@@ -61,13 +61,3 @@ tp_mem_used_percentage_at_least() {
6161
echo "($mem_used_bytes / $mem_total_bytes) * 100 >= $threshold_percentage" | bc -l
6262
}
6363

64-
# source https://askubuntu.com/a/179949
65-
# Rounds positive numbers up to the number of digits to the right of the decimal point.
66-
# Example: "__round 1.2345 3" -> "((1000 * 1.2345) + 0.5) / 1000" -> "1.235"
67-
__round() {
68-
local number="$1"
69-
local digits="$2"
70-
71-
env printf "%.${digits}f" "$(echo "scale=${digits};(((10^${digits})*${number})+0.5)/(10^${digits})" | bc)"
72-
};
73-

lib/util.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,14 @@ tp_err_seg() {
6666
# TMUX_POWERLINE_CUR_SEGMENT_NAME is being set before each segment run
6767
tp_err "${TMUX_POWERLINE_CUR_SEGMENT_NAME:-unknown_segment}" "$*"
6868
}
69+
70+
# source https://askubuntu.com/a/179949
71+
# Rounds positive numbers up to the number of digits to the right of the decimal point.
72+
# Example: "tp_round 1.2345 3" -> "((1000 * 1.2345) + 0.5) / 1000" -> "1.235"
73+
tp_round() {
74+
local number="$1"
75+
local digits="$2"
76+
77+
env printf "%.${digits}f" "$(echo "scale=${digits};(((10^${digits})*${number})+0.5)/(10^${digits})" | bc)"
78+
};
79+

0 commit comments

Comments
 (0)