summaryrefslogtreecommitdiff
path: root/dotfiles/system/.profile.d/chronographic.sh
blob: d53fe2bb54f8b4708b3eea4afa9905d453ce48a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh

# chronographic.sh
# Craig Jennings <c@cjennings.net>
# 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
    echo "Alarm '${@:2}' is queued for $1." && echo "To see all alarms, issue the command: 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")"
}