Skip to content

Commit

Permalink
improved progressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
lunarcloud committed Jun 18, 2024
1 parent bb5a8a1 commit 456e4ee
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Functions
| **checklist** | Display a list of multiply-selectable items | <ol><li>The file whose text to display</li><li>Number of options</li><li>First item's value</li><li>First item's description</li><li>First item's default checked status (ON or OFF)</li><li>(repeat for all items)</li></ol> | Value text of the selected item (or the default item) |
| **radiolist** | Display a list of singularly-selectable items | <ol><li>The file whose text to display</li><li>Number of options</li><li>First item's value</li><li>First item's description</li><li>First item's default selected status (ON or OFF)</li><li>(repeat for all items)</li></ol> | Value text of the selected item (or the default item) |
| **progressbar** | A pipe that displays a progressbar | the current value of the bar (repeatable, should be piped) | |
| **progressbar_update** | Updates the value of the progressbar (call from within the progressbar piped block) | the value to set the bar | |
| **progressbar_update** | Updates the value of the progressbar (call from within the progressbar piped block) | <ol><li>the new percentage value/li><li>the new status text</li></ol> | |
| **progressbar_finish** | Completes the the progressbar (call from within the progressbar piped block) | | |
| **filepicker** | Display a file selector dialog | <ol><li>The starting folder</li><li>"save" or "open" (assume "open" if omitted)</li></ol> | Path to selected file | |
| **folderpicker** | Display a folder selector dialog | The starting folder | Path to selected folder |
Expand Down
63 changes: 52 additions & 11 deletions script-dialog.sh
Original file line number Diff line number Diff line change
Expand Up @@ -972,10 +972,12 @@ function progressbar() {
fi
_calculate-gui-title

export PROGRESS_ACTIVITY=$ACTIVITY

if [ "$INTERFACE" == "whiptail" ]; then
whiptail --gauge "$ACTIVITY" "$RECMD_LINES" "$RECMD_COLS" 0
whiptail --backtitle "$APP_NAME" --title "$ACTIVITY" --gauge " " "$RECMD_LINES" "$RECMD_COLS" 0
elif [ "$INTERFACE" == "dialog" ]; then
dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --gauge "" "$RECMD_LINES" "$RECMD_COLS" 0
dialog --clear --backtitle "$APP_NAME" --title "$ACTIVITY" --gauge "" "$RECMD_LINES" "$RECMD_COLS" 0
elif [ "$INTERFACE" == "zenity" ]; then
zenity --title "$GUI_TITLE" $ZENITY_ICON_ARG "$GUI_ICON" ${ZENITY_HEIGHT+--height=$ZENITY_HEIGHT} ${ZENITY_WIDTH+--width=$ZENITY_WIDTH} --progress --text="$ACTIVITY" --auto-close --auto-kill --percentage 0
elif [ "$INTERFACE" == "kdialog" ]; then
Expand All @@ -994,7 +996,8 @@ function progressbar() {

qdbus "${dbusRef[@]}" close
else
echo -ne "\r${HOURGLASS_SYMBOL}$ACTIVITY 0%"
BAR="[░░░░░░░░░░░░░░░░]"
echo -ne "\r${HOURGLASS_SYMBOL}$ACTIVITY $BAR 0%"
cat
fi
}
Expand All @@ -1016,17 +1019,54 @@ function progressbar() {
function progressbar_update() {
if [ "$INTERFACE" == "kdialog" ]; then
DBUS_BAR_PATH=/tmp/script-dialog.$$/progressbar_dbus
if [ -e $DBUS_BAR_PATH ]; then
read -r -d '' -a dbusRef < <( cat $DBUS_BAR_PATH )
qdbus "${dbusRef[@]}" Set "" value "$1"
sleep 0.2 # requires slight sleep
else
echo -e "Could not update progressbar $$"
if [ -e $DBUS_BAR_PATH ]; then
read -r -d '' -a dbusRef < <( cat $DBUS_BAR_PATH )
if qdbus "${dbusRef[@]}" 2>/dev/null; then
qdbus "${dbusRef[@]}" Set "" value "$1"
qdbus "${dbusRef[@]}" setLabelText "$2"
sleep 0.2 # requires slight sleep
else
progressbar_finish
fi
else
echo -e "Could not update progressbar $$"
fi
elif [ "$INTERFACE" == "whiptail" ] || [ "$INTERFACE" == "dialog" ] || [ "$INTERFACE" == "zenity" ]; then
elif [ "$INTERFACE" == "zenity" ]; then
echo -e "$1"
echo -e "#$2"
elif [ "$INTERFACE" == "whiptail" ] || [ "$INTERFACE" == "dialog" ]; then
echo -e "XXX\n$1\n$2\nXXX"
else
echo -ne "\r${HOURGLASS_SYMBOL}$ACTIVITY $1%"
case "$1" in
5) BAR="[█░░░░░░░░░░░░░░░░░░░]" ;;
10) BAR="[██░░░░░░░░░░░░░░░░░░]" ;;
15) BAR="[███░░░░░░░░░░░░░░░░░]" ;;
20) BAR="[████░░░░░░░░░░░░░░░░]" ;;
25) BAR="[█████░░░░░░░░░░░░░░░]" ;;
30) BAR="[██████░░░░░░░░░░░░░░]" ;;
35) BAR="[███████░░░░░░░░░░░░░]" ;;
40) BAR="[████████░░░░░░░░░░░░]" ;;
45) BAR="[█████████░░░░░░░░░░░]" ;;
50) BAR="[██████████░░░░░░░░░░]" ;;
55) BAR="[███████████░░░░░░░░░]" ;;
60) BAR="[████████████░░░░░░░░]" ;;
65) BAR="[█████████████░░░░░░░]" ;;
70) BAR="[██████████████░░░░░░]" ;;
75) BAR="[███████████████░░░░░]" ;;
80) BAR="[████████████████░░░░]" ;;
85) BAR="[█████████████████░░░]" ;;
90) BAR="[██████████████████░░]" ;;
95) BAR="[███████████████████░]" ;;
100)BAR="[████████████████████]" ;;
*) BAR="[░░░░░░░░░░░░░░░░░░░░]";;
esac

TEXT=""
if [[ "$2" != "" ]]; then
TEXT=": $2"
fi

echo -ne "\e[2K\r${HOURGLASS_SYMBOL}$ACTIVITY $BAR ${1}% $TEXT"
fi
}

Expand Down Expand Up @@ -1054,6 +1094,7 @@ function progressbar_finish() {
elif [ "$INTERFACE" != "whiptail" ] && [ "$INTERFACE" != "dialog" ] && [ "$INTERFACE" != "zenity" ]; then
echo ""
fi
unset PROGRESS_ACTIVITY
}


Expand Down
9 changes: 8 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@ message-info "Nice to meet you, $NAME"
ACTIVITY="Pretending to load..."
{
for ((i = 0 ; i <= 100 ; i+=5)); do
progressbar_update "$i"
# optional text during progress
if [[ "$((RANDOM % 2))" == "1" ]]; then
SUB_ACTIVITY="it's thinking"
else
SUB_ACTIVITY=""
fi

progressbar_update "$i" "$SUB_ACTIVITY"
sleep 0.2
done
progressbar_finish
Expand Down

0 comments on commit 456e4ee

Please sign in to comment.