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 hidden or 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)" | |
| } |
@Stewie410 Thanks a lot! I updated the code with your feedback.
A few notes:
- I believe
$messageshould be redefined in__writebecause__writedoesn't always print the message passed towrite_message: It also prints an empty message of the same length to clean up. - I like the
functionkeyword. - I also like the
localkeyword on the same line as the definition. - I personally don't like
tputfor 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
tputlike 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).
- 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
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.
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
As per my reddit comment, you could also use
printf&tputfor both more control and to make things a bit easier to understand.Additionally, lines 28-31 could probably just be
tputsequences.With that in mind, here's a "reimagined" version while leveraging
printf&tputin place of escape sequences:A few quick notes:
$messageshouldn't need to be defined in each sub-functionwrite_message()) defined the variable, internal functions should have access to the same variablesmain()calls_init_vars()or somethingunset -fis even needed for the internal functionsfunctionkeyword is neither POSIX compliant, nor necessary even in the context of bash$messageshould be defined with$*to expand args as a string (without word-splitting), rather than$@which is an array$message_length, though that's really just a matter of choicelocal/declare(unless read-only)sleep 10does not wait "1 minute" before continuing -- it waits 10 secondsLike the idea though -- would be interesting to see if there would be a way to do the same thing, but instead hook in to messages sent with
notify-send, for greater system-wide notification handling.