Refactor emoji-clock plugin

* Use prompt sequences instead of external date command

* Replace switch statement with associative array and
  indexing logic
This commit is contained in:
Alexis Hildebrandt 2015-09-18 14:22:27 +02:00
parent 9c08641d7c
commit 0102eda345

View File

@ -4,30 +4,51 @@
# Inspired by Andre Torrez' "Put A Burger In Your Shell" # Inspired by Andre Torrez' "Put A Burger In Your Shell"
# http://notes.torrez.org/2013/04/put-a-burger-in-your-shell.html # http://notes.torrez.org/2013/04/put-a-burger-in-your-shell.html
# AUTHOR: Alexis Hildebrandt (afh[at]surryhill.net) # AUTHOR: Alexis Hildebrandt (afh[at]surryhill.net)
# VERSION: 1.0.0 # VERSION: 2.0.0
# ----------------------------------------------------------------------------- # ------------------------------------------------------------------------------
function emoji-clock() { function emoji-clock() {
# Add 15 minutes to the current time and save the value as $minutes. # The emoji clockface definitions are copied from the
(( minutes = $(date '+%M') + 15 )) # emoji plugin to avoid introducing a requirement to it.
(( hour = $(date '+%I') + minutes / 60 )) # Should dependency tracking or dependency management
# make sure minutes and hours don't exceed 60 nor 12 respectively # find its way into oh-my-zsh a dependency on the emoji
# plugin can be specified and the local emoji_clockface
# definitions below can be removed.
typeset -AH emoji_clockface
emoji_clockface[clock_face_one_oclock]=$'\U1F550'
emoji_clockface[clock_face_two_oclock]=$'\U1F551'
emoji_clockface[clock_face_three_oclock]=$'\U1F552'
emoji_clockface[clock_face_four_oclock]=$'\U1F553'
emoji_clockface[clock_face_five_oclock]=$'\U1F554'
emoji_clockface[clock_face_six_oclock]=$'\U1F555'
emoji_clockface[clock_face_seven_oclock]=$'\U1F556'
emoji_clockface[clock_face_eight_oclock]=$'\U1F557'
emoji_clockface[clock_face_nine_oclock]=$'\U1F558'
emoji_clockface[clock_face_ten_oclock]=$'\U1F559'
emoji_clockface[clock_face_eleven_oclock]=$'\U1F55A'
emoji_clockface[clock_face_twelve_oclock]=$'\U1F55B'
emoji_clockface[clock_face_one_thirty]=$'\U1F55C'
emoji_clockface[clock_face_two_thirty]=$'\U1F55D'
emoji_clockface[clock_face_three_thirty]=$'\U1F55E'
emoji_clockface[clock_face_four_thirty]=$'\U1F55F'
emoji_clockface[clock_face_five_thirty]=$'\U1F560'
emoji_clockface[clock_face_six_thirty]=$'\U1F561'
emoji_clockface[clock_face_seven_thirty]=$'\U1F562'
emoji_clockface[clock_face_eight_thirty]=$'\U1F563'
emoji_clockface[clock_face_nine_thirty]=$'\U1F564'
emoji_clockface[clock_face_ten_thirty]=$'\U1F565'
emoji_clockface[clock_face_eleven_thirty]=$'\U1F566'
emoji_clockface[clock_face_twelve_thirty]=$'\U1F567'
# Shift the current time by 15 minutes, so that the oclock face is shown
# from "45 to "15 and the thirty face is shown from "15 to "30
(( minutes = $(print -P '%D{%M}') + 15 ))
(( hour = $(print -P '%D{%L}') + minutes / 60 ))
# Make sure minutes and hours don't exceed 60 nor 12 respectively
(( minutes %= 60 )); (( hour %= 12 )) (( minutes %= 60 )); (( hour %= 12 ))
case $hour in hourhands="twelve one two three four five six seven eight nine ten eleven"
0) clock="🕛"; [ $minutes -ge 30 ] && clock="🕧";; hourhand=${${=hourhands}[((hour+1))]}
1) clock="🕐"; [ $minutes -ge 30 ] && clock="🕜";; if [ $minutes -ge 30 ]; then minutehand="thirty" else minutehand="oclock" fi
2) clock="🕑"; [ $minutes -ge 30 ] && clock="🕝";; echo $emoji_clockface[clock_face_${hourhand}_${minutehand}]
3) clock="🕒"; [ $minutes -ge 30 ] && clock="🕞";;
4) clock="🕓"; [ $minutes -ge 30 ] && clock="🕟";;
5) clock="🕔"; [ $minutes -ge 30 ] && clock="🕠";;
6) clock="🕕"; [ $minutes -ge 30 ] && clock="🕡";;
7) clock="🕖"; [ $minutes -ge 30 ] && clock="🕢";;
8) clock="🕗"; [ $minutes -ge 30 ] && clock="🕣";;
9) clock="🕘"; [ $minutes -ge 30 ] && clock="🕤";;
10) clock="🕙"; [ $minutes -ge 30 ] && clock="🕥";;
11) clock="🕚"; [ $minutes -ge 30 ] && clock="🕦";;
*) clock="⌛";;
esac
echo $clock
} }