Last active
August 8, 2023 07:13
-
-
Save Grub4K/e25776975b105e56357ad20a70b77437 to your computer and use it in GitHub Desktop.
Small bash notification panel helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
show_help() { | |
cat << EOF | |
${1} | |
USAGE: | |
snotify log [MODULE] MESSAGE | |
Log a message to the notifications panel | |
snotify enable [HEIGHT] | |
Enable notification panel with height (default: 1) | |
snotify disable | |
Disable notifications panel | |
snotify -h | --help | |
Show this help message | |
EOF | |
} | |
log() { | |
local message module | |
if [[ -z "${1}" ]]; then | |
printf 'No notification text specified\n' >&2 | |
return 1 | |
fi | |
if [[ -z "${2}" ]]; then | |
module="snotify" | |
message="${1}" | |
else | |
module="${1}" | |
message="${2}" | |
fi | |
printf $'\e7\e[1;%dr\e[%dH\e[1S [%s] [%-20s] %s\e[%dr\e8' \ | |
"${SNOTIFY_LINES}" "${SNOTIFY_LINES}" \ | |
"$(date '+%F %T')" "${module}" "${message}" \ | |
"${SNOTIFY_RESERVED}" | |
} | |
enable() { | |
local cols sep | |
cols="$(tput cols)" | |
sep="$(printf '%*s' "$(((cols - 15) / 2))" '' | tr ' ' '-')" | |
export SNOTIFY_LINES="${1}" | |
export SNOTIFY_RESERVED="$((${1} + 2))" | |
printf $'\e[?1049h\e[%dr\e[%dH\e[B%s Notifications %s\n' \ | |
"${SNOTIFY_RESERVED}" "${SNOTIFY_LINES}" "${sep}" "${sep}" | |
} | |
disable() { | |
printf $'\e[?1049l' | |
export SNOTIFY_LINES="" | |
export SNOTIFY_RESERVED="" | |
} | |
main() { | |
local opt | |
for opt in "${@}"; do | |
if [[ "${opt}" =~ ^-(h|-help)$ ]]; then | |
show_help "Notification pane in your terminal!" | |
return | |
fi | |
done | |
case "${1}" in | |
enable ) | |
enable "${2:-1}"; return;; | |
disable ) | |
disable; return;; | |
log ) | |
log "${2}" "${3}"; return;; | |
* ) | |
show_help "Invalid subcommand: ${1}" >&2; return 1;; | |
esac | |
} | |
main "${@}" | |
unset -f main enable disable log show_help |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment