|
| 1 | +#' Visual Progression Feedback |
| 2 | +#' |
| 3 | +#' A progression handler for [pbmcapply::progressBar()]. |
| 4 | +#' |
| 5 | +#' @inheritParams make_progression_handler |
| 6 | +#' |
| 7 | +#' @param substyle (integer) The progress-bar substyle according to [pbmcapply::progressBar()]. |
| 8 | +#' |
| 9 | +#' @param style (character) The progress-bar style according to [pbmcapply::progressBar()]. |
| 10 | +#' |
| 11 | +#' @param file (connection) A [base::connection] to where output should be sent. |
| 12 | +#' |
| 13 | +#' @param \ldots Additional arguments passed to [make_progression_handler()]. |
| 14 | +#' |
| 15 | +#' @example incl/pbmcapply_handler.R |
| 16 | +#' |
| 17 | +#' @section Requirements: |
| 18 | +#' This progression handler requires the \pkg{pbmcapply} package. |
| 19 | +#' |
| 20 | +#' @importFrom utils file_test flush.console txtProgressBar setTxtProgressBar |
| 21 | +#' @export |
| 22 | +pbmcapply_handler <- function(substyle = 3L, style = "ETA", file = stderr(), intrusiveness = getOption("progressr.intrusiveness.terminal", 1), target = "terminal", ...) { |
| 23 | + if (!is_fake("pbmcapply_handler")) { |
| 24 | + progressBar <- pbmcapply::progressBar |
| 25 | + eraseTxtProgressBar <- function(pb) { |
| 26 | + pb_env <- environment(pb$getVal) |
| 27 | + with(pb_env, { |
| 28 | + style_eta <- exists(".time0", inherits = FALSE) |
| 29 | + if (!style_eta) { |
| 30 | + if (style == 1L || style == 2L) { |
| 31 | + n <- .nb |
| 32 | + } else if (style == 3L) { |
| 33 | + n <- 3L + nw * width + 6L |
| 34 | + } |
| 35 | + } else { |
| 36 | + ## FIXME: Seems to work; if not, see pbmcapply:::txtProgressBarETA() |
| 37 | + n <- width |
| 38 | + } |
| 39 | + cat("\r", strrep(" ", times = n), "\r", sep = "", file = file) |
| 40 | + flush.console() |
| 41 | + }) |
| 42 | + } |
| 43 | + } else { |
| 44 | + progressBar <- function(..., style, substyle) txtProgressBar(..., style = substyle) |
| 45 | + setTxtProgressBar <- function(...) NULL |
| 46 | + eraseTxtProgressBar <- function(pb) NULL |
| 47 | + } |
| 48 | + |
| 49 | + reporter <- local({ |
| 50 | + ## Import functions |
| 51 | + |
| 52 | + pb <- NULL |
| 53 | + |
| 54 | + make_pb <- function(...) { |
| 55 | + if (!is.null(pb)) return(pb) |
| 56 | + pb <<- progressBar(...) |
| 57 | + pb |
| 58 | + } |
| 59 | + |
| 60 | + list( |
| 61 | + reset = function(...) { |
| 62 | + pb <<- NULL |
| 63 | + }, |
| 64 | + |
| 65 | + initiate = function(config, state, progression, ...) { |
| 66 | + if (!state$enabled || config$times == 1L) return() |
| 67 | + make_pb(max = config$max_steps, style = style, substyle = substyle, file = file) |
| 68 | + }, |
| 69 | + |
| 70 | + update = function(config, state, progression, ...) { |
| 71 | + if (!state$enabled || progression$amount == 0 || config$times <= 2L) return() |
| 72 | + make_pb(max = config$max_steps, style = style, substyle = substyle, file = file) |
| 73 | + setTxtProgressBar(pb, value = state$step) |
| 74 | + }, |
| 75 | + |
| 76 | + finish = function(config, state, progression, ...) { |
| 77 | + ## Already finished? |
| 78 | + if (is.null(pb)) return() |
| 79 | + if (!state$enabled) return() |
| 80 | + if (config$clear) { |
| 81 | + eraseTxtProgressBar(pb) |
| 82 | + ## Suppress newline outputted by close() |
| 83 | + pb_env <- environment(pb$getVal) |
| 84 | + file <- pb_env$file |
| 85 | + pb_env$file <- tempfile() |
| 86 | + on.exit({ |
| 87 | + if (file_test("-f", pb_env$file)) file.remove(pb_env$file) |
| 88 | + pb_env$file <- file |
| 89 | + }) |
| 90 | + } else { |
| 91 | + setTxtProgressBar(pb, value = state$step) |
| 92 | + } |
| 93 | + close(pb) |
| 94 | + pb <<- NULL |
| 95 | + } |
| 96 | + ) |
| 97 | + }) |
| 98 | + |
| 99 | + make_progression_handler("pbmcapply", reporter, intrusiveness = intrusiveness, ...) |
| 100 | +} |
0 commit comments