Debian Codegraph @

R/utils-formatting.R

raw
1 #' Pad Character Vectors
2 #'
3 #' @param x a `character` vector
4 #' @param width `integer` scalar. target string width
5 #' @param pad `character` scalar. the symbol to pad with
6 #'
7 #' @export pad_right
8 #' @name pad_right
9 #'
10 #' @examples
11 #' pad_left("foo", 5)
12 #' pad_right("foo", 5, ".")
13 #' pad_left(c("foo", "foooooo"), pad = ".")
14 NULL
15
16
17
18
19 #' @export pad_left
20 #' @rdname pad_right
21 #' @name pad_left
22 NULL
23
24
25
26
27 # internal ----------------------------------------------------------------
28
29 fmt_function_signature <- function(x){
30 paste0("function(", paste(names(formals(x)), collapse = ", "), ")")
31 }
32
33
34
35
36 fmt_threshold <- function(
37 x,
38 type = "both",
39 log_levels = getOption("lgr.log_levels")
40 ){
41 assert(all(is.na(x)) || is_integerish(x[!is.na(x)]) || is.character(x))
42
43 log_levels = c("off" = 0L, log_levels)
44 if (is.character(x)){
45 assert(all(x %in% names(log_levels)))
46 x <- unlabel_levels(x, log_levels = log_levels)
47 }
48
49 impl <- function(.x){
50 assert((length(.x) == 1L) && (is.na(.x)) || is_integerish(.x))
51
52 if (.x %in% log_levels){
53 .r <- log_levels[which(log_levels == .x)]
54
55 } else if (is.na(.x)){
56 .r <- c("all" = NA)
57 } else {
58 return(format(.x))
59 }
60
61 if (identical(type, "character")){
62 return(names(.r))
63 }
64
65 paste0(names(.r), " (", .r, ")")
66 }
67
68 vapply(x, impl, character(1))
69 }