add resume support to dwl

This commit is contained in:
fatalerrors
2026-05-20 16:28:32 +02:00
parent 8bfb3272c0
commit 5f8800ab44
5 changed files with 37 additions and 3 deletions

View File

@@ -36,20 +36,29 @@
# ------------------------------------------------------------------------------
# Download a resource using curl, wget, or fetch.
# Usage: dwl [-t <seconds>] <url> [output_file]
# Usage: dwl [-t <seconds>] [-r|--resume] <url> [output_file]
dwl()
{
local timeout=""
local resume=0
case "${DWL_DEFAULT_RESUME,,}" in
1|true|yes|on)
resume=1
;;
esac
# Parse leading options before the URL.
while [[ $# -gt 0 ]]; do
case "$1" in
--help|-h)
echo "Usage: dwl [-t <seconds>|--timeout <seconds>] <url> [output_file]"
echo "Usage: dwl [-t <seconds>|--timeout <seconds>] [-r|--resume] [--no-resume] <url> [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 " -r, --resume Resume an interrupted download when possible (file output only)."
echo " --no-resume Disable resume mode even if enabled in config."
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
@@ -62,6 +71,14 @@ dwl()
timeout="$2"
shift 2
;;
-r|--resume)
resume=1
shift
;;
--no-resume)
resume=0
shift
;;
--)
shift
break
@@ -100,8 +117,12 @@ dwl()
local args=(-sL)
[[ -n "$timeout" ]] && args+=(--max-time "$timeout" --connect-timeout "$timeout")
if [[ -z "$output" ]]; then
if (( resume == 1 )); then
echo "Warning: --resume requires an output file; ignoring resume mode for stdout." >&2
fi
curl "${args[@]}" "$url"
else
(( resume == 1 )) && args+=(-C -)
curl "${args[@]}" -o "$output" "$url"
fi
}
@@ -110,6 +131,7 @@ dwl()
{
local args=(-q)
[[ -n "$timeout" ]] && args+=(--timeout="$timeout")
(( resume == 1 )) && args+=(-c)
if [[ -z "$output" ]]; then
wget "${args[@]}" -O- "$url"
else
@@ -121,6 +143,9 @@ dwl()
{
local args=()
[[ -n "$timeout" ]] && args+=(-T "$timeout")
if (( resume == 1 )); then
echo "Warning: resume mode is not supported with fetch; continuing without resume." >&2
fi
if [[ -z "$output" ]]; then
fetch "${args[@]}" -o - "$url"
else