Better beer statistics
authorTomas Mudrunka <tomas@mudrunka.cz>
Mon, 21 Oct 2013 22:14:11 +0000 (00:14 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Mon, 21 Oct 2013 22:14:11 +0000 (00:14 +0200)
c/beermeter/beermeter.sh

index c43701e33717bd542b36c27f8296037758a8e4d8..73fdaa88834fe6e1bec7917c31fbfc80f04470f1 100755 (executable)
@@ -11,16 +11,27 @@ backend='./audio.sh'
 tmp="/tmp/beertmp-$$";
 dialog=$(which dialog);
 
+rst() { echo -ne "\033[0;0H"; }
+
 calc() {
-       echo 'scale=2; '"$@" | bc
+       precise="$(echo 'scale=10; '"$@" | bc)"
+       echo 'scale=2; '"$precise/1" | bc
+}
+
+beer_calc() {
+       pulses="$1"
+       litres=$(calc "$pulses/$pulses_per_liter")
+       beers=$(calc "($pulses/$pulses_per_liter)/$beer_liters")
+       price=$(calc "(($pulses/$pulses_per_liter)/$beer_liters)*$beer_price")
+       echo $beers beers '('$price $currency, $litres l, $pulses pulses')'
+}
+
+beer_pulses() {
+       echo $(wc -c "$accounts/$1" | cut -d ' ' -f 1)
 }
 
 beer_stat() {
-       pulses=$(wc -c "$accounts/$1" | cut -d ' ' -f 1)
-       litres=$(calc $pulses/$pulses_per_liter)
-       beers=$(calc $litres/$beer_liters)
-       price=$(calc $beers*$beer_price)
-       echo $beers piv '('$price $currency, $litres l, $pulses pulses')'
+       beer_calc "$(beer_pulses "$1")"
 }
 
 add_account() {
@@ -56,14 +67,30 @@ beer_menu() {
        done | xargs -0 $dialog --menu "$title stamgasti" 0 0 0
 }
 
+tap() {
+       stamgast="$1"
+
+       pulses_start=$(beer_pulses "$stamgast")
+       clear
+       while true; do
+               pulses_current=$(beer_pulses "$stamgast")
+               pulses_diff=$[ $pulses_current - $pulses_start ]
+
+               rst
+               echo == Cepuje stamgast $stamgast, ukonci ctrl+c ==
+               echo "CURRE: $(beer_calc $pulses_diff)        ";
+               echo "TOTAL: $(beer_calc $pulses_current)     ";
+               sleep 0.5;
+       done &
+       "$backend" | tee -a "$accounts/$stamgast" >/dev/null
+       kill $!
+}
+
 main_menu() {
        while true; do
                beer_menu 2>"$tmp"
                [ "$?" = "0" ] && {
-                       stamgast="$(cat "$tmp")"
-                       clear
-                       echo == Cepuje stamgast $stamgast, ukonci ctrl+c ==
-                       "$backend" | tee -a "$accounts/$stamgast"
+                       tap "$(cat "$tmp")"
                        true
                } || servis_menu
        done
This page took 0.113804 seconds and 4 git commands to generate.