From 1b4c540448d2212bf33f610fb40209854b32221b Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 22 May 2020 20:34:27 -0500 Subject: Change file descriptors and add functions --- bollux | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/bollux b/bollux index 5ed99ba..b027eab 100755 --- a/bollux +++ b/bollux @@ -65,6 +65,13 @@ die() { # die [EXIT-CODE] MESSAGE exit "$ec" } +# ask the user for input +ask() { # ask PROMPT [READ_OPT...] + prompt="$1" + shift + read -e -r -u 3 -p "$prompt> " "$@" +} + # fail if something isn't installed require() { hash "$1" 2>/dev/null || die 127 "Requirement '$1' not found."; } @@ -135,7 +142,7 @@ handle() { # handle URL < RESPONSE 1*) # INPUT log 3 "Input" put "$meta" - read -rep "? " + ask "?" bollux "$URL?$REPLY" ;; 2*) # SUCCESS @@ -219,44 +226,48 @@ display() { # display MIMETYPE < DOCUMENT # ($LESSOPEN, $LESSCLOSE) ;; *) - tn="$(mktemp)" - dd status=progress >"$tn" - fn="$DLDR/${URL##*/}" - if [[ -f "$fn" ]]; then - log - "Saved '$tn'." - else - if mv "$tn" "$fn"; then - log - "Saved '$fn'." - else - log 0 "Error saving '$fn'." - fi - fi + download "$URL" ;; esac } +download() { # download URL < FILE + tn="$(mktemp)" + dd status=progress >"$tn" + fn="$DLDR/${URL##*/}" + if [[ -f "$fn" ]]; then + log - "Saved '$tn'." + else + if mv "$tn" "$fn"; then + log - "Saved '$fn'." + else + log 0 "Error saving '$fn'." + log - "Saved '$tn'." + fi + fi +} + ### main entry point ### bollux() { - bollux_setup + # use &3 for user input + exec 3<>/dev/tty if (($# == 1)); then URL="$1" else - read -r -u 3 -p "GO> " URL + ask GO URL fi log 5 "URL : $URL" - request "$URL" >.resource & - handle "$URL" <.resource - - bollux_cleanup + request "$URL" | handle "$URL" } bollux_setup() { mkfifo .resource trap bollux_cleanup INT QUIT TERM EXIT } + bollux_cleanup() { echo rm -f .resource @@ -272,4 +283,5 @@ if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then require sed bollux "$@" + echo fi -- cgit 1.4.1-21-gabe81