Skip to content

Instantly share code, notes, and snippets.

@mathandy
Last active August 11, 2021 04:15
Show Gist options
  • Save mathandy/5d579a77cc6bbf11638bbf0dda87f87c to your computer and use it in GitHub Desktop.
Save mathandy/5d579a77cc6bbf11638bbf0dda87f87c to your computer and use it in GitHub Desktop.
script to make terminal flash until any key is pressed (works inside tmux also)
#!/bin/bash
# script to make terminal flash until any key is pressed
# credit:
# https://stackoverflow.com/questions/25532773/change-background-color-of-active-or-inactive-pane-in-tmux/33553372#33553372
# https://stackoverflow.com/questions/32009787/bash-how-can-the-terminal-be-set-to-flash
# https://stackoverflow.com/questions/5297638/bash-how-to-end-infinite-loop-with-any-key-pressed
# check if tmux version is < 2.1
tmux_is_old=$(awk -v n1="$(tmux -V| cut -d' ' -f2)" -v n2="2.1" 'BEGIN {printf (n1<n2?"1":"0")}')
if [ $tmux_is_old ]; then
tmux_default=$(tmux show-options -g pane-border-style| cut -d' ' -f2)
else
# NOTE -- I'm not sure how to retrieve the current setting, please help
tmux_default=default
fi
flashon () {
if [[ "$TERM" =~ "screen".* ]]; then
if [ $tmux_is_old ]; then
tmux set -g pane-border-style 'fg=colour235,bg=colour200' > /dev/null 2>&1
else
tmux select-pane -P 'fg=blue,bg=green' > /dev/null 2>&1
fi
else
printf '\e[?5h'
fi
}
flashoff () {
if [[ "$TERM" =~ "screen".* ]]; then
if [ $tmux_is_old ]; then
tmux set -g pane-border-style "$tmux_default" > /dev/null 2>&1
else
tmux select-pane -P "$tmux_default"
fi
else
printf '\e[?5l'
fi
}
set -e
trap ctrl_c INT
function ctrl_c() {
flashoff
}
if [ -t 0 ]; then
SAVED_STTY="`stty -g`"
stty -echo -icanon -icrnl time 0 min 0
fi
count=0
keypress=''
while [ "x$keypress" = "x" ]; do
flashon
sleep 0.5
flashoff
sleep 0.5
keypress="`cat -v`"
done
if [ -t 0 ]; then stty "$SAVED_STTY"; fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment