-
Notifications
You must be signed in to change notification settings - Fork 3
/
battery.fish
130 lines (108 loc) · 2.84 KB
/
battery.fish
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
# NAME
# battery [arguments]
#
# SYNOPSIS
# OS X and Linux compatible battery utility.
#
# USAGE
# if available battery
# battery
# end
#
# ENV
# BATTERY_IS_PLUGGED
# BATTERY_IS_CHARGING
# BATTERY_TIME_LEFT Time left in `HH:MM` format.
# BATTERY_SLOTS Number of slots/gauges in base 10.
# BATTERY_MAX_CAP Battery maximum capacity.
# BATTERY_CUR_CAP Battery current capacity.
# BATTERY_PCT Current battery life in %.
#
# ARGUMENTS
# filled_slot_ch ▮
# empty_slot_ch ▯
# show_empty_slots true
function init --on-event init_battery
battery_update_info
end
function battery_update_info
if test $OSTYPE = "Darwin"
battery_update_info_darwin
else
battery_update_info_linux
end
end
function battery_update_info_linux
# TODO
end
function battery_update_info_darwin
set -l ioreg (ioreg -rc "AppleSmartBattery")
printf "%s"\n $ioreg \
| grep -q "\"ExternalConnected\" = Yes"
and set -g BATTERY_IS_PLUGGED
or set -e BATTERY_IS_PLUGGED
printf "%s"\n $ioreg \
| grep -q "\"IsCharging\" = Yes"
and set -g BATTERY_IS_CHARGING
or set -e BATTERY_IS_CHARGING
set -g BATTERY_MAX_CAP (printf "%s\n" $ioreg \
| grep "\"MaxCapacity\" =" \
| sed -e 's/^.*"MaxCapacity"\ =\ //')
set -g BATTERY_CUR_CAP (printf "%s\n" $ioreg \
| grep "\"CurrentCapacity\" =" \
| sed -e 's/^.*CurrentCapacity"\ =\ //')
set -g BATTERY_PCT (printf "%.1f" \
(echo "$BATTERY_CUR_CAP / $BATTERY_MAX_CAP * 100" | bc -l))
set -g BATTERY_TIME_LEFT (printf "%s\n" $ioreg \
| grep "\"AvgTimeToEmpty\" =" \
| sed -e 's/^.*"AvgTimeToEmpty"\ =\ //')
set BATTERY_TIME_LEFT (printf "%02d:%02d" \
(echo "$BATTERY_TIME_LEFT / 60" | bc) \
(echo "$BATTERY_TIME_LEFT % 60" | bc))
set -g BATTERY_SLOTS (math $BATTERY_PCT / 10)
end
function battery -a \
filled_slot_ch \
empty_slot_ch \
show_empty_slots \
red yellow green
if test $OSTYPE != "Darwin"
echo (set_color red)"Error: Battery package Linux support in progress."(set_color normal)
return 1
end
if test -z "$filled_slot_ch"
set filled_slot_ch ▮
end
if test -z "$empty_slot_ch"
set empty_slot_ch ▯
end
if test -z "$show_empty_slots"
set show_empty_slots true
end
if test -z "$red"
set red (set_color -o f00)
end
if test -z "$yellow"
set yellow (set_color -o ff0)
end
if test -z "$green"
set green (set_color -o 0f0)
end
set -l normal (set_color normal)
set -l color $green
switch $BATTERY_SLOTS
case 0 1 2; set color $red
case 3 4; set color $yellow
case "*"; set color $green
end
for n in (seq 10)
if test $n -le $BATTERY_SLOTS
printf "$color$filled_slot_ch$normal"
else
if test $show_empty_slots = true
printf "$color$empty_slot_ch$normal"
end
end
end
battery_update_info
end