* Added support for command line parameters.
authorUrban Wallasch <urban.wallasch@freenet.de>
Fri, 10 Apr 2020 17:59:45 +0000 (19:59 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Fri, 10 Apr 2020 17:59:45 +0000 (19:59 +0200)
* Use fzf(1) instead of dialog(1) to generate main menu by default; use -d to revert to old behavior.
* Several minor fixes and improvements.

XbpsUI.sh

index 24f7f4f60092bae10f05438dc14e208628ed1355..e6c1d5d981cd69c3006fcdd319fc9f0af8e1894d 100755 (executable)
--- a/XbpsUI.sh
+++ b/XbpsUI.sh
@@ -1,5 +1,28 @@
 #!/bin/bash
 
+verstr="XbpsUI 2.0"
+
+# Evaluate command line:
+for arg in "$@" ; do
+    case "$arg" in
+        "-V")
+            echo "$verstr"
+            exit 0
+            ;; 
+        "-d")
+            use_dialog=1
+            ;;
+        "-h"|*)
+            echo "Usage:  $(basename $0) [-d] [-h] [-V]"
+            echo "    -d  use dialog(1) to display the main menu, not fzf(1)"
+            echo "    -h  display this help page"
+            echo "    -V  display version info and exit"
+            exit 0
+            ;;
+    esac
+done
+
+
 # Make sure we can actually do anything meaningful:
 SUDO=''
 if (( $EUID != 0 )); then
@@ -12,8 +35,9 @@ if (( $EUID != 0 )); then
 fi
 
 # Check/install essential prerequisites:
-preq="dialog fzf gawk grep"
 pinst=""
+preq="fzf gawk grep"
+[[ "$use_dialog" == "1" ]] && preq="dialog $preq"
 for cmd in $preq ; do
     if ! command -pv "$cmd" > /dev/null ; then
         pinst="$pinst $cmd"
@@ -47,6 +71,7 @@ trap cleanup SIGHUP
 
 
 # Create resource file for dialog:
+if [[ "$use_dialog" == "1" ]] ; then
 echo "# dialogrc
 use_colors = ON
 use_shadow = OFF
@@ -72,7 +97,7 @@ tag_key_selected_color = (RED,WHITE,ON)
 border2_color = dialog_color
 menubox_border2_color = dialog_color
 " > $tmp/dialogrc
-
+fi
 
 # Update repository package database:
 function syncRepo() {
@@ -86,13 +111,13 @@ function distUpgrade() {
 
 # Clean cache by removing obsolete binary packages:
 function cleanCache() {
-       echo "Cleaning package cache ..."
+    echo "Cleaning package cache ..."
     $SUDO xbps-remove -Ov
 }
 
 # Remove orphaned packages:
 function reapOrphans() {
-       echo "Searching for orphans ..."
+    echo "Searching for orphans ..."
     $SUDO xbps-remove -ov
 }
 
@@ -338,42 +363,78 @@ TAB toggle | ENTER proceed | ESC cancel" \
 
 # Main menu:
 function menu() {
-
-    # NOTE: Using explicit output fd redirection, as dialog's --stdout
-    # option might fail in unexpected ways.
-    exec 3>&1
-    choice=$( \
-        DIALOGRC=$tmp/dialogrc \
-        dialog \
-        --backtitle 'XbpsUI v2' \
-        --begin 1 0 \
-        --cancel-label 'Quit' \
-        --colors \
-        --no-lines \
-        --no-shadow \
-        --menu '\Zb\ZuSelect Operation:' 20 60 15 \
-        'sync'      'Synchronize repository package database' \
-        'upgrade'   'Update all packages to their latest version' \
-        'install'   'Install a new package' \
-        'reinstall' 'Re-install an already installed package' \
-        'purge'     'Remove a currently installed package' \
-        'reconfig'  'Forcibly reconfigure an installed package' \
-        'clean'     'Remove obsolete packages from cache' \
-        'orphan'    'Remove orphaned packages' \
-        'manual'    'Mark packages as manually installed' \
-        'auto'      'Mark packages as automatically installed' \
-        'hold'      'Set packages on hold' \
-        'unhold'    'Unhold held packages' \
-        2>&1 1>&3 \
-    )
-    res=$?
-    exec 3>&-
-    clear
-    if (( $res != 0 )) ; then
-        # quit / cancel dialog
-        exit 0
+    if [[ -n $use_dialog ]] ; then
+        # Use dialog(1) for main menu.
+        # NOTE: Using explicit output redirection, as dialog's --stdout
+        # option allegedly might fail in unexpected ways.  Go figure.
+        exec 3>&1
+        choice=$( \
+            DIALOGRC=$tmp/dialogrc \
+            dialog \
+            --title "$verstr" \
+            --begin 1 0 \
+            --cancel-label 'Quit' \
+            --colors \
+            --no-lines \
+            --no-shadow \
+            --menu '' 19 60 15 \
+            'sync'      'Synchronize repository package database' \
+            'upgrade'   'Update all packages to their latest version' \
+            'install'   'Install a new package' \
+            'reinstall' 'Re-install an already installed package' \
+            'purge'     'Remove a currently installed package' \
+            'reconfig'  'Forcibly reconfigure an installed package' \
+            'clean'     'Remove obsolete packages from cache' \
+            'orphan'    'Remove orphaned packages' \
+            'manual'    'Mark packages as manually installed' \
+            'auto'      'Mark packages as automatically installed' \
+            'hold'      'Set packages on hold' \
+            'unhold'    'Unhold held packages' \
+            2>&1 1>&3 \
+        )
+        res=$?
+        exec 3>&-
+        clear
+        if (( $res != 0 )) ; then
+            # quit / cancel dialog
+            exit 0
+        fi
+    else
+        # Use fzf(1) for main menu.
+        nl=$'\n'
+        hstr="$nl$verstr$nl$nl"
+        choice="$( echo \
+$'sync        Synchronize repository package database
+upgrade     Update all packages to their latest version
+install     Install a new package
+reinstall   Re-install an already installed package
+purge       Remove a currently installed package
+reconfig    Forcibly reconfigure an installed package
+clean       Remove obsolete packages from cache
+orphan      Remove orphaned packages
+manual      Mark packages as manually installed
+auto        Mark packages as automatically installed
+hold        Set packages on hold
+unhold      Unhold held packages
+quit        Quit' |
+            fzf -i \
+            --no-multi \
+            --exact \
+            --no-sort \
+            --cycle \
+            --reverse \
+            --margin="4%,1%,1%,2%" \
+            --inline-info \
+            --header="$hstr" |
+            awk '{ print $1 }'
+        )"
+        res=$?
+        if [[ -z "$choice" ]] ; then
+            # quit / cancel dialog
+            exit 0
+        fi
     fi
-
+    
     # Perform requested operation:
     case "$choice" in
         "sync" )       syncRepo      ;;
@@ -388,10 +449,10 @@ function menu() {
         "auto" )       markPkgAuto   ;;
         "hold" )       holdPkg       ;;
         "unhold" )     unholdPkg     ;;
-        * )            echo "Oops, unknown operation!?!" ; false ;;
+        "quit" )       exit 0        ;;
+        * )            echo "Unknown operation, WTF!?!" ; false ;;
     esac
-    res=$?
-    return $res
+    return $?
 }
 
 # Report exit status of an operation:
@@ -406,8 +467,7 @@ function statrep() {
     read
 }
 
-# Main loop
-
+# Main loop:
 while : ; do
     menu
     statrep $?