aboutsummaryrefslogtreecommitdiff
path: root/scripts/battery.nix
blob: 87b6cffdfc7e308410ae7cc3431731768873411c (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{ pkgs, ... }:

let
  name = "bat";
  symbol = "·";
in
pkgs.writeScriptBin name
  ''
    red="\e[31m"
    grn="\e[32m"
    ylw="\e[33m"
    cyn="\e[36m"
    blu="\e[34m"
    prp="\e[35m"
    bprp="\e[35;1m"
    rst="\e[0m"

    bat_status=""
    bat_status=$( cat /sys/class/power_supply/BAT0/capacity )
    charging_status=$( cat /sys/class/power_supply/BAT0/status )

    health() {
    for i in {0..4}
    do
        if [[ $i -le $(( $bat_status/20 )) ]]; then
            echo -ne "#[fg=colour1]${symbol} "
        else
            echo -ne "#[fg=colour8]${symbol} "
        fi
    done
    echo
    }

    bat_status_small() {
    if [[ "$charging_status" = *Charging* ]]; then
        echo -ne "+$bat_status%"
    else
        echo -ne "$bat_status%"
    fi
    }

    [ -z "$1" ] && {
    bat_status_small
    }

    while getopts qi options 
    do
    case $options in
        i)
            bat_status_small
            ;;
        q)
            if [[ "$charging_status" = *Charging* ]]; then
                echo -ne "+ $(health)"
            else
                health
            fi
    esac
    done
  ''