Browse Source

Handle the cancel button or an empty list after dialog

When the user selects cancel or presses esc, exit nicely.
When the user selects ok with an empty list of artists, inform him and
exit with 1.
Matteo Zeccoli Marazzini 4 years ago
parent
commit
bbd1ed2561
1 changed files with 16 additions and 5 deletions
  1. 16 5
      vaporget

+ 16 - 5
vaporget

@@ -16,19 +16,30 @@
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 
 WEBSITE="https://vaporwave.ivan.moe/list/"
-# Save current virtual terminal for later
-TTY=$(tty)
 
 # Fetch the artist list from the website and let the user select from a menu
-wget -qO- $WEBSITE 2>/dev/null \
+CHOICES=$(wget -qO- $WEBSITE 2>/dev/null \
 	| grep "^<a href" \
 	| sed -e 's/<a href="//' \
 	      -e 's/">/\n/' \
 	      -e 's/&gt;<\/a>.*/\noff/' \
 	      -e 's/\/<\/a>.*/\noff/' \
-	| xargs -d '\n' dialog --title "VAPORGET" --checklist "Please, select an artist:" 0 0 0 3>&1 1>&2 2>&3 3>&- \
+	| xargs -d '\n' dialog --title "VAPORGET" --checklist "Please, select an artist:" 0 0 0 3>&1 1>&2 2>&3 3>&-)
+
+if [ $? != 0 ]; then	# User selected cancel, therefore we exit
+	clear
+	exit 0
+fi
+
+clear
+if [ -z "${CHOICES}" ]; then	# User didn't select any artist
+	echo "Please, select at least one artist." >&2
+	exit 1
+fi
+
+echo "${CHOICES}" \
 	| sed -e 's/ /\n/g' -e '$a\' \
-	| { sed -e 's,^,'"${WEBSITE}"','; clear > $TTY; } \
+	| sed -e 's,^,'"${WEBSITE}"',' \
 	| wget -i - --spider -r -l inf --no-parent --no-directories 2>&1 \
 	| grep --line-buffered '^--' \
 	| stdbuf -oL cut -d' ' -f4 \