Is there a way to update tmux window attributes based on command exit status? Similar to activity monitoring, I would like the title to change color when a command exits, say, green for success and red for failure. I've hacked something together using PROMPT_COMMAND (which goes in ~/.bashrc), but it's not entirely satisfactory. It doesn't play well with activity monitoring (i.e. the red/green can't be seen unless activity monitoring is disabled) and the color change is sticky; it maintains state after you visit the window instead of returning to the default like other tmux monitoring does.
function set_color_from_return_code {
local bg_color=$([ $? == 0 ] && echo "green" || echo "red")
tmux set-window-option -t${TMUX_PANE} window-status-bg $bg_color # &> /dev/null
}
PROMPT_COMMAND="set_color_from_return_code"
Edit: Specifically, I'm using tmux as the backend for byobu, so I'm adding the byobu tag, as a byobu-specific solution is fine by me.
Best Answer
.exit-monitor.sh
, then executechmod +x .exit-monitor.sh
. Write this to it:.bashrc
, add this:This will remove the script from PROMPT_COMMAND if it's already in there and re-add it to the beginning of the variable, but only if tmux is running. Otherwise you would keep adding the script to PROMPT_COMMAND over and over again, and you'd get annoying error messages if tmux isn't running.
Now, a little square in the left corner of your tmux status bar will show either green or red and the exit status number. If you want, you can also add text, e.g. FAIL/SUCCESS by using
or similar. Of course you can also change the position, e.g. use
status-right
. One important note is that.exit-monitor.sh
needs to be the first entry inPROMPT_COMMAND
for this to work and if you have any other tools writing the status text, those tools have to be able to append their status instead of replacing it.Very interesting idea by the way, I never thought of this, but will definitely keep this in my setup.