/
print.R
146 lines (114 loc) · 3.16 KB
/
print.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' Print goodpractice results
#'
#' @param x Object of class `goodPractice`, as returned by [gp()].
#' @param positions_limit How many positions to print at most.
#' @param ... Unused, for compatibility with [base::print()] generic method.
#'
#' @importFrom rstudioapi hasFun
#' @importFrom praise praise
#' @importFrom clisymbols symbol
#' @importFrom crayon red bold
#'
#' @export
print.goodPractice <- function(x, positions_limit = 5, ...) {
failure <- FALSE
for (check in names(x$checks)) {
if (! check_passed(x$checks[[check]], na_as_passed = TRUE)) {
if (!failure) {
failure <- TRUE
gp_header(x)
}
gp_advice(x, check, positions_limit)
}
}
if (failure) {
gp_footer(x)
if (getOption("goodpractice.rstudio_source_markers", TRUE) &&
hasFun("sourceMarkers")) {
rstudio_source_markers(x)
}
} else {
cat(
"\n", sep = "",
bold(red(clisymbols::symbol$heart)),
praise(paste0(
" ${Exclamation}! ${Adjective} package! ",
"Keep up the ${adjective} work!"
)),
"\n"
)
}
invisible(x)
}
#' @importFrom clisymbols symbol
make_line <- function(x) {
paste(rep(clisymbols::symbol$line, x), collapse = "")
}
lines <- vapply(1:100, FUN.VALUE = "", make_line)
header_line <- function(left = "", right = "",
width = getOption("width")) {
ncl <- nchar(left)
ncr <- nchar(right)
if (ncl) left <- paste0(" ", left, " ")
if (ncr) right <- paste0(" ", right, " ")
ndashes <- width - ((ncl > 0) * 2 + (ncr > 0) * 2 + ncl + ncr)
if (ndashes < 4) {
right <- substr(right, 1, ncr - (4 - ndashes))
ncr <- nchar(right)
}
dashes <- if (ndashes <= length(lines)) {
lines[ndashes]
} else {
make_line(ndashes)
}
res <- paste0(
substr(dashes, 1, 2),
left,
substr(dashes, 3, ndashes - 4),
right,
substr(dashes, ndashes - 3, ndashes)
)[1]
substring(res, 1, width)
}
gp_header <- function(x) {
h <- header_line(left = paste("GP", x$package))
cat(crayon::yellow(h), "\n\n", sep = "")
cat(crayon::bold("It is good practice to"), "\n\n", sep = "")
}
gp_footer <- function(x) {
f <- header_line()
cat(crayon::yellow(f), "\n")
}
#' @importFrom clisymbols symbol
gp_advice <- function(state, fail, limit) {
MYCHECKS <- prepare_checks(CHECKS, state$extra_checks)
chk <- MYCHECKS[[fail]]
res <- state$checks[[fail]]
str <- if (is.function(chk$gp)) chk$gp(state) else chk$gp
str <- gsub("\n\\s*", " ", str)
str <- paste(
strwrap(
paste0(crayon::red(clisymbols::symbol$cross), " ", str),
indent = 2,
exdent = 4
),
collapse = "\n"
)
cat(str)
if ("positions" %in% names(res)) gp_positions(res[["positions"]], limit)
cat("\n")
}
gp_positions <- function(pos, limit) {
num <- length(pos)
if (length(pos) > limit) pos <- pos[1:limit]
cat("\n\n")
lapply(pos, function(x) {
cat(sep = "", " ", crayon::blue(x$filename), ":",
crayon::blue(as.character(x$line_number)), ":",
crayon::blue(as.character(x$column_number)), "\n")
})
if (num > limit) {
and <- paste0(" ... and ", num - limit, " more lines\n")
cat(crayon::blue(and))
}
}