T••LBX: Blog

Zshell version of the flasher function

The "flasher" function is just a small command which flashes your screen until you press any key. It is usually put at the end of a long running process in order to indicate that the job is finished.

$ crystal build c3po-protocols.cr --release; flasher

Without further ado, here is the function:

flasher () {
  while true; do
    printf "\e[?5h"
    sleep 0.2
    printf "\e[?5l"
    read -sk -t1 && break

As you can see if you know the Bash version, it is not very different. The tricky part is when it reads because read is a builtin and has different options. In fact if you run the Bash version, you'll get a bad option: -1. That is because in zshell, the option for the number of keys you are expecting is -k instead of -n. If you don't pass a number, it will default to 1.

The way this function works is that it uses the terminal escape sequences \e[?5h and \e[?5l. They switch on and off reverse video in a loop. At the end of each cycle we try to read a character from the user with a timeout of 1 second. If no key is pressed after 1 second, it does another cycle. Pretty simple.

Tags:     shell zshell zsh