random-scripts/brightness-control.sh

74 lines
1.6 KiB
Bash
Raw Permalink Normal View History

2022-09-06 01:42:03 -07:00
#!/usr/bin/bash
SAVE_FILE="${HOME}/.cache/brightness-control-cache"
2024-08-24 16:49:55 -07:00
let really_notify=1
2022-09-06 01:42:03 -07:00
notify-brightness() {
2024-08-24 16:49:55 -07:00
((${really_notify})) || return
2022-09-06 01:42:03 -07:00
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] <ammount>' 1>&2
exit 1
}
if [ "$#" -lt 1 ]; then
print-usage
fi
2024-08-24 16:49:55 -07:00
if [[ "${1}" == -n ]]; then
really_notify=0
shift 1
fi
2022-09-06 01:42:03 -07:00
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