#!/bin/sh # chronographic.sh # Craig Jennings # quick and dirty terminal clock, timer, alarm and stopwatch functions alias beep='paplay $BEEP' export BEEP="/usr/share/sounds/freedesktop/stereo/bell.oga" ## ## CLOCK ## clock() { while true; do tput clear; echo ""; date +" %l : %M %p" | figlet -f roman -w 200 ; sleep 1; done } ## ## TIMER ## timer() { # Ensure we have at least two arguments if [ "$#" -lt 2 ]; then echo "Pass the time and a notification. Example: timer 1h30m Parking expiring" return 1 fi message="${@:2}" start_time=$(date +"%H:%M:%S %p") printf "\nStarting %s timer at $start_time\n" "$1" snore "$1" && paplay "$BEEP" && notify-send "Timer" "$message" && echo "" } ## ## ALARM ## alarm() { # Ensure we have two or more arguments if [ "$#" -lt 2 ]; then echo "Pass both the time and a message. Example: alarm 1:45pm Time to eat!" return 1 fi # Validate the first argument is a valid time ! date -d "$1" >/dev/null 2>&1 && echo "Invalid time: $1" && return 1 # silently schedule the command using 'at' command echo "paplay \$BEEP && notify-send \"Alarm\" \"$@\"" | at "$1" >> /dev/null 2>&1 echo "" echo "Alarm '${@:2}' is queued for $1." echo "To see all alarms, issue the command: 'atq'" echo "To remove an alarm, issue the command: 'atrm' and the number of entry from atq" echo "" } ## ## STOPWATCH FUNCTIONS ## sw_start_time=0 sw_started=0 swreset() { sw_start_time=0 sw_started=0 echo "Stopwatch reset" } swshow() { if [ "$sw_started" = 0 ] ; then echo "Error: Stopwatch not started" >&2 && return 1 fi current_time=$(date +%s) elapsed_time=$((current_time - sw_start_time)) if (( elapsed_time < 60 )); then # Display elapsed time in seconds echo "Elapsed time: $elapsed_time seconds" elif (( elapsed_time < 3600 )); then # Display elapsed time in minutes and seconds minutes=$((elapsed_time / 60)) seconds=$((elapsed_time % 60)) echo "Elapsed time: $minutes minutes, $seconds seconds" else # Display elapsed time in hours, minutes, and seconds hours=$((elapsed_time / 3600)) minutes=$(((elapsed_time / 60) % 60)) seconds=$((elapsed_time % 60)) echo "Elapsed time: $hours hours, $minutes minutes, and $seconds seconds" fi } swstop() { swshow swreset } swstart() { if [ "$sw_started" = 1 ] ; then printf "Stopwatch is already started. Reset? (y/n): " read -r answer if [ "$answer" = "y" -o "$answer" = "Y" ]; then swreset # continue on to start the new timer elif [ "$answer" = "n" -o "$answer" = "N" ]; then echo "Stopwatch not reset." swshow # return to avoid restarting the timer return else echo "Error: Invalid input. Exiting." >&2 && return 1 fi fi sw_started=1 sw_start_time=$(date +%s) printf "Stopwatch started at %s\n\n" "$(date +"%H:%M:%S %p")" }