about summary refs log tree commit diff stats
path: root/pkgs/by-name/br/brightness/brightness.sh
blob: d0cccfee8e446a6f3213d670339b460abc074e68 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env dash

# shellcheck source=/dev/null
SHELL_LIBRARY_VERSION="2.1.2" . %SHELL_LIBRARY_PATH

help() {
    cat <<EOF
This is a system brightness manager

USAGE:
    $NAME up [VALUE] | down [VALUE]

OPTIONS:
    --help   | -h
                                Output this help and exit.

    --version   | -v
                                Output the version and exit.

COMMANDS:
    up [VALUE]
                                Increase the brightness by VALUE or 5%.

    down [VALUE]
                                Decrease the brightness by VALUE or 5%.

ARGUMENTS:
    VALUE := [[seq 0 100]]
                                The amount to increase/decrease the brightness. In percentage
EOF
}

BACKLIGHT="/sys/class/backlight/%BACKLIGHT_NAME"

brightness() {
    offset="$1"

    max="$(cat $BACKLIGHT/max_brightness)"
    cur="$(cat $BACKLIGHT/brightness)"
    percentage="$(echo | awk --assign=cur="$cur" --assign=max="$max" '{printf cur / max}')"

    new="$(echo | awk --assign=per="$percentage" --assign=offset="$offset" '{printf per + (offset / 10)}')"

    output="$(echo | awk --assign=new="$new" --assign=max="$max" '{printf max * new}')"

    echo "$output" | sudo tee "$BACKLIGHT/brightness" >/dev/null
}

for arg in "$@"; do
    case "$arg" in
    "--help" | "-h")
        help
        exit 0
        ;;
    "--version" | "-v")
        version
        exit 0
        ;;
    esac
done

case "$1" in
"up")
    shift 1
    value="5"
    [ -n "$1" ] && value="$1"
    brightness "+$value"
    ;;
"down")
    shift 1
    value="-5"
    [ -n "$1" ] && value="$1"
    brightness "-$value"
    ;;
*)
    die "The command '$1' does not exist! See '--help' for a list"
    ;;
esac

# vim: ft=sh