Skip to content

Commit 9c18fdd

Browse files
pbcol: harmonize argument names with 'progress::progress_bar()' and now take functions (typically 'crayon' ones) instead of strings for more flexibility
1 parent a61323a commit 9c18fdd

File tree

2 files changed

+21
-56
lines changed

2 files changed

+21
-56
lines changed

R/handler_pbcol.R

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
#' @param pad (integer) Amount of padding on each side of the message,
1010
#' where padding is done by spaces.
1111
#'
12-
#' @param text_col,done_col,todo_col (character string) The \pkg{crayon}
13-
#' foreground and background colors used for the progress bar, where
14-
#' `text_col` is used for all of the progress bar, `done_col` is used for the
15-
#' part of the progress bar that is already done and `todo_col` for what
16-
#' remains.
12+
#' @param complete,incomplete (function) Functions that take "complete" and
13+
#' "incomplete" strings that comprise the progress bar as input and annotate
14+
#' them to reflect their two different parts. The default is to annotation
15+
#' them with two different background colors and the same foreground color
16+
#' using the \pkg{crayon} package.
1717
#'
1818
#' @param \ldots Additional arguments passed to [make_progression_handler()].
1919
#'
@@ -24,9 +24,9 @@
2424
#'
2525
#' @importFrom utils flush.console
2626
#' @export
27-
handler_pbcol <- function(adjust = 0.0, pad = 1L, text_col = "white", done_col = "blue", todo_col = "cyan", intrusiveness = getOption("progressr.intrusiveness.terminal", 1), target = "terminal", ...) {
28-
crayon_enabled <- getOption("crayon.enabled", NA)
29-
if (is.na(crayon_enabled)) crayon_enabled <- crayon::has_color()
27+
handler_pbcol <- function(adjust = 0.0, pad = 1L, complete = function(s) crayon::bgBlue(crayon::white(s)), incomplete = function(s) crayon::bgCyan(crayon::white(s)), intrusiveness = getOption("progressr.intrusiveness.terminal", 1), target = "terminal", ...) {
28+
crayon_enabled <- getOption("crayon.enabled", NULL)
29+
if (is.null(crayon_enabled)) crayon_enabled <- crayon::has_color()
3030

3131
cat_ <- function(...) {
3232
cat(..., sep = "", collapse = "", file = stderr())
@@ -39,7 +39,7 @@ handler_pbcol <- function(adjust = 0.0, pad = 1L, text_col = "white", done_col =
3939

4040
redraw_progress_bar <- function(ratio, message, spin = " ") {
4141
stop_if_not(ratio >= 0, ratio <= 1)
42-
if (crayon_enabled) {
42+
if (crayon_enabled && !is.null(getOption("crayon.enabled", NULL))) {
4343
options(crayon.enabled = TRUE)
4444
on.exit(options(crayon.enabled = TRUE), add = TRUE)
4545
}
@@ -48,9 +48,8 @@ handler_pbcol <- function(adjust = 0.0, pad = 1L, text_col = "white", done_col =
4848
msg = message,
4949
adjust = adjust,
5050
pad = pad,
51-
text_col = text_col,
52-
done_col = done_col,
53-
todo_col = todo_col,
51+
complete = complete,
52+
incomplete = incomplete,
5453
spin = spin,
5554
)
5655
cat_("\r", pbstr)
@@ -96,38 +95,7 @@ handler_pbcol <- function(adjust = 0.0, pad = 1L, text_col = "white", done_col =
9695

9796

9897

99-
pbcol <- function(fraction = 0.0, msg = "", adjust = 0, pad = 1L, width = getOption("width") - 1L, text_col = "white", done_col = "blue", todo_col = "cyan", spin = " ") {
100-
bgColor <- function(s, col) {
101-
bgFcn <- switch(col,
102-
black = crayon::bgBlack,
103-
blue = crayon::bgBlue,
104-
cyan = crayon::bgCyan,
105-
green = crayon::bgGreen,
106-
magenta = crayon::bgMagenta,
107-
red = crayon::bgRed,
108-
yellow = crayon::bgYellow,
109-
white = crayon::bgWhite,
110-
stop("Unknown 'crayon' background color: ", sQuote(col))
111-
)
112-
bgFcn(s)
113-
}
114-
115-
fgColor <- function(s, col) {
116-
fgFcn <- switch(col,
117-
black = crayon::black,
118-
blue = crayon::blue,
119-
cyan = crayon::cyan,
120-
green = crayon::green,
121-
magenta = crayon::magenta,
122-
red = crayon::red,
123-
silver = crayon::silver,
124-
yellow = crayon::yellow,
125-
white = crayon::white,
126-
stop("Unknown 'crayon' foreground color: ", sQuote(col))
127-
)
128-
fgFcn(s)
129-
}
130-
98+
pbcol <- function(fraction = 0.0, msg = "", adjust = 0, pad = 1L, width = getOption("width") - 1L, complete = function(s) crayon::bgBlue(crayon::white(s)), incomplete = function(s) crayon::bgCyan(crayon::white(s)), spin = " ") {
13199
if (length(msg) == 0L) msg <- ""
132100
stop_if_not(length(msg) == 1L, is.character(msg))
133101

@@ -163,10 +131,8 @@ pbcol <- function(fraction = 0.0, msg = "", adjust = 0, pad = 1L, width = getOpt
163131
len <- round(fraction * nchar(pmsg), digits = 0L)
164132
lmsg <- substr(pmsg, start = 1L, stop = len)
165133
rmsg <- substr(pmsg, start = len + 1L, stop = nchar(pmsg))
166-
lmsg <- bgColor(lmsg, done_col)
167-
rmsg <- bgColor(rmsg, todo_col)
168-
lmsg <- fgColor(lmsg, text_col)
169-
rmsg <- fgColor(rmsg, text_col)
134+
if (!is.null(complete)) lmsg <- complete(lmsg)
135+
if (!is.null(incomplete)) rmsg <- incomplete(rmsg)
170136
bar <- paste(lmsg, rmsg, sep = "")
171137

172138
bar

man/handler_pbcol.Rd

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)