Debian Codegraph @

R/pretty.R

raw
1
2 renv_pretty_print_records <- function(preamble, records, postamble = NULL) {
3
4 if (empty(records))
5 return(invisible(NULL))
6
7 if (!renv_verbose())
8 return(invisible(NULL))
9
10 # NOTE: use 'sort()' rather than 'csort()' here so that
11 # printed output is sorted in the expected way in the users locale
12 # https://github.com/rstudio/renv/issues/1289
13 names(records) <- names(records) %||% map_chr(records, `[[`, "Package")
14 records <- records[sort(names(records))]
15 packages <- names(records)
16 descs <- map_chr(records, renv_record_format_short)
17 text <- sprintf("- %s [%s]", format(packages), descs)
18
19 all <- c(preamble, text, postamble, if (length(postamble)) "")
20 renv_caution_impl(all)
21
22 }
23
24 renv_pretty_print_records_pair <- function(preamble,
25 old,
26 new,
27 postamble = NULL,
28 formatter = NULL)
29 {
30 formatter <- formatter %||% renv_record_format_pair
31
32 all <- c(
33 c(preamble, ""),
34 renv_pretty_print_records_pair_impl(old, new, formatter),
35 if (length(postamble)) c(postamble, "")
36 )
37
38 renv_caution_impl(all)
39 }
40
41 renv_pretty_print_records_pair_impl <- function(old, new, formatter) {
42
43 # NOTE: use 'sort()' rather than 'csort()' here so that
44 # printed output is sorted in the expected way in the users locale
45 # https://github.com/rstudio/renv/issues/1289
46 all <- sort(union(names(old), names(new)))
47
48 # compute groups
49 groups <- map_chr(all, function(package) {
50
51 lhs <- old[[package]]; rhs <- new[[package]]
52 case(
53 is.null(lhs$Source) ~ rhs$Repository %||% rhs$Source,
54 is.null(rhs$Source) ~ lhs$Repository %||% lhs$Source,
55 !is.null(rhs$Repository) ~ rhs$Repository,
56 !is.null(rhs$Source) ~ rhs$Source
57 )
58
59 })
60
61 n <- max(nchar(all))
62
63 # iterate over each group and print
64 uapply(csort(unique(groups)), function(group) {
65
66 lhs <- renv_records_select(old, groups, group)
67 rhs <- renv_records_select(new, groups, group)
68
69 nms <- union(names(lhs), names(rhs))
70 text <- map_chr(nms, function(nm) {
71 formatter(lhs[[nm]], rhs[[nm]])
72 })
73
74 if (group == "unknown")
75 group <- "(Unknown Source)"
76
77 c(
78 header(group),
79 paste("-", format(nms, width = n), " ", text),
80 ""
81 )
82
83 })
84
85 }
86
87 # NOTE: Used by vetiver, so perhaps is part of the API.
88 # We should think of a cleaner way of exposing this.
89 # https://github.com/rstudio/renv/issues/1413
90 renv_pretty_print_impl <- renv_caution_impl
91
92 renv_pretty_bytes <- function(bytes) {
93
94 if (testing())
95 return("XX kB")
96
97 if (bytes < 1e3)
98 return(paste(round(bytes), "B"))
99
100 if (bytes < 1e6)
101 return(sprintf("%.0f kB", bytes / 1e3))
102
103 if (bytes < 1e9)
104 return(sprintf("%.1f MB", bytes / 1e6))
105
106 sprintf("%.1f GB", bytes / 1e9)
107
108 }