Title: | A Collection of Helper Functions for the cli package |
---|---|
Description: | Extends the cli package with user response prompts and stopifnot equivalents. |
Authors: | Eli Pousson [aut, cre, cph] |
Maintainer: | Eli Pousson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9001 |
Built: | 2024-10-29 03:15:57 UTC |
Source: | https://github.com/elipousson/cliExtras |
Signal an error, warning, or message with a cli formatted message if any expressions in ... are not all TRUE or are all TRUE
cli_abort_ifnot( ..., message = NULL, call = .envir, .envir = parent.frame(), .frame = .envir, condition = NULL ) cli_abort_if( ..., message = NULL, call = .envir, .envir = parent.frame(), .frame = .envir, condition = NULL ) cli_warn_ifnot(..., message = NULL, .envir = parent.frame(), condition = NULL) cli_warn_if(..., message = NULL, .envir = parent.frame(), condition = NULL) cli_inform_ifnot( ..., message = NULL, .envir = parent.frame(), condition = NULL ) cli_inform_if(..., message = NULL, .envir = parent.frame(), condition = NULL)
cli_abort_ifnot( ..., message = NULL, call = .envir, .envir = parent.frame(), .frame = .envir, condition = NULL ) cli_abort_if( ..., message = NULL, call = .envir, .envir = parent.frame(), .frame = .envir, condition = NULL ) cli_warn_ifnot(..., message = NULL, .envir = parent.frame(), condition = NULL) cli_warn_if(..., message = NULL, .envir = parent.frame(), condition = NULL) cli_inform_ifnot( ..., message = NULL, .envir = parent.frame(), condition = NULL ) cli_inform_if(..., message = NULL, .envir = parent.frame(), condition = NULL)
... |
Any number of R expressions which should each evaluate to a
logical vector. If |
message |
It is formatted via a call to |
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
.envir |
Environment to evaluate the glue expressions in. |
.frame |
The throwing context. Used as default for
|
condition |
Logical. For |
Alerts are typically short status messages. Note: These functions use wrap = TRUE
by default. Alert messages can be muffled by set_cli_quiet()
while
cli::cli_inform()
messages are not.
cli_alert_ifnot( text = NULL, condition = NULL, .fn = cli::cli_alert, id = NULL, class = NULL, wrap = TRUE, .envir = parent.frame(), call = parent.frame() ) cli_danger_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_info_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_success_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_warning_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_alert_if( text = NULL, condition = NULL, .fn = cli::cli_alert, id = NULL, class = NULL, wrap = TRUE, .envir = parent.frame(), call = parent.frame() ) cli_danger_if(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_info_if(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_success_if(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_warning_if(text = NULL, condition = NULL, ..., .envir = parent.frame())
cli_alert_ifnot( text = NULL, condition = NULL, .fn = cli::cli_alert, id = NULL, class = NULL, wrap = TRUE, .envir = parent.frame(), call = parent.frame() ) cli_danger_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_info_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_success_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_warning_ifnot(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_alert_if( text = NULL, condition = NULL, .fn = cli::cli_alert, id = NULL, class = NULL, wrap = TRUE, .envir = parent.frame(), call = parent.frame() ) cli_danger_if(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_info_if(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_success_if(text = NULL, condition = NULL, ..., .envir = parent.frame()) cli_warning_if(text = NULL, condition = NULL, ..., .envir = parent.frame())
text |
Text of the alert. |
condition |
If |
.fn |
cli function to use for alert. Defaults to |
id |
Id of the alert element. Can be used in themes. |
class |
Class of the alert element. Can be used in themes. |
wrap |
Whether to auto-wrap the text of the alert. |
.envir |
Environment to evaluate the glue expressions in. |
call |
Caller environment. Used to improve error messages for argument checks. |
... |
Additional parameters passed to cli_alert_if or cli_alert_ifnot by
functions like |
## Not run: if (interactive()) { cli_alert_if(text = "Example text", condition = TRUE) cli_alert_ifnot(text = "Example text", condition = FALSE) cli_alert_success_if(text = "Success!", condition = TRUE) cli_alert_danger_ifnot(text = "Danger!", condition = FALSE) } ## End(Not run)
## Not run: if (interactive()) { cli_alert_if(text = "Example text", condition = TRUE) cli_alert_ifnot(text = "Example text", condition = FALSE) cli_alert_success_if(text = "Success!", condition = TRUE) cli_alert_danger_ifnot(text = "Danger!", condition = FALSE) } ## End(Not run)
Display a Message then Read a Line from the Terminal
cli_ask(prompt = "?", ..., .envir = rlang::caller_env(), call = .envir)
cli_ask(prompt = "?", ..., .envir = rlang::caller_env(), call = .envir)
prompt |
Characters to show as user prompt in console following
displayed message. A non-breaking space is always placed after the prompt
before passing to |
... |
Arguments passed on to
|
.envir |
Environment to evaluate the glue expressions in. |
call |
The execution environment of a currently
running function, e.g. |
List of items using bulletize helper
cli_bulletize( items, bullet = "*", n_show = Inf, n_fudge = 2, style = NULL, sep = NULL, before = NULL, after = NULL, id = NULL, class = NULL, .envir = parent.frame() )
cli_bulletize( items, bullet = "*", n_show = Inf, n_fudge = 2, style = NULL, sep = NULL, before = NULL, after = NULL, id = NULL, class = NULL, .envir = parent.frame() )
items |
A named vector or list to use in creating a bulletted list with
|
bullet |
Character to use for bullet. Defaults to "". |
n_show |
The maximum number of items to include in the bullet list. Defaults to 5. |
n_fudge |
The minimum number of items to include in summary of additional bullet items. If the summary would only include a number of items equal or less than n_fudge, they are included in the bullet list and the summary is not displayed. Defaults to 2. |
style |
A cli style name, e.g. code, val, file, url |
sep , before , after
|
Additional characters or character vectors applied
using |
id |
Optional id of the |
class |
Optional additional class(es) for the |
.envir |
Environment to evaluate the glue expressions in. |
These functions are convenient alternatives to the internal cli functions but may be removed as my understanding of the cli package improves.
bracketize()
pastes open and close brackets around a vector.
stylize()
appends a style prefix, e.g. "{.val value}
" to a vector.
bulletize()
turns a vector into a neatly abbreviated bullet list.
bulletize()
is adapted from the gargle package (see
utils-ui.R
).
This function is available under a MIT license and is the work of the gargle
authors.
bracketize(..., .open = "{", .close = "}", collapse = NULL) stylize( x, style = NULL, bracket = FALSE, .open = "{", .close = "}", collapse = NULL ) bulletize( x, bullet = "*", sep = NULL, before = NULL, after = NULL, style = NULL, n_show = 5, n_fudge = 2, bracket = FALSE )
bracketize(..., .open = "{", .close = "}", collapse = NULL) stylize( x, style = NULL, bracket = FALSE, .open = "{", .close = "}", collapse = NULL ) bulletize( x, bullet = "*", sep = NULL, before = NULL, after = NULL, style = NULL, n_show = 5, n_fudge = 2, bracket = FALSE )
... |
one or more R objects, to be converted to character vectors. |
.open , .close
|
Open and close bracket characters. |
collapse |
an optional character string to separate the results. Not
|
x |
A vector. |
style |
A cli style name, e.g. code, val, file, url |
bracket |
If |
bullet |
Character to use for bullet. Defaults to "". |
sep , before , after
|
Additional characters or character vectors applied
using |
n_show |
The maximum number of items to include in the bullet list. Defaults to 5. |
n_fudge |
The minimum number of items to include in summary of additional bullet items. If the summary would only include a number of items equal or less than n_fudge, they are included in the bullet list and the summary is not displayed. Defaults to 2. |
bracketize("value") stylize("styled value", "val") stylize("styled variable", "val", bracket = TRUE) bulletize(c("val 1", "val 2", "val 3")) bulletize(rep("val", 20), n_show = 3)
bracketize("value") stylize("styled value", "val") stylize("styled variable", "val", bracket = TRUE) bulletize(c("val 1", "val 2", "val 3")) bulletize(rep("val", 20), n_show = 3)
Execute a function if a predicate function returns TRUE. Intended for use with cli functions.
cli_if( x = NULL, ..., .predicate = rlang::is_true, .fn = NULL, .default = cli::cli_alert, .envir = rlang::caller_env(), call = rlang::caller_env() ) cli_ifnot( x = NULL, ..., .predicate = rlang::is_false, .fn = NULL, .default = cli::cli_alert, .envir = rlang::caller_env(), call = rlang::caller_env() )
cli_if( x = NULL, ..., .predicate = rlang::is_true, .fn = NULL, .default = cli::cli_alert, .envir = rlang::caller_env(), call = rlang::caller_env() ) cli_ifnot( x = NULL, ..., .predicate = rlang::is_false, .fn = NULL, .default = cli::cli_alert, .envir = rlang::caller_env(), call = rlang::caller_env() )
x |
Parameter to passed to .predicate function, Default: |
... |
Additional parameters passed to .fn. |
.predicate |
Single parameter predicate function, Defaults to
|
.fn |
Function to call with ... parameters if x, Default: |
.default |
Default function to execute when .predicate function returns
|
call |
The execution environment of a currently
running function, e.g. |
The output from the .fn function or .default if .fn is NULL
cli_if(FALSE, "No alert.") cli_if(TRUE, "Alert on TRUE!") cli_ifnot(FALSE, "Alert on FALSE!")
cli_if(FALSE, "No alert.") cli_if(TRUE, "Alert on TRUE!") cli_ifnot(FALSE, "Alert on FALSE!")
Display a list of files as a list of items
cli_list_files( path = NULL, files = NULL, pattern = NULL, text = NULL, bullet = "*", n_show = 10, show_full = FALSE, include_dirs = TRUE, return_list = FALSE, .envir = current_env(), ... )
cli_list_files( path = NULL, files = NULL, pattern = NULL, text = NULL, bullet = "*", n_show = 10, show_full = FALSE, include_dirs = TRUE, return_list = FALSE, .envir = current_env(), ... )
path |
a character vector of full path names; the default
corresponds to the working directory, |
files |
List to file names to display. Ignored if path is provided. If
path is a vector of existing files, path is used as files. If files share a
single directory, path is set to that directory, otherwise path is set to
|
pattern |
an optional regular expression. Only file names which match the regular expression will be returned. |
text |
Passed to |
bullet |
Character defining style to use list of file names. |
n_show |
Number of file names to show in list. The remaining number of files n_show are noted at the end of the list but the file names are not displayed. Defaults to 10. |
show_full |
If |
include_dirs |
If |
return_list |
If |
.envir |
Passed to |
... |
Arguments passed on to
|
cli_list_files(system.file("R", package = "cliExtras"), n_show = 5)
cli_list_files(system.file("R", package = "cliExtras"), n_show = 5)
cli_paths(path, msg)
cli_paths(path, msg)
path |
One or more file paths. |
msg |
Passed to message for |
Use cli_list_files()
instead.
A convenience function for creating progress messages that use piped data as an input.
cli_progress_pipe( data, ..., current = TRUE, clear = FALSE, .auto_close = TRUE, .envir = current_env(), time = NULL )
cli_progress_pipe( data, ..., current = TRUE, clear = FALSE, .auto_close = TRUE, .envir = current_env(), time = NULL )
data |
Input data. Reference w/ "data" if .envir is currrent_env(). |
... |
Arguments passed on to
|
current |
Passed to |
clear |
Whether to remove the progress bar from the screen after it has
terminated. Defaults to |
.auto_close |
Passed to |
.envir |
The environment to use for auto-termination and for glue
substitution. It is also used to find and set the current progress bar.
Defaults to |
time |
Passed to |
df <- data.frame("letters" = LETTERS, "numbers" = c(1:26)) df |> cli_progress_pipe("Data has {nrow(data)} rows and {ncol(data)} columns.") |> head(2)
df <- data.frame("letters" = LETTERS, "numbers" = c(1:26)) df |> cli_progress_pipe("Data has {nrow(data)} rows and {ncol(data)} columns.") |> head(2)
cli_quiet()
is a helper to enable a quiet option in other functions.
cli_quiet(quiet = FALSE, push = FALSE, .frame = rlang::caller_env())
cli_quiet(quiet = FALSE, push = FALSE, .frame = rlang::caller_env())
quiet |
If |
push |
If |
.frame |
The environment of a stack frame which defines the scope of the temporary options. When the frame returns, the options are set back to their original values. |
test_fn <- function(quiet = FALSE) { cli_quiet(quiet = quiet) cli::cli_alert_info( "{.arg quiet} is {.val {quiet}}" ) } options("cli.default_handler" = NULL) test_fn() test_fn(quiet = TRUE)
test_fn <- function(quiet = FALSE) { cli_quiet(quiet = quiet) cli::cli_alert_info( "{.arg quiet} is {.val {quiet}}" ) } options("cli.default_handler" = NULL) test_fn() test_fn(quiet = TRUE)
Format a list of items as an unordered list with cli_ul()
cli_ul_items(items, style = c("code", "val"), sep = ": ", bracket = FALSE)
cli_ul_items(items, style = c("code", "val"), sep = ": ", bracket = FALSE)
items |
Character vector of items, or |
style |
Length 2 character vector indicating cli style to use for names of items and style to use for items. |
sep |
Length 1 character vector with separator between item names and names. |
bracket |
If |
Adapted from yesno::yesno()
and usethis::ui_yeah()
to work with
cli_inform()
. This function does not respect the cliExtras.quiet option and
aborts if the session is not interactive.
cli_yesno( message, yes = c("Yes", "Definitely", "For sure", "Yup", "Yeah", "I agree", "Absolutely"), no = c("No way", "Not now", "Negative", "No", "Nope", "Absolutely not"), n_yes = 2, n_no = 1, call = .envir, .envir = rlang::caller_env() ) check_yes( prompt = NULL, yes = c("", "Y", "Yes", "Yup", "Yep", "Yeah"), message = "Aborted. A yes is required.", .envir = rlang::caller_env(), call = .envir )
cli_yesno( message, yes = c("Yes", "Definitely", "For sure", "Yup", "Yeah", "I agree", "Absolutely"), no = c("No way", "Not now", "Negative", "No", "Nope", "Absolutely not"), n_yes = 2, n_no = 1, call = .envir, .envir = rlang::caller_env() ) check_yes( prompt = NULL, yes = c("", "Y", "Yes", "Yup", "Yep", "Yeah"), message = "Aborted. A yes is required.", .envir = rlang::caller_env(), call = .envir )
message |
Passed to |
yes , no
|
Character strings with yes and no options. |
n_yes , n_no
|
Number of yes and no options to provide in user prompt. |
call |
The execution environment of a currently running
function, e.g. You only need to supply Can also be For more information about error calls, see Including function calls in error messages. |
.envir |
Environment to evaluate the glue expressions in. |
prompt |
For |
The yesno and usethis packages are both available under an MIT license (yesno LICENSE and usethis LICENSE) and are the work of the yesno and usethis authors.
TRUE
if yes response and FALSE
if no response.
cli_inform()
The implementation of the cliExtras.quiet option is based on the
implementation of a similar setting in googlesheets4, googledrive, and
gargle. If the cliExtras.quiet option is set to TRUE
, quiet_cli_inform()
does not trigger a message. It is used by cli_inform_ifnot()
and
cli_inform_if()
but not by most other functions in cliExtras.
quiet_cli_inform(..., .envir = parent.frame())
quiet_cli_inform(..., .envir = parent.frame())
... |
Additional parameters passed to |
.envir |
Environment to evaluate the glue expressions in. |
Register a custom cli format method with registerS3method
register_cli_format(class = NULL, method = NULL, envir = parent.frame())
register_cli_format(class = NULL, method = NULL, envir = parent.frame())
class , method
|
Object class and format function passed to
|
envir |
Environment where the S3 method is registered. Defaults to parent.frame(). |
set_cli_quiet(quiet = FALSE, msg = !quiet)
set_cli_quiet(quiet = FALSE, msg = !quiet)
quiet |
If |
msg |
If |