/
lolcat.R
72 lines (66 loc) · 1.78 KB
/
lolcat.R
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
61
62
63
64
65
66
67
68
69
70
71
72
##' Replacement for cat, message, and print.
##'
##' @title Colourful printing
##'
##' @param ... Arguments as for [cat], including
##' `sep`, etc, arguments passed to [message] or extra arguments
##' to [print]
##'
##' @param file Used to check that we don't put silly output into
##' actual files. Base [cat] will be used when `file` is
##' given
##'
##' @param fill Passed to [cat]
##'
##' @param lol Optional [rainbowrite::lol] engine to use. If omitted
##' we use the default one registered when the package is loaded
##'
##' @export
##' @examples
##' for (i in 1:20) {
##' lolcat("hello world\n")
##' }
lolcat <- function(..., file = "", fill = FALSE, lol = NULL) {
if (file != "") {
cat(..., file = file)
} else {
msg <- utils::capture.output(cat(..., fill = fill))
reset <- fill || isTRUE(grepl("\n", switch(...length(), ...)))
cat(render(msg, lol, reset))
}
}
##' @export
##'
##' @rdname lolcat
##'
##' @param domain used by translations (not yet supported)
##'
##' @param appendLF logical: should messages given as a character
##' string have a newline appended? (see [message])
lolmessage <- function(..., domain = NULL, appendLF = TRUE, # nolint
lol = NULL) {
msg <- .makeMessage(..., domain = domain, appendLF = appendLF)
message(render(msg, lol, appendLF), appendLF = FALSE)
}
##' @export
##'
##' @rdname lolcat
##' @param x Object to print
lolprint <- function(x, ..., lol = NULL) {
msg <- utils::capture.output(print(x, ...))
cat(render(msg, lol, TRUE))
}
render <- function(x, lol, reset) {
if (is.null(lol)) {
lol <- pkg$default
if (is.null(lol)) {
lol <- default_reset()
}
}
text <- lol$render(x, reset)
if (reset) {
paste0(text, "\n", collapse = "")
} else {
paste0(text, collapse = "\n")
}
}