Skip to content

Instantly share code, notes, and snippets.

@koozz
Last active August 29, 2015 13:59
Show Gist options
  • Save koozz/10643748 to your computer and use it in GitHub Desktop.
Save koozz/10643748 to your computer and use it in GitHub Desktop.
Fix leading zero on the minutes.
#!/bin/bash
# On/off visual
on=$(tput setaf 2)
off=$(tput setaf 0)
# Qlock data
qlock=(
I T L I S A S T I M E
A C Q U A R T E R D C
T W E N T Y F I V E X
H A L F B T E N F T O
P A S T E R U N I N E
O N E S I X T H R E E
F O U R F I V E T W O
E I G H T E L E V E N
S E V E N T W E L V E
T E N S E O C L O C K
)
# Qlock state
state=()
# Short hand to activate a part
activate() {
for s in ${!1}; do state[$s]=1; done
}
# Identifiable parts
it_is=(0 1 3 4)
minute_half=(33 34 35 36)
minute_ten=(38 39 40)
minute_quarter=(11 13 14 15 16 17 18 19)
minute_twenty=(22 23 24 25 26 27)
minute_five=(28 29 30 31)
to=(42 43)
past=(44 45 46 47)
oclock=(104 105 106 107 108 109)
hour_1=(55 56 57)
hour_2=(74 75 76)
hour_3=(61 62 63 64 65)
hour_4=(66 67 68 69)
hour_5=(70 71 72 73)
hour_6=(58 59 60)
hour_7=(88 89 90 91 92)
hour_8=(77 78 79 80 81)
hour_9=(51 52 53 54)
hour_10=(99 100 101)
hour_11=(82 83 84 85 86 87)
hour_12=(93 94 95 96 97 98)
# Use system time
hours=$(date +%H | sed 's/^0//')
minutes=$(date +%M | sed 's/^0//')
# Round minutes
minutes_offset=$((minutes % 5))
[ ${minutes_offset} -lt 3 ] && minutes=$((minutes - minutes_offset)) || minutes=$((minutes - minutes_offset + 5))
[ ${minutes} -eq 60 ] && minutes=0 && hours=$((hours+1))
# Activate natural language
activate it_is[@]
if [ ${minutes} -ne 0 ]
then
if [ ${minutes} -gt 30 ]
then
hours=$((hours+1))
activate to[@]
else
activate past[@]
fi
else
activate oclock[@]
fi
# Fix hours
while [ ${hours} -gt 12 ]; do hours=$((hours-12)); done
# Activate minutes
case ${minutes} in
5|55)
activate minute_five[@]
;;
10|50)
activate minute_ten[@]
;;
15|45)
activate minute_quarter[@]
;;
20|40)
activate minute_twenty[@]
;;
25|35)
activate minute_twenty[@]
activate minute_five[@]
;;
30)
activate minute_half[@]
;;
esac
# Activate hour
activate hour_${hours}[@]
# Show the qlock
index=0
echo -n ${off}
while [ "$index" -lt "${#qlock[*]}" ]
do
[ "${state[$index]}" ] && echo -n "${on}${qlock[$index]}${off} " || echo -n "${qlock[$index]} "
[ "$((index % 11))" -eq 10 ] && echo
((index++))
done
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment