Last active
June 7, 2024 08:07
-
-
Save ilukashin/6cbd6f3e531c6a9ab69f49207714b791 to your computer and use it in GitHub Desktop.
MacOS zsh terminal notification hooks for long executed operations
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
| # here i am using terminal-notifier | |
| # https://github.com/julienXX/terminal-notifier | |
| # you can use your own solution | |
| # main setting stored in variable | |
| # local execution_treshold=30 | |
| # if your process executed more than 30 seconds | |
| # you will get notified | |
| function sysnotify() { | |
| terminal-notifier -group 'background' -title 'Background process' -message '✅' -subtitle 'finished' -sound glass | |
| } | |
| # definition of zsh hook | |
| function preexec() { | |
| cmd_execution_begin_time=$(date +%s) | |
| } | |
| # definition of zsh hook | |
| function precmd() { | |
| # alternative to cmd_execution_end_time | |
| local cmd_new_prompt_time=$(date +%s) | |
| # seconds | |
| local execution_treshold=30 | |
| # check time exists for cmd initialization | |
| if [[ -v cmd_execution_begin_time ]]; then | |
| else | |
| cmd_execution_begin_time=cmd_new_prompt_time | |
| fi | |
| local last_execution_time=$((cmd_new_prompt_time - cmd_execution_begin_time)) | |
| if (($last_execution_time > $execution_treshold)) | |
| then | |
| sysnotify | |
| fi | |
| } |
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
| # i prefer to store configs in separated files | |
| # so add this line in your .zshrc | |
| source ~/.hooksrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment