Laurence build a tiny project to show washing time remaining elapse in the terminal1, that idea is very fun. and I was very curious about all code behind of this
#! /bin/sh
set -e
case $1 in
date) date -u +%M:%S ;;
countdown)
tput civis
# trap "tput cnorm" 1 2 3 13 15
trap "tput cnorm" HUP INT QUIT PIPE TERM
while true; do
t=$(./countdown.sh date)
if [ $t = "25:00" ]; then
break
fi
printf "\r%s%s" \
"$(tput el)" \
"$t"
# sleep 30
done
tput cnorm
echo
terminal-notifier -title "Show Title" -message "done"
;;
esac
chatGPT worte same as:
#!/bin/bash
case "$1" in
date)
date -u +%M:%S
;;
countdown)
tput civis
trap 'tput cnorm; exit' INT TERM
SECONDS=0
while true; do
if (( SECONDS >= 20 )); then
break
fi
printf "\r%s" "$(./countdown.sh date)"
tput el
sleep 0.5
((SECOUNDS++))
done
tput cnorm
terminal-notifier -title "Show Title" -message "done"
;;
*)
echo "Invalid option. Usage: $0 {date|countdown}"
exit 1
;;
esac
and explain
This is a shell script that has two cases:
date
andcountdown
.The
date
case simply prints the current minute and second in UTC time.The
countdown
case uses thetput
command to hide the cursor and sets up a trap to restore the cursor if the script is interrupted. It then enters a loop that repeatedly prints the current time remaining until the countdown reaches 25 minutes. Theprintf
command usestput el
to clear the line before printing the current time. Once the countdown reaches 25 minutes, the loop breaks and the cursor is restored. Finally, the script uses theterminal-notifier
command to display a notification with the title "Show Title" and the message "done".This script can be run with the command
./countdown.sh {{case}}
, where{{case}}
is eitherdate
orcountdown
.
tput
is a command in Unix-like operating systems that allows you to manipulate terminal settings. It can be used to set the terminal's foreground and background colors, move the cursor to specific positions, clear the screen, and more.
tput
is often used in shell scripts to make them more interactive and user-friendly by providing a way to control the terminal's appearance and behavior.
the code behave like this
