Last active
July 23, 2024 08:43
-
-
Save sebastiancarlos/ca7124e33d4af1afcf3f29b01f5deadd to your computer and use it in GitHub Desktop.
Show notifications in your terminal
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
# All my gist code is licensed under the terms of the MIT license. | |
# Video demo: https://www.youtube.com/shorts/WVyqVkGYb4k | |
# Add this somewhere in ~/.bashrc | |
# write_message | |
# - write a message on the lower right corner of the terminal | |
function write_message () { | |
if [[ "$#" -eq 0 ]]; then | |
echo "write_message: please provide a message" | |
return | |
fi | |
local message=" ${*} " | |
# CSI sequences | |
local csi="\033[" | |
local save_cursor_position="${csi}s" | |
local restore_cursor_position="${csi}u" | |
local erase_from_cursor_to_end_of_screen="${csi}J" | |
local reset="${csi}0m" | |
local set_bg_color="${csi}44m" | |
local set_fg_color="${csi}30m" | |
local move_cursor_to_bottom="${csi}${LINES};$((COLUMNS - ${#message}))H" | |
# write the message | |
printf "${save_cursor_position}" | |
printf "${move_cursor_to_bottom}" | |
printf "${set_bg_color}${set_fg_color}${message}${reset}" | |
printf "${restore_cursor_position}" | |
# clean up after 5 seconds of timeout | |
function __clean_up () { | |
sleep 5 | |
printf "${save_cursor_position}" | |
printf "${move_cursor_to_bottom}" | |
printf "${erase_from_cursor_to_end_of_screen}" | |
printf "${restore_cursor_position}" | |
} | |
# clean up in the background | |
# run in a subshell to avoid showing 'job control' output | |
(__clean_up &) | |
# clean up internal functions | |
unset -f __clean_up | |
} | |
# sample function | |
# run this in the background with 'check_email &' | |
function check_email () { | |
sleep 10 | |
write_message "You have 69 new emails (total 420 unread)" | |
} |
I did not have luck running this, the popup appears for 0.1s and disappears, Love the idea, but couldn't fix it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Stewie410 Thanks a lot! I updated the code with your feedback.
A few notes:
$message
should be redefined in__write
because__write
doesn't always print the message passed towrite_message
: It also prints an empty message of the same length to clean up.function
keyword.local
keyword on the same line as the definition.tput
for a few reasons:tput
like library with extremely explicit names, but I don't think that exists right now, and in any case, variables are good enough for documenting.printf
s, but we are all sinners under the shadow of our lord Jesus Christ).Thanks again. I'm about to make a small improvement to optionally render a link that you can either click or open with a keybinding. It's surprisingly easy to do, and I wonder why no more people are exploiting these terminal shenanigans for custom hacks.
Edit: I simplified a bit more and managed to remove one internal function.