This repository has been archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeuntil
executable file
·115 lines (105 loc) · 3.27 KB
/
timeuntil
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
#!/bin/bash
# Set some variables
homeDir=$(ls -d ~)
file="${homeDir}/.timeuntil"
if [ ! -e $file ]; then
touch $file
elif [ ! -f $file ]; then
echo "Invalid countdown file: $file"
exit 1
fi
now=$(date +%s)
records=$(cat $file | base64 -d)
numRecords=$(echo $records | grep -o '::' | wc -l)
usage="\
Usage: $(basename $0) [--short|-s] #get time until destination
$(basename $0) [--add|-a] \"time\" \"name\" #assign time to the name
$(basename $0) [--trim|-t] #remove old trackers
$(basename $0) [--reset|-r] #reset trackers"
doShort=false
# Option handling
if [ $# -gt 0 ]; then
case $1 in
# Add a new record
--add|-a)
if [ $# -ne 3 ]; then
echo "$usage"
exit 1
fi
records+="$(echo $3 | tr -d ':'):"$(date -d "$2" +%s)"::"
echo $records | base64 > $file
;;
# Reset the record file
--reset|-r)
echo -n '' > $file
exit
;;
# Short output lines
--short|-s)
doShort=true
;;
# Remove old records (time before now)
--trim|-t)
records=$(echo $records | awk -F:: -v "now=${now}" ' {
for (i=1; i<NF; i++) {
split($i, r, ":")
if (r[2] < now) {
next
}
printf("%s::", $i)
}
}')
echo $records | base64 > $file
;;
# Print help
--help|-h)
echo "$usage"
exit
;;
# Wat
*)
echo "$usage"
exit 1
;;
esac
fi
# Process records
echo $records | sed 's/::/\n/g' | sed '/^$/d' | while IFS= read record; do
recordName=$(echo $record | cut -d: -f1)
recordTime=$(echo $record | cut -d: -f2)
# Set the difference, determine if it's old
timeDiff=$(( $recordTime - $now ))
if [ $timeDiff -lt 1 ]; then
echo "$recordName already happened."
records="${records#$record"::"}"
numRecords=$(( $numRecords - 1 ))
continue
fi
# Chunk it down to weeks
seconds=$(( $timeDiff % 60 ))
timeDiff=$(( $timeDiff - $seconds ))
minutes=$(( $timeDiff % 3600 / 60 ))
timeDiff=$(( $timeDiff - $minutes ))
hours=$(( $timeDiff % 86400 / 3600 ))
timeDiff=$(( $timeDiff - $hours ))
days=$(( $timeDiff % 604800 / 86400 ))
timeDiff=$(( $timeDiff - $hours ))
weeks=$(( $timeDiff / 604800 ))
# Print this record, omitting unnecessary bits.
printLine=''
if [ "$doShort" == "true" ]; then
[ $weeks -ne 0 ] && { [ $weeks -gt 1 ] && printLine+="$weeks wks, " || printLine+="$weeks wk, "; }
[ $days -ne 0 ] && { [ $days -gt 1 ] && printLine+="$days days, " || printLine+="$days day, "; }
[ $hours -ne 0 ] && { [ $hours -gt 1 ] && printLine+="$hours hrs, " || printLine+="$hours hr, "; }
[ $minutes -ne 0 ] && { [ $minutes -gt 1 ] && printLine+="$minutes mins, " || printLine+="$mintues min, "; }
printLine="${printLine%,*} til $recordName."
else
[ $weeks -ne 0 ] && { [ $weeks -gt 1 ] && printLine+="$weeks weeks, " || printLine+="$weeks week, "; }
[ $days -ne 0 ] && { [ $days -gt 1 ] && printLine+="$days days, " || printLine+="$days day, "; }
[ $hours -ne 0 ] && { [ $hours -gt 1 ] && printLine+="$hours hours, " || printLine+="$hours hour, "; }
[ $minutes -ne 0 ] && { [ $minutes -gt 1 ] && printLine+="$minutes minutes, " || printLine+="$mintues minute, "; }
[ $seconds -ne 0 ] && { [ $seconds -gt 1 ] && printLine+="$seconds seconds, " || printLine+="$seconds seconds, "; }
printLine="${printLine%,*} until $recordName."
fi
echo "$printLine"
done