File: utils-ui.R

package info (click to toggle)
r-cran-reprex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 1,584 kB
  • sloc: sh: 13; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,671 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
reprex_quiet <- function() {
  as.logical(Sys.getenv("REPREX_QUIET", unset = "NA"))
}

local_reprex_quiet <- function(reprex_quiet = "TRUE", env = parent.frame()) {
  withr::local_envvar(c(REPREX_QUIET = reprex_quiet), .local_envir = env)
}

local_reprex_loud <- function(env = parent.frame()) {
  local_reprex_quiet("FALSE", env = env)
}

reprex_alert <- function(text,
                         type = c("success", "info", "warning", "danger"),
                         .envir = parent.frame()) {
  quiet <- reprex_quiet() %|% is_testing()
  if (quiet) {
    return(invisible())
  }
  cli_fun <- switch(
    type,
    success = cli::cli_alert_success,
    info    = cli::cli_alert_info,
    warning = cli::cli_alert_warning,
    danger  = cli::cli_alert_danger,
    cli::cli_alert
  )
  cli_fun(text = text, wrap = TRUE, .envir = .envir)
}

reprex_success <- function(text, .envir = parent.frame()) {
  reprex_alert(text, type = "success", .envir = .envir)
}

reprex_info <- function(text, .envir = parent.frame()) {
  reprex_alert(text, type = "info", .envir = .envir)
}

reprex_warning <- function(text, .envir = parent.frame()) {
  reprex_alert(text, type = "warning", .envir = .envir)
}

reprex_danger <- function(text, .envir = parent.frame()) {
  reprex_alert(text, type = "danger", .envir = .envir)
}

reprex_path <- function(header, path, .envir = parent.frame()) {
  quiet <- reprex_quiet() %|% is_testing()
  if (quiet) {
    return(invisible())
  }
  reprex_success(header, .envir = .envir)
  cli::cli_ul()
  cli::cli_li()
  cli::cli_text("{.file {path}}")
}

message <- function(...) {
  abort("Internal error: use reprex's UI functions, not `message()`")
}