summaryrefslogtreecommitdiff
path: root/battery-status.sh
blob: 370d5fe72f1d6d971ac4ee7ef8f9c5393a1471ec (plain) (blame)
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
#!/bin/bash
#
# Print current battery status.  Can be used in genmon xfce plugin.

print_sepparator=false

print_status() {
  [ $print_sepparator = true ] && printf ' '

  case "$status" in
  "Full" | "Not charging")
    return
    ;;
  "Discharging")
    status="↓"
    ;;
  "Charging")
    status="↑"
    ;;
  "Unknown")
    # Probably rich the threshold
    if [ "$(echo "$current_charge > 50" | bc -l)" == 1 ]; then
      return
    fi
    status="?"
    ;;
  *)
    status="?"
    ;;
  esac

  printf '%s %.0f%s' "$(basename "$bat")" "$current_charge" "$status"

  print_sepparator=true
}

for bat in /sys/class/power_supply/BAT?; do
  status=$(cat "$bat/status")
  energy_full=$(cat "$bat/energy_full")
  energy_now=$(cat "$bat/energy_now")
  current_charge=$(bc <<<"scale=2; $energy_now / $energy_full * 100")

  print_status
done
echo