2 ms·
The article mentions not wanting to put a bell in their terminal prompt because it would cause too many notifications, but I found it difficult to remember to p
by wolfgang42 3y ago
The article mentions not wanting to put a bell in their terminal prompt because it would cause too many notifications, but I found it difficult to remember to put `;tput bel` on every command that would be slow, so I compromised by configuring my shell to notify only on commands that took longer than 10 seconds. I also use a popup instead of a straight bell so I'll see a reminder of what it was that was happening (this is for MacOS, on Linux it would use `notify-send` instead):
function preexec() {
lrn_timer=${timer:-$SECONDS}
lrn_command="$1"
}
lrn_timer=0
function precmd() {
[[ -v lrn_timer ]] || return # preexec() doesn't set timer if no command run (e.g. from ^C before running)
timer_show=$(($SECONDS - $lrn_timer))
unset lrn_timer
# 10 is the notification threshold in seconds
if (( ${timer_show} > 10 )); then
terminal-notifier -sound default -message "Exit $? in ${timer_show}s: $lrn_command"
fi
}
(Based on https://gist.github.com/petethepig/2d29e8b7e2ebc808bfe760b632608966 https://gist.github.com/petethepig/2d29e8b7e2ebc808bfe760b63...)
- mklein994 3y ago> [...] but I found it difficult to remember to put `;tput bel` on every command that would be slow [...] What I do in these situations (assuming the job is still in the foreground, and can be interrupted), is suspend the task with ctrl+z and resume with `fg`, chaining it with some other command: fg; notify-send "done" This uses the "job control" feature of `bash`, so it requires no extra setup. Your approach has the simple advantage however, that once it's set up, it just works, automatically.
- JNRowe 3y agoOne of the smart things with zsh is that the hooks¹ like precmd can be replaced with arrays of functions, so that you can cleanly have multiple hooks per hook point. It even provides a nice tool to work with the the arrays too², which makes editing/enabling/disabling far easier. It makes life far easier when you see people post useful snippets(like wolfgang42's), but don't want to have to mangle them in to your already messy monolithic function. ¹ https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-Functions https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-F... ² https://zsh.sourceforge.io/Doc/Release/User-Contributions.html#Manipulating-Hook-Functions https://zsh.sourceforge.io/Doc/Release/User-Contributions.ht...
- pmarreck 3y agoHow does this work without `trap` or `PROMPT_COMMAND`? (I think PROMPT_COMMAND will take an array of functions in later versions of Bash)
- wolfgang42 3y agozsh has some special handling if you define functions with magic names: https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-Functions https://zsh.sourceforge.io/Doc/Release/Functions.html#Hook-F... (I apparently knew this when I wrote the above code, since on closer inspection it doesn’t actually match the gist my comments claim it came from, but I have to admit I had since forgotten it.)
- pmarreck 3y agoRight, but I was asking about Bash, where this worked unchanged on my Mac... I do actually have something installed in my env, I think, that might trigger those functions automatically if they exist, now that I think about it... bash> v PROMPT_COMMAND PROMPT_COMMAND is an indexed array variable declare -a PROMPT_COMMAND=([0]=$'__bp_precmd_invoke_cmd\nmcfly_prompt_command;_direnv_hook\n:' [1]="__bp_interactive_mode") (pretty sure that this broke when I tried to force it to be a pure array instead of a 2 element array with the first as a concatenation of functions... unfortunately) ("v" is a "view/show" function I wrote that allows me to see the definition of anything; https://github.com/pmarreck/dotfiles/blob/80a392470f8eddd70ef072799b6812863513affa/bin/functions/show.sh#L72 https://github.com/pmarreck/dotfiles/blob/80a392470f8eddd70e... )
- pmarreck 3y agois it notify-send, or libnotify, or notify-desktop? (I'm on NixOS and only see the latter 2 options...)
- wolfgang42 3y agoOn Ubuntu, the command `notify-send` is provided by libnotify: $ dpkg-query -S "$(which notify-send)" libnotify-bin: /usr/bin/notify-send The README for https://github.com/nowrep/notify-desktop https://github.com/nowrep/notify-desktop says “It's basically clone of notify-send from libnotify” so I assume that would also work, but I don’t know your system.