Skip to content

Instantly share code, notes, and snippets.

@sebastiancarlos
Last active July 23, 2024 08:43
Show Gist options
  • Save sebastiancarlos/ca7124e33d4af1afcf3f29b01f5deadd to your computer and use it in GitHub Desktop.
Save sebastiancarlos/ca7124e33d4af1afcf3f29b01f5deadd to your computer and use it in GitHub Desktop.
Show notifications in your terminal
# 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)"
}
@sebastiancarlos
Copy link
Author

sebastiancarlos commented Aug 6, 2023

@Stewie410 Thanks a lot! I updated the code with your feedback.

A few notes:

  • I believe $message should be redefined in __write because __write doesn't always print the message passed to write_message: It also prints an empty message of the same length to clean up.
  • I like the function keyword.
  • I also like the local keyword on the same line as the definition.
  • I personally don't like tput for a few reasons:
    • I think it's a historical accident from the pre-xterm days. Now that terminal emulator compatibility is better, there's no need to avoid writing escape sequences by hand. I would take a 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.
    • It starts a new process just for writing strings (this is hypocritical of me because I don't mind having 5 consecutive printfs, 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.

@adriangalilea
Copy link

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