Skip to content

Instantly share code, notes, and snippets.

@rafpaf
Created February 5, 2024 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafpaf/26e65bea887693fdd1547c265e4e4d92 to your computer and use it in GitHub Desktop.
Save rafpaf/26e65bea887693fdd1547c265e4e4d92 to your computer and use it in GitHub Desktop.
# I haven't battle-tested this but it seems to work OK.
#!/usr/bin/env zsh
# If an alert-triggering string appears in the running output of a command,
# show an alert in the tmux window title.
#
# Useful for getting immediate feedback that a command like rollup -w (i.e.
# rollup watch) has noticed an error in your js
# Initialize variables
command=""
alert_string="(!)"
clear_string="Build succeeded"
alert_icon="🚨"
# Function to show help message
print_help() {
echo "Usage: ${0} -c <command> [-a <alert_string>] [-r <clear_string>] [-i <alert_icon>]"
echo
echo " -c Specify the command to execute."
echo " -a Define the string whose presence triggers the alert (default is '(!)')."
echo " -r Define the string whose presence clears the alert (default is 'Build succeeded')."
echo " -i Set the icon to use for alert (default is '🫤')."
echo " -h Show this help message."
}
# Parse command line options
while getopts ":c:a:r:i:h" opt; do
case ${opt} in
c)
command="${OPTARG}"
;;
a)
alert_string="${OPTARG}"
;;
r)
clear_string="${OPTARG}"
;;
i)
alert_icon="${OPTARG}"
;;
h)
print_help
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
print_help
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
if [[ -z "$command" ]]; then
echo "The -c <command> flag is required."
exit 1
fi
get_title() {
tmux display-message -p '#W'
}
# The id of the current tmux window, where the command is executed from
CURRENT_TMUX_WINDOW=`tmux display-message -p -t "${TMUX_PANE}" '#{window_index}'`
ORIGINAL_TITLE=`get_title`
rename() {
tmux rename-window -t $CURRENT_TMUX_WINDOW "$1"
}
# Run command and capture the output
eval $command 2>&1 | while read -r line; do
echo "$line"
if echo "$line" | grep -iq "$alert_string"; then
rename "$ORIGINAL_TITLE 🫤"
fi
if echo "$line" | grep -iq $clear_string; then
rename $ORIGINAL_TITLE
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment