r-lib / cli
1

2
#' cli situation report
3
#'
4
#' Contains currenty:
5
#' * `cli_unicode_option`: whether the `cli.unicode` option is set and its
6
#'   value. See [is_utf8_output()].
7
#' * `symbol_charset`: the selected character set for [symbol], UTF-8,
8
#'   Windows, or ASCII.
9
#' * `console_utf8`: whether the console supports UTF-8. See
10
#'   [base::l10n_info()].
11
#' * `latex_active`: whether we are inside knitr, creating a LaTeX
12
#'   document.
13
#' * `num_colors`: number of ANSI colors. See [crayon::num_colors()].
14
#' * `console_with`: detected console width.
15
#'
16
#' @return Named list with entries listed above. It has a `cli_sitrep`
17
#' class, with a `print()` and `format()` method.
18
#'
19
#' @export
20
#' @examples
21
#' cli_sitrep()
22

23
cli_sitrep <- function() {
24 1
  structure(
25 1
    list(
26 1
      cli_unicode_option = getOption("cli.unicode", NULL),
27 1
      symbol_charset = get_active_symbol_set(),
28 1
      console_utf8 = l10n_info()$`UTF-8`,
29 1
      latex_active = is_latex_output(),
30 1
      num_colors = crayon::num_colors(),
31 1
      console_width = console_width()),
32 1
    class = "cli_sitrep")
33
}
34

35
#' @export
36

37
print.cli_sitrep <- function(x, ...) {
38 1
  cat(format(x, ...), sep = "\n")
39 1
  invisible(x)
40
}
41

42
get_active_symbol_set <- function() {
43 1
  if (identical(symbol, symbol_utf8)) {
44 1
    "UTF-8"
45 1
  } else if (identical(symbol, symbol_rstudio)) {
46 0
    "RStudio (UTF-8)"
47 1
  } else if (identical(symbol, symbol_win)) {
48 0
    "Windows (non UTF-8)"
49
  } else {
50 1
    "ASCII (non UTF-8)"
51
  }
52
}
53

54
#' @export
55

56
format.cli_sitrep <- function(x, ...) {
57 1
  fmt_names <- format(names(x))
58 1
  fmt_vals  <- vapply(x, format, character(1))
59 1
  paste0("- ", fmt_names, " : ", fmt_vals)
60
}
61

62
#' @export
63

64
as.character.cli_sitrep <- function(x, ...) {
65 0
  "<cli sitrep>"
66
}

Read our documentation on viewing source code .

Loading