Skip to content

Instantly share code, notes, and snippets.

@bxb100
Last active April 14, 2023 08:03
Show Gist options
  • Save bxb100/15761a3f712741f1cd637f030cef648c to your computer and use it in GitHub Desktop.
Save bxb100/15761a3f712741f1cd637f030cef648c to your computer and use it in GitHub Desktop.
Display time elapse in your terminal

Initiate

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

Using ChatGPT to explain

#! /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 and countdown.

The date case simply prints the current minute and second in UTC time.

The countdown case uses the tput 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. The printf command uses tput 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 the terminal-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 either date or countdown.

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.

Footnotes

  1. https://tratt.net/laurie/blog/2023/displaying_my_washing_machines_remaining_time_with_curl_jq_pizauth.html

@bxb100
Copy link
Author

bxb100 commented Apr 14, 2023

the code behave like this
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment