aboutsummaryrefslogtreecommitdiffstats
path: root/bin/get_battery_status
blob: 3597acbe4b3c3179eb4c6ca66cc2586bfa49bbaf (plain)
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

acpi_output=""
pmset_output=""
battery_number=0
battery_percentage=0
battery_remaining=""

if [ $(uname) == "Darwin" ]; then
  pmset_output="$(pmset -g batt| tail -n1)"
  battery_percentage="$(echo $pmset_output| cut -d';' -f1| cut -d' ' -f4)"
  battery_percentage=${battery_percentage%"%"}
  if [[ "$pmset_output" == *"discharging"* ]]; then
    battery_remaining="$(echo $pmset_output| cut -d';' -f3| cut -d' ' -f2)"
    echo "$battery_percentage% ($battery_remaining)"
  else
    echo "$battery_percentage%"
  fi
else
  if [ -x /usr/bin/acpi ]; then
    acpi_output=$(/usr/bin/acpi -b)
    battery_number=$(echo $acpi_output|cut -d':' -f1|cut -d' ' -f2)
    battery_state=$(echo $acpi_output|cut -d' ' -f3)
    battery_state=${battery_state%","}
    battery_percentage=$(echo $acpi_output|cut -d' ' -f4)
    battery_percentage=${battery_percentage%","}
    battery_percentage=${battery_percentage%"%"}
    if [ $battery_number -ge 0 ]; then
      if [ $battery_state = "Discharging" ]; then
        battery_remaining=$(echo $acpi_output|cut -d' ' -f5)
        echo "$battery_percentage% ($battery_remaining)"
      else
        echo "$battery_percentage%"
      fi
    fi
  fi
fi

exit 0