forked from matryer/xbar-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuptime.1m.sh
executable file
·78 lines (62 loc) · 1.87 KB
/
uptime.1m.sh
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
#!/bin/bash
# <xbar.title>uptime</xbar.title>
# <xbar.version>v1.2</xbar.version>
# <xbar.author>Matteo Ferrando</xbar.author>
# <xbar.author.github>chamini2</xbar.author.github>
# <xbar.desc>Show uptime command information.</xbar.desc>
# <xbar.image>http://i.imgur.com/qaIxpJN.png</xbar.image>
# the `sed` command removes the occasional leading whitespace
INFO=$(uptime | sed 's/^ *//g')
echo "$INFO" | awk -F'[ ,:\t\n]+' '
{
PLURAL = 1
VERBOSE = 0
SEP = ", "
DS = " day"
HS = " hr"
MS = " min"
SS = " sec"
################################
D = H = M = S = 0
if (substr($5,0,1) == "d") {
# up for a day or more
D = $4
P = $6
Q = $7
} else {
P = $4
Q = $5
}
if (int(Q) == 0) {
# words evaluate to zero
# exact times format, like `P = 55`, `Q = secs`
Q = substr(Q,0,1)
if (Q == "h") { H = P }
else if (Q == "m") { M = P }
else if (Q == "s") { S = P }
} else {
# hh:mm format, like `P = 4`, `Q = 20`
H = P
M = Q
}
MSG = "↑ " include(D, DS, SEP, PLURAL)
MSG = MSG include(H, HS, SEP, PLURAL, (D > 0 && VERBOSE))
MSG = MSG include(M, MS, SEP, PLURAL, (D > 0 && VERBOSE))
MSG = MSG include(S, SS, SEP, PLURAL)
# remove the remaining SEP
MSG = substr(MSG, 0, length(MSG) - length(SEP))
print "[", MSG, "] | size=12"
}
function include(VAL, UNIT, SUFFIX, PLURAL, VERBOSE) {
VAL = int(VAL)
if (PLURAL && VAL != 1) {
UNIT = UNIT"s"
}
if (VAL > 0 || VERBOSE) {
return (VAL UNIT SUFFIX)
} else {
# return ""
}
}'
echo "---"
echo "$INFO" | tr "," "\n" | tail -n 2