~tardypad/dotfiles

ref: 7dd5e02494e2821ff6b4fd5758a590ef03b5403b dotfiles/packages/i3blocks/scripts/power -rwxr-xr-x 1.4 KiB
7dd5e024 — Damien Tardy-Panis add mako package config 3 years ago
                                                                                
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
#! /usr/bin/env zsh

IS_ADAPTER_ON=$( cat /sys/class/power_supply/AC/online )
IS_ANY_BATTERY_CHARGING=0
TOTAL_ENERGY_NOW=0
TOTAL_ENERGY_FULL=0

foreach bat in /sys/class/power_supply/BAT*; do
  (( TOTAL_ENERGY_NOW += $( cat $bat/energy_now ) ))
  (( TOTAL_ENERGY_FULL += $( cat $bat/energy_full ) ))

  if [[ $( cat $bat/status ) == 'Charging' ]]; then
    IS_ANY_BATTERY_CHARGING=1
  fi
done


TOTAL_ENERGY_PERCENT=$(
  printf %.0f $(
    echo "( ${TOTAL_ENERGY_NOW} / ${TOTAL_ENERGY_FULL} ) * 100" \
      | bc -l
  )
)

if [[ "${TOTAL_ENERGY_PERCENT}" -lt 10 ]]; then
  BATTERY_ICON=''
elif [[ "${TOTAL_ENERGY_PERCENT}" -lt 40 ]]; then
  BATTERY_ICON=''
elif [[ "${TOTAL_ENERGY_PERCENT}" -lt 60 ]]; then
  BATTERY_ICON=''
elif [[ "${TOTAL_ENERGY_PERCENT}" -lt 90 ]]; then
  BATTERY_ICON=''
else
  BATTERY_ICON=''
fi

if [[ "${IS_ADAPTER_ON}" -eq 1 ]]; then
  if [[ "${IS_ANY_BATTERY_CHARGING}" -eq 1 ]]; then
    echo "<span foreground='#{color/base0A/hex}'> ${BATTERY_ICON}</span> ${TOTAL_ENERGY_PERCENT}%"
  else
    echo "<span foreground='#{color/base0A/hex}'></span> AC"
  fi
else
  if [[ "${TOTAL_ENERGY_PERCENT}" -lt 10 ]]; then
    echo "<span foreground='#{color/alert/hex}'>${BATTERY_ICON} ${TOTAL_ENERGY_PERCENT}%</span>"
    notify_low_battery
    exit 33
  else
    echo "<span foreground='#{color/base0A/hex}'>${BATTERY_ICON}</span> ${TOTAL_ENERGY_PERCENT}%"
  fi
fi