-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathhelpers.sh
More file actions
152 lines (134 loc) · 2.97 KB
/
helpers.sh
File metadata and controls
152 lines (134 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env bash
export LANG=C
export LC_ALL=C
get_tmux_option() {
local option
local default_value
local option_value
option="$1"
default_value="$2"
option_value="$(tmux show-option -qv "$option")"
if [ -z "$option_value" ]; then
option_value="$(tmux show-option -gqv "$option")"
fi
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
is_osx() {
[ "$(uname)" == "Darwin" ]
}
is_freebsd() {
[ "$(uname)" == "FreeBSD" ]
}
is_openbsd() {
[ "$(uname)" == "OpenBSD" ]
}
is_linux() {
[ "$(uname)" == "Linux" ]
}
is_cygwin() {
command -v WMIC &>/dev/null
}
is_linux_iostat() {
# Bug in early versions of linux iostat -V return error code
iostat -c &>/dev/null
}
# is second float bigger or equal?
fcomp() {
awk -v n1="$1" -v n2="$2" 'BEGIN {if (n1<=n2) exit 0; exit 1}'
}
load_status() {
local percentage=$1
local prefix=$2
medium_thresh=$(get_tmux_option "@${prefix}_medium_thresh" "30")
high_thresh=$(get_tmux_option "@${prefix}_high_thresh" "80")
if fcomp "$high_thresh" "$percentage"; then
echo "high"
elif fcomp "$medium_thresh" "$percentage" && fcomp "$percentage" "$high_thresh"; then
echo "medium"
else
echo "low"
fi
}
temp_status() {
local temp
temp=$1
cpu_temp_medium_thresh=$(get_tmux_option "@cpu_temp_medium_thresh" "80")
cpu_temp_high_thresh=$(get_tmux_option "@cpu_temp_high_thresh" "90")
if fcomp "$cpu_temp_high_thresh" "$temp"; then
echo "high"
elif fcomp "$cpu_temp_medium_thresh" "$temp" && fcomp "$temp" "$cpu_temp_high_thresh"; then
echo "medium"
else
echo "low"
fi
}
cpus_number() {
if is_linux; then
if command_exists "nproc"; then
nproc
else
echo "$(($(sed -n 's/^processor.*:\s*\([0-9]\+\)/\1/p' /proc/cpuinfo | tail -n 1) + 1))"
fi
else
sysctl -n hw.ncpu
fi
}
command_exists() {
local command
command="$1"
command -v "$command" &>/dev/null
}
get_tmp_dir() {
local tmpdir
tmpdir="${TMPDIR:-${TMP:-${TEMP:-/tmp}}}"
[ -d "$tmpdir" ] || local tmpdir=~/tmp
echo "$tmpdir/tmux-$EUID-cpu"
}
get_time() {
perl -MTime::HiRes=time -e 'printf "%.9f\n", time'
}
get_cache_val() {
local key
local timeout
local cache
key="$1"
# seconds after which cache is invalidated
timeout="${2:-2}"
cache="$(get_tmp_dir)/$key"
if [ -f "$cache" ]; then
awk -v cache="$(head -n1 "$cache")" -v timeout="$timeout" -v now="$(get_time)" \
'BEGIN {if (now - timeout < cache) exit 0; exit 1}' &&
tail -n+2 "$cache"
fi
}
put_cache_val() {
local key
local val
local tmpdir
key="$1"
val="${*:2}"
tmpdir="$(get_tmp_dir)"
[ ! -d "$tmpdir" ] && mkdir -p "$tmpdir" && chmod 0700 "$tmpdir"
(
get_time
echo -n "$val"
) >"$tmpdir/$key"
echo -n "$val"
}
cached_eval() {
local command
local key
local val
command="$1"
key="$(basename "$command")"
val="$(get_cache_val "$key")"
if [ -z "$val" ]; then
put_cache_val "$key" "$($command "${@:2}")"
else
echo -n "$val"
fi
}