From e64a857a436ed6287218fd07c3a9109164107aed Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Thu, 7 May 2026 11:52:53 +0200 Subject: [PATCH] fix too long long on non functionnal networt (and improve dwl) --- profile.d/net.sh | 75 +++++++++++++++++++++++++++++++------------- profile.d/updates.sh | 9 ++++-- 2 files changed, 60 insertions(+), 24 deletions(-) diff --git a/profile.d/net.sh b/profile.d/net.sh index 166949c..846dd6e 100644 --- a/profile.d/net.sh +++ b/profile.d/net.sh @@ -36,27 +36,52 @@ # ------------------------------------------------------------------------------ # Download a resource using curl, wget, or fetch. -# Usage: dwl [output_file] +# Usage: dwl [-t ] [output_file] dwl() { - case "$1" in - --help|-h) - echo "Usage: dwl [output_file]" - echo "Downloads a resource using curl, wget, or fetch." - echo "" - echo "Arguments:" - echo " url The full URL to download (http/https/ftp)." - echo " output_file (Optional) Path to save the file. If omitted, prints to stdout." - return 0 - ;; + local timeout="" + + # Parse leading options before the URL. + while [[ $# -gt 0 ]]; do + case "$1" in + --help|-h) + echo "Usage: dwl [-t |--timeout ] [output_file]" + echo "Downloads a resource using curl, wget, or fetch." + echo "" + echo "Arguments:" + echo " -t, --timeout Maximum time in seconds to wait for the transfer." + echo " url The full URL to download (http/https/ftp)." + echo " output_file (Optional) Path to save the file. If omitted, prints to stdout." + return 0 + ;; + -t|--timeout) + [[ -z "${2:-}" || ! "${2:-}" =~ ^[0-9]+$ ]] && { + echo "Error: --timeout requires a positive integer argument." >&2 + return 1 + } + timeout="$2" + shift 2 + ;; + --) + shift + break + ;; + -*) + echo "Error: Unknown option '$1'. Try 'dwl --help'." >&2 + return 1 + ;; + *) + break + ;; + esac + done + + case "${1:-}" in "") echo "Error: URL argument is missing." >&2 - echo "Try 'get_resource --help' for usage." >&2 + echo "Try 'dwl --help' for usage." >&2 return 1 ;; - esac - - case "$1" in http://*|https://*|ftp://*) ;; *) echo "Error: '$1' does not look like a valid URL. Must start with http://, https://, or ftp://" >&2 @@ -65,35 +90,41 @@ dwl() esac local url="$1" - local output="$2" + local output="${2:-}" # Honour preferred tool from configuration; fall back to auto-detection. local preferred="${DWL_PREFERRED_TOOL:-}" _try_curl() { + local args=(-sL) + [[ -n "$timeout" ]] && args+=(--max-time "$timeout" --connect-timeout "$timeout") if [[ -z "$output" ]]; then - curl -sL "$url" + curl "${args[@]}" "$url" else - curl -sL -o "$output" "$url" + curl "${args[@]}" -o "$output" "$url" fi } _try_wget() { + local args=(-q) + [[ -n "$timeout" ]] && args+=(--timeout="$timeout") if [[ -z "$output" ]]; then - wget -qO- "$url" + wget "${args[@]}" -O- "$url" else - wget -q -O "$output" "$url" + wget "${args[@]}" -O "$output" "$url" fi } _try_fetch() { + local args=() + [[ -n "$timeout" ]] && args+=(-T "$timeout") if [[ -z "$output" ]]; then - fetch -o - "$url" + fetch "${args[@]}" -o - "$url" else - fetch -o "$output" "$url" + fetch "${args[@]}" -o "$output" "$url" fi } diff --git a/profile.d/updates.sh b/profile.d/updates.sh index 37d9fc6..611c8c3 100644 --- a/profile.d/updates.sh +++ b/profile.d/updates.sh @@ -90,9 +90,14 @@ check_updates() return 4 } - dwl "$UPDT_URL/version" "$vfile" >/dev/null 2>&1 || { + # In quiet mode (startup), use a short timeout so a missing or slow network + # never blocks the interactive prompt. + local dwl_opts=() + (( quiet == 1 )) && dwl_opts+=(-t 3) + + dwl "${dwl_opts[@]}" "$UPDT_URL/version" "$vfile" >/dev/null 2>&1 || { rm -f "$vfile" - disp E "Cannot download version file; unable to continue." + (( quiet != 1 )) && disp E "Cannot download version file; unable to continue." return 5 }