add a function to show appointments, callable from .profile
[shellsnippets/shellsnippets.git] / mksh / progress-bar
index 7755409..de1ebb5 100644 (file)
@@ -48,7 +48,7 @@ function init_progress_bar {
        global -i _cnt_progress_bar=$1 _cur_progress_bar=0
        global -i nlin_progress_bar=$LINES isin_progress_bar=1
 
-       trap 'done_progress_bar' EXIT
+       trap 'done_progress_bar 1' EXIT
        trap 'sigwinch_progress_bar' WINCH
        # set up scrolling region, draw initial empty bar
        sigwinch_progress_bar
@@ -65,7 +65,7 @@ function sigwinch_progress_bar {
        # save position; clear scrolling region; restore position; newline;
        # up one line (to ensure we are not in the last line); save position;
        # clear rest of screen; set new scrolling region; restore position
-       print -n "\\e7\\e[0;0r\\e8\\n\\e[A\\e7\\e[J\\e[1;$((# nlin_progress_bar - 1))r\\e8"
+       print -nu2 "\\e7\\e[0;0r\\e8\\n\\e[A\\e7\\e[J\\e[1;$((# nlin_progress_bar - 1))r\\e8"
 
        # redraw progress bar
        draw_progress_bar_internal
@@ -76,14 +76,20 @@ function done_progress_bar {
        isin_progress_bar=0
        # save position; clear scrolling region; restore position;
        # save position; clear rest of screen; restore position
-       print -n "\\e7\\e[0;0r\\e8\\e7\\e[J\\e8"
+       print -nu2 "\\e7\\e[0;0r\\e8\\e7\\e[J\\e8"
        trap - WINCH
        trap - EXIT
+       [[ -n $1 ]] || (( _cur_progress_bar == _cnt_progress_bar )) || \
+           print -ru2 W: expected $_cnt_progress_bar draw_progress_bar calls, \
+           got only $_cur_progress_bar
 }
 
 function draw_progress_bar {
        # increment current progress
-       let ++_cur_progress_bar
+       if (( ++_cur_progress_bar > _cnt_progress_bar )); then
+               print -ru2 "W: too many draw_progress_bar calls"
+               _cur_progress_bar=$_cnt_progress_bar
+       fi
        # remaining drawing code
        draw_progress_bar_internal
 }
@@ -112,5 +118,5 @@ function draw_progress_bar_internal {
        # save position; go to last line; set colours;
        # output a line full of spaces (and completion percentage);
        # jump to first column; output bar (line præfix); restore position
-       print -n -- "\\e7\\e[$nlin_progress_bar;1H\\e[0;1;33;44m$spc\\r$bar\\e8"
+       print -nu2 -- "\\e7\\e[$nlin_progress_bar;1H\\e[0;1;33;44m$spc\\r$bar\\e8"
 }