r-lib / cli
1
#' `cat()` helpers
2
#'
3
#' These helpers provide useful wrappers around [cat()]: most importantly
4
#' they all set `sep = ""`, and `cat_line()` automatically adds a newline.
5
#'
6
#' @export
7
#' @param ... For `cat_line()` and `cat_bullet()`, paste'd together with
8
#'   `collapse = "\n"`. For `cat_rule()` and `cat_boxx()` passed on to
9
#'   [rule()] and [boxx()] respectively.
10
#' @param bullet Name of bullet character. Indexes into [symbol]
11
#' @param col,background_col,bullet_col Colours for text, background, and
12
#'   bullets respectively.
13
#' @param x An object to print.
14
#' @param file Output destination. Defaults to standard output.
15
#' @examples
16
#' cat_line("This is ", "a ", "line of text.", col = "red")
17
#' cat_bullet(letters[1:5])
18
#' cat_bullet(letters[1:5], bullet = "tick", bullet_col = "green")
19
#' cat_rule()
20
cat_line <- function(..., col = NULL, background_col = NULL, file = stdout()) {
21 1
  out <- paste0(..., collapse = "\n")
22 1
  out <- apply_style(out, col)
23 1
  out <- apply_style(out, background_col, bg = TRUE)
24

25 1
  cat(out, "\n", sep = "", file = file, append = TRUE)
26
}
27

28
#' @export
29
#' @rdname cat_line
30
cat_bullet <- function(..., col = NULL, background_col = NULL,
31
                       bullet = "bullet", bullet_col = NULL,
32
                       file = stdout()) {
33 1
  out <- apply_style(paste0(...), col)
34 1
  bullet <- apply_style(symbol[[bullet]], bullet_col)
35

36 1
  cat_line(paste(bullet, out), background_col = background_col, file = file)
37
}
38

39
#' @export
40
#' @rdname cat_line
41
cat_boxx <- function(..., file = stdout()) {
42 1
  cat_line(boxx(...), file = file)
43
}
44

45
#' @export
46
#' @rdname cat_line
47
cat_rule <- function(..., file = stdout()) {
48 1
  cat_line(rule(...), file = file)
49
}
50

51
#' @export
52
#' @rdname cat_line
53
cat_print <- function(x, file = "") {
54 1
  if (!identical(file, "")) {
55 1
    sink(file)
56 1
    on.exit(sink(NULL))
57
  }
58

59 1
  print(x)
60
}

Read our documentation on viewing source code .

Loading