-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #20
- Loading branch information
Showing
6 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#' Add a progress bar to long downloads or uploads | ||
#' | ||
#' When uploading or downloading a large file, it's often useful to | ||
#' provide a progress bar so that you know how long you have to wait. | ||
#' | ||
#' @inheritParams req_headers | ||
#' @param type Type of progress to display: either number of bytes uploaded | ||
#' or downloaded. | ||
#' @export | ||
#' @examples | ||
#' req <- request("https://r4ds.s3.us-west-2.amazonaws.com/seattle-library-checkouts.csv") %>% | ||
#' req_progress() | ||
#' | ||
#' \dontrun{ | ||
#' path <- tempfile() | ||
#' req %>% req_perform(path = path) | ||
#' } | ||
req_progress <- function(req, type = c("down", "up")) { | ||
type <- arg_match(type) | ||
|
||
# https://curl.se/libcurl/c/CURLOPT_XFERINFOFUNCTION.html | ||
req_options(req, | ||
noprogress = FALSE, | ||
xferinfofunction = make_progress(type) | ||
) | ||
} | ||
|
||
make_progress <- function(type, frame = caller_env()) { | ||
force(type) | ||
init <- FALSE | ||
|
||
function(down, up) { | ||
if (type == "down") { | ||
total <- down[[1]] | ||
now <- down[[2]] | ||
} else { | ||
total <- up[[1]] | ||
now <- up[[2]] | ||
} | ||
|
||
if (total == 0 && now == 0) { | ||
init <<- FALSE | ||
return(TRUE) | ||
} | ||
|
||
if (!init) { | ||
init <<- TRUE | ||
if (total == 0) { | ||
cli::cli_progress_bar( | ||
format = "Downloading {cli::pb_spin}", | ||
.envir = frame | ||
) | ||
} else { | ||
cli::cli_progress_bar( | ||
format = "Downloading {cli::pb_percent} {cli::pb_bar} {cli::pb_eta}", | ||
total = total, | ||
.envir = frame | ||
) | ||
} | ||
} | ||
|
||
if (now < total && total > 0) { | ||
cli::cli_progress_update(set = now, .envir = frame) | ||
} else { | ||
cli::cli_progress_done(.envir = frame) | ||
} | ||
|
||
TRUE | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.