-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundpeats.zsh
51 lines (39 loc) · 1.24 KB
/
soundpeats.zsh
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
function get_battery_level() {
local output
output=$(dbus-send --session --dest=tn.aziz.soundpeats.BLEService --print-reply /tn/aziz/soundpeats/BLEService tn.aziz.soundpeats.BLEService.GetBatteryLevel 2>&1)
if [[ $output == *"Not connected"* ]]; then
echo "disconnected"
return
fi
local left
local right
left=$(echo "$output" | awk '/string "left"/ {getline; print $3}')
right=$(echo "$output" | awk '/string "right"/ {getline; print $3}')
echo "L: $left, R: $right"
}
function get_cached_battery_level() {
local cache_file="/tmp/headset_battery_level"
local cache_duration=60 # Cache duration in seconds (1 minute)
local current_time
current_time=$(date +%s)
if [[ -f $cache_file ]]; then
local cache_time
cache_time=$(stat -c %Y "$cache_file")
local time_diff=$((current_time - cache_time))
if (( time_diff < cache_duration )); then
cat "$cache_file"
return
fi
fi
local battery_level
battery_level=$(get_battery_level)
echo "$battery_level" > "$cache_file"
echo "$battery_level"
}
alias bat=get_battery_level
# p10k.zsh
function prompt_soundpeats() {
local battery_level
battery_level=$(get_cached_battery_level)
p10k segment -f 208 -i '🎧' -t "${battery_level}"
}