Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a german fuzzyclock in shell
#!/bin/bash
###################################################################################
# Copyright (c) 2009 Armin Widegreen <armin.widegreen@gmail.com> #
# #
# This program is free software; you can redistribute it and/or modify it under #
# the terms of the GNU General Public License as published by the Free Software #
# Foundation, either version 3 of the License, or (at your option) any later #
# version. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A #
# PARTICULAR PURPOSE. See the GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License along with #
# this program. If not, see <http://www.gnu.org/licenses/>. #
###################################################################################
wordsExactly=( "genau" );
wordsMinute=( "eine" "zwei" "drei" "vier" "fuenf" "sechs" "sieben" "acht" "\n" "neun" "zehn" "elf" "zwoelf" "dreizehn"
"vierzehn" "fuenfzehn" "\n" "sechszehn" "siebzehn" "achtzehn" "neunzehn" "zwanzig" "\n" "einundzwanzig" "zweiundzwanzig"
"dreiundzwanzig" "\n" "vierundzwanzig" "fuenfundzwanzig" "sechsundzwanzig" "\n" "siebenundzwanzig" "achtundzwanzig"
"neunundzwanzig" "\n" "dreissig" "minuten" );
wordsBeforAfter=( "vor" "nach" );
wordsHour=( "eins" "zwei" "drei" "vier" "\n" "fuenf" "sechs" "sieben" "acht" "neun" "zehn" "elf" "zwoelf" "\n" );
tmpHighlightWord=""
###############################################
### functions
###############################################
function getNameFromValue {
# $1 should be an value between 1 and 30
case "$1" in
00) result="null";;
0) result="null";;
01) result="eine";;
1) result="eins";;
02) result="zwei";;
2) result="zwei";;
03) result="drei";;
3) result="drei";;
04) result="vier";;
4) result="vier";;
05) result="fuenf";;
5) result="fuenf";;
06) result="sechs";;
6) result="sechs";;
07) result="sieben";;
7) result="sieben";;
08) result="acht";;
8) result="acht";;
09) result="neun";;
9) result="neun";;
10) result="zehn";;
11) result="elf";;
12) result="zwoelf";;
13) result="dreizehn";;
14) result="vierzehn";;
15) result="fuenfzehn";;
16) result="sechszehn";;
17) result="siebzehn";;
18) result="achtzehn";;
19) result="neunzehn";;
20) result="zwanzig";;
21) result="einundzwanzig";;
22) result="zweiundzwanzig";;
23) result="dreiundzwanzig";;
24) result="vierundzwanzig";;
25) result="fuenfundzwanzig";;
26) result="sechsundzwanig";;
27) result="siebenundzwanzig";;
28) result="achtundzwanzig";;
29) result="neunundzwanzig";;
30) result="dreissig";;
*) result="nischt!";;
esac
numAsString=$result
}
function highlightWord {
tmpHighlightWord='\E[37;44m'"\033[1m${1}\033[0m"
}
function changeOutput {
# $1 type: wordsExactly, wordsMinute, wordsHour, wordsBeforAfter
# $2 value to change (string)
if [ "$1" == "wordsExactly" ]; then
numOfWords=${#wordsExactly[@]}
for (( i=0;i<$numOfWords;i++)); do
if [ "$2" == "${wordsExactly[${i}]}" ]; then
highlightWord ${wordsExactly[${i}]}
wordsExactly[${i}]=$tmpHighlightWord
break
fi
done
elif [ "$1" == "wordsMinute" ]; then
numOfWords=${#wordsMinute[@]}
for (( i=0;i<$numOfWords;i++)); do
if [ "$2" == "${wordsMinute[${i}]}" -o "${wordsMinute[${i}]}" == "minuten" ]; then
highlightWord ${wordsMinute[${i}]}
wordsMinute[${i}]=$tmpHighlightWord
fi
done
elif [ "$1" == "wordsBeforAfter" ]; then
numOfWords=${#wordsBeforAfter[@]}
for (( i=0;i<$numOfWords;i++)); do
if [ "$2" == "${wordsBeforAfter[${i}]}" ]; then
highlightWord ${wordsBeforAfter[${i}]}
wordsBeforAfter[${i}]=$tmpHighlightWord
break
fi
done
elif [ "$1" == "wordsHour" ]; then
numOfWords=${#wordsHour[@]}
for (( i=0;i<$numOfWords;i++)); do
if [ "$2" == "${wordsHour[${i}]}" ]; then
highlightWord ${wordsHour[${i}]}
wordsHour[${i}]=$tmpHighlightWord
break
fi
done
fi
}
function echoWord {
# $1: word to output
out=$1
if [ "$out" != "\n" ]; then
echo -en "$out "
else
echo -en $out
fi
}
###############################################
### programm
###############################################
hour=$(date +%l)
minute=$(date +%M)
beforeAfter=""
if [ "$minute" -lt 30 -a "$minute" -ne 00 ]; then
beforeAfter="nach"
elif [ "$minute" -ge 30 ]; then
beforeAfter="vor"
((minute=60-$minute))
if [ $minute -eq 1 ]; then
minute="01"
fi
if [ "$hour" -eq 12 ]; then
hour=1
else
((hour++))
fi
fi
#echo $minute
#echo $hour
if [ "$minute" -eq 00 ]; then
changeOutput "wordsExactly" "genau"
getNameFromValue $hour
changeOutput "wordsHour" $numAsString
else
getNameFromValue $minute
changeOutput "wordsMinute" $numAsString
changeOutput "wordsBeforAfter" $beforeAfter
getNameFromValue $hour
changeOutput "wordsHour" $numAsString
fi
#output all!
echo "-------------------------------------------------"
# output exactly
numElements=${#wordsExactly[@]}
for (( i=0;i<$numElements;i++)); do
echoWord ${wordsExactly[${i}]}
done
# output minute
numElements=${#wordsMinute[@]}
for (( i=0;i<$numElements;i++)); do
echoWord ${wordsMinute[${i}]}
done
# output beforeafter
numElements=${#wordsBeforAfter[@]}
for (( i=0;i<$numElements;i++)); do
echoWord ${wordsBeforAfter[${i}]}
done
# output hour
numElements=${#wordsHour[@]}
for (( i=0;i<$numElements;i++)); do
echoWord ${wordsHour[${i}]}
done
echo "-------------------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment