Updates: make the progress bar interruptible with ESC

Add a simple non-blocking read-alike function and use it to check
whether the user pressed ESC during the updates.

Signed-off-by: Mauro Giachero <mauro.giachero@gmail.com>
This commit is contained in:
mauro.giachero 2009-02-05 08:10:12 +00:00
parent ef8c8602fa
commit 02e0d19060

View File

@ -326,7 +326,7 @@ function progressbar_cli () {
else else
SCREENWIDTH=80 SCREENWIDTH=80
fi fi
BARWIDTH=$(($SCREENWIDTH - 7)) BARWIDTH=$(($SCREENWIDTH - 8))
while read PROGRESS; do while read PROGRESS; do
# Show the percentage # Show the percentage
@ -358,12 +358,29 @@ function progressbar () {
fi fi
} }
function read_nonblock () {
# This is a simple non-blocking read function reading a single
# character (if available) from stdin and putting it in $1.
local STTY_STATUS=$(stty --save)
stty -icanon time 0 min 0 -echo
read $1
stty $STTY_STATUS
}
function progressbar_interrupted () {
# This function checks whether the user pressed ESC
local ESC="" KEY
read_nonblock KEY
[[ "$KEY" = "$ESC" ]]
}
check_for_updates () { check_for_updates () {
# This checks for updates to installed SBo packages. Thanks to Mauro # This checks for updates to installed SBo packages. Thanks to Mauro
# Giachero for this much-improved update code and related functions! # Giachero for this much-improved update code and related functions!
local NEWSB NEWINFO NEWVER local NEWSB NEWINFO NEWVER
local VERSION_EXPRESSION local VERSION_EXPRESSION
local TEMPFILE UPDATELIST VERSION_FILE local TEMPFILE UPDATELIST VERSION_FILE PROGRESSBAR_INTERRUPTED
local STRING INDEX OLDNAME NAME VER ARCH BUILD local STRING INDEX OLDNAME NAME VER ARCH BUILD
local VER_NUMERIC NEWVER_NUMERIC UPDATED local VER_NUMERIC NEWVER_NUMERIC UPDATED
local PKGS NUMPKGS PROGRESSCOUNTER=0 local PKGS NUMPKGS PROGRESSCOUNTER=0
@ -382,6 +399,7 @@ check_for_updates () {
PKGS=$(ls *_SBo) PKGS=$(ls *_SBo)
NUMPKGS=$(echo $PKGS |wc -w) NUMPKGS=$(echo $PKGS |wc -w)
VERSION_FILE=$TMP/sbopkg-script-version VERSION_FILE=$TMP/sbopkg-script-version
PROGRESSBAR_INTERRUPTED=$TMP/sbopkg_progressbar-interrupted
if [ -e "$PKGS" ]; then if [ -e "$PKGS" ]; then
echo "No SlackBuilds.org packages detected." >> $UPDATELIST echo "No SlackBuilds.org packages detected." >> $UPDATELIST
else else
@ -389,10 +407,11 @@ check_for_updates () {
potential updates..." >> $UPDATELIST potential updates..." >> $UPDATELIST
echo >> $UPDATELIST echo >> $UPDATELIST
{ # Grouping for the progressbar { # Grouping for the progressbar
echo 0 # Progressbar begin
for CURPKG in $PKGS; do for CURPKG in $PKGS; do
# Progress indicator, for the progressbar # Bail out if the user pressed ESC
echo $(($PROGRESSCOUNTER * 100 / $NUMPKGS)) progressbar_interrupted && touch $PROGRESSBAR_INTERRUPTED && break
(( PROGRESSCOUNTER += 1 ))
# This next code is borrowed and modified from pkgtool # This next code is borrowed and modified from pkgtool
#echo $i | sed 's/_SBo$//;s/-[^-]*-[^-]*-[^-]*$//' #echo $i | sed 's/_SBo$//;s/-[^-]*-[^-]*-[^-]*$//'
@ -508,22 +527,29 @@ check_for_updates () {
echo " Not in the repository." >> $UPDATELIST echo " Not in the repository." >> $UPDATELIST
fi fi
fi fi
# Progress indicator, for the progressbar
(( PROGRESSCOUNTER += 1 ))
echo $(($PROGRESSCOUNTER * 100 / $NUMPKGS))
done done
echo 100 # To complete the progressbar
} | progressbar "Building list of potential updates" "This may take\ } | progressbar "Building list of potential updates" "This may take\
a few moments depending on how many SlackBuilds.org packages are\ a few moments depending on how many SlackBuilds.org packages are\
installed..." installed..."
echo >> $UPDATELIST echo >> $UPDATELIST
echo "Potential update list complete." >> $UPDATELIST echo "Potential update list complete." >> $UPDATELIST
fi fi
if [ "$DIAG" = 1 ]; then if [[ ! -f $PROGRESSBAR_INTERRUPTED ]]; then
dialog --title "Viewing potential updates." --textbox $UPDATELIST 0 0 if [ "$DIAG" = 1 ]; then
dialog --title "Viewing potential updates." --textbox $UPDATELIST 0 0
else
cat $UPDATELIST
fi
# Permanent log of the updatelist is saved when DEBUG is enabled.
if [ "$DEBUG" -ge "1" ]; then
cp $UPDATELIST $TMP/sbopkg-debug-updatelist
fi
else else
cat $UPDATELIST rm -f $PROGRESSBAR_INTERRUPTED
fi
# Permanent log of the updatelist is saved when DEBUG is enabled.
if [ "$DEBUG" -ge "1" ]; then
cp $UPDATELIST $TMP/sbopkg-debug-updatelist
fi fi
} }