commit 8ddd293b102bbf48aadd834f5769d232f70bf881 Author: Alexander Rosenberg Date: Tue Sep 6 01:42:03 2022 -0700 Initial commit diff --git a/brightness-control.sh b/brightness-control.sh new file mode 100755 index 0000000..f6f6011 --- /dev/null +++ b/brightness-control.sh @@ -0,0 +1,65 @@ +#!/usr/bin/bash + +SAVE_FILE="${HOME}/.cache/brightness-control-cache" + +notify-brightness() { + brightness="$(brightnessctl -m | sed 's/,/ /g' | awk '{print $4}' | tr -d '\n' | tr -d '%')" + notify-send -h string:x-canonical-private-synchronous:brightness-control-sh -t 4000 -u normal -a "Brightness" -h int:value:"$brightness" "$brightness%" +} + +save-brightness() { + brightness="$(brightnessctl -m | sed 's/,/ /g' | awk '{print $4}' | tr -d '\n' | tr -d '%')" + printf '%d' "${brightness}" >"${SAVE_FILE}" +} + +restore-brightness() { + if [ ! -e "${SAVE_FILE}" ]; then + echo "brightness-control.sh: error: save file at \"${SAVE_FILE}\" not found" >&2 + exit 1 + fi + brightness="$(cat "${SAVE_FILE}")" + brightnessctl s "${brightness}%" +} + +print-usage() { + echo 'usage: brightness-control.sh [up|down|set|restore|display] ' 1>&2 + exit 1 +} + +if [ "$#" -lt 1 ]; then + print-usage +fi + +ammount='5%' +value='50%' +if [ "${#}" -gt 1 ]; then + ammount="${2}" + value="${2}" +fi + +case "${1}" in + 'up') + brightnessctl s "+${ammount}" > /dev/null + notify-brightness + save-brightness + ;; + 'down') + brightnessctl s "${ammount}-" > /dev/null + notify-brightness + save-brightness + ;; + 'set') + brightnessctl s "${value}" > /dev/null + notify-brightness + save-brightness + ;; + 'display') + notify-brightness + ;; + 'restore') + restore-brightness + ;; + *) + print-usage + ;; +esac