aboutsummaryrefslogtreecommitdiff
path: root/.config/eww/scripts/battery.sh
blob: a984346abed06ce55e866a89d4753c35e6406143 (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
46
47
48
49
50
51
52
53
#!/usr/bin/env bash

# Send a notification if battery is low, change status in info file accordingly
notify_low () {

  # Battery isn't much but is charging (don't notify)
  [ "$status" = "on-line" ] && return

  # Battery critically low
  if [ "$capacity" -le 10 ] && ! grep -q critically-low "$lowinfo"; then
    notify-send -i "$HOME/.config/icons/critical-battery.png" -u critical "Battery critically low!" 
    echo critically-low >"$lowinfo"

  # Battery is low 
  elif [ "$capacity" -le 20 ] && ! grep -q low "$lowinfo"; then
    notify-send -i "$HOME/.config/icons/low-battery.png" -t 5500 "Battery low!"
    echo low >"$lowinfo"
  fi
}

status="$(apm | grep -E "AC Line status:" | sed -n -e 's/^.*AC Line status: //p')"
capacity="$(apm | grep -E "Remaining battery life:" | head -1 | sed -n -e 's/^.*Remaining battery life: //p' | sed 's/.$//')"

# Get the low battery status from file ~/.cache/battery-low-status
lowinfo="$HOME/.cache/battery-low-status"
[ ! -e "$lowinfo" ] && touch "$lowinfo"


case $1 in
  left-click)
    ;;
  *)
    # Set charging icon and capacity icon
    [ "$status" = "on-line" ] && charging_icon=" " || charging_icon=""
    [ "$capacity" -gt 100 ] && capacity=100
    if   [ "$capacity" -lt 15 ]; then capacity_icon='  ' 
    elif [ "$capacity" -lt 40 ]; then capacity_icon='  '
    elif [ "$capacity" -lt 60 ]; then capacity_icon='  ' 
    elif [ "$capacity" -lt 90 ]; then capacity_icon='  '
    else capacity_icon='  ' 
    fi

    # Report low battery                                         
    [ "$capacity" -le 20 ] && notify_low

    # Reset low battery information in these cases
    { [ "$capacity" -gt 20 ] || [ "$status" = "Charging" ]; } && [ -n "$(cat "$lowinfo")" ] && echo "" >"$lowinfo"
	
    printf "%s%s%d%%\n" "$charging_icon" "$capacity_icon" "$capacity" 
    ;;
esac