Skip to content

Commit 8dad103

Browse files
Merge pull request #640 from michaelquinn32/communication
- Add option for suppressing styler communication (#640).
2 parents 85a81fb + 3400e9a commit 8dad103

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
to not directly modify test files in the current directory (#548).
99
- `style_file()` and friends gain argument `dry` to control if changes should
1010
be applied to files or not (#634).
11+
- added an option for disabling all communication when using the package
12+
(`styler.quiet`) (#640).
1113

1214
## Minor chnages and fixes
1315

@@ -439,3 +441,4 @@ specify_reindention(
439441
)
440442
initialize_default_attributes(pd_flat)
441443
```
444+

R/addins.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ get_addins_style_transformer <- function() {
153153

154154
communicate_addins_style_transformers <- function() {
155155
style_name <- get_addins_style_transformer_name()
156-
cat("Using style transformers `", style_name, "`\n", sep = "")
156+
if (!getOption("styler.quiet", FALSE)) {
157+
cat("Using style transformers `", style_name, "`\n", sep = "")
158+
}
157159
}
158160

159161
#' Style a file as if it was an .R file

R/communicate.R

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
#' @inheritParams can_verify_roundtrip
88
#' @keywords internal
99
communicate_warning <- function(changed, transformers) {
10-
if (any(changed, na.rm = TRUE) && !can_verify_roundtrip(transformers)) {
10+
if (any(changed, na.rm = TRUE) &&
11+
!can_verify_roundtrip(transformers) &&
12+
!getOption("styler.quiet", FALSE)
13+
) {
1114
cat("Please review the changes carefully!", fill = TRUE)
1215
}
1316
}
@@ -19,12 +22,14 @@ communicate_warning <- function(changed, transformers) {
1922
#' @param ruler_width Integer used to determine the width of the ruler.
2023
#' @keywords internal
2124
communicate_summary <- function(changed, ruler_width) {
22-
cli::cat_rule(width = max(40, ruler_width))
23-
cat("Status\tCount\tLegend \n")
24-
cli::cat_bullet("\t", sum(!changed, na.rm = TRUE), "\tFile unchanged.", bullet = "tick")
25-
cli::cat_bullet("\t", sum(changed, na.rm = TRUE), "\tFile changed.", bullet = "info")
26-
cli::cat_bullet(bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an error.")
27-
cli::cat_rule(width = max(40, ruler_width))
25+
if (!getOption("styler.quiet", FALSE)) {
26+
cli::cat_rule(width = max(40, ruler_width))
27+
cat("Status\tCount\tLegend \n")
28+
cli::cat_bullet("\t", sum(!changed, na.rm = TRUE), "\tFile unchanged.", bullet = "tick")
29+
cli::cat_bullet("\t", sum(changed, na.rm = TRUE), "\tFile changed.", bullet = "info")
30+
cli::cat_bullet(bullet = "cross", "\t", sum(is.na(changed)), "\tStyling threw an error.")
31+
cli::cat_rule(width = max(40, ruler_width))
32+
}
2833
}
2934

3035
#' @importFrom rlang abort

R/transform-files.R

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ transform_files <- function(files, transformers, include_roxygen_examples, dry)
1515
transformer <- make_transformer(transformers, include_roxygen_examples)
1616
max_char <- min(max(nchar(files), 0), getOption("width"))
1717
len_files <- length(files)
18-
if (len_files > 0L) {
18+
if (len_files > 0L && !getOption("styler.quiet", FALSE)) {
1919
cat("Styling ", len_files, " files:\n")
2020
}
2121

@@ -51,16 +51,20 @@ transform_file <- function(path,
5151
max_char_after_message_path <- nchar(message_before) + max_char_path + 1
5252
n_spaces_before_message_after <-
5353
max_char_after_message_path - char_after_path
54-
cat(
55-
message_before, path,
56-
rep_char(" ", max(0L, n_spaces_before_message_after)),
57-
append = FALSE
58-
)
54+
if (!getOption("styler.quiet", FALSE)) {
55+
cat(
56+
message_before, path,
57+
rep_char(" ", max(0L, n_spaces_before_message_after)),
58+
append = FALSE
59+
)
60+
}
5961
changed <- transform_code(path, fun = fun, ..., dry = dry)
6062

6163
bullet <- ifelse(is.na(changed), "warning", ifelse(changed, "info", "tick"))
6264

63-
cli::cat_bullet(bullet = bullet)
65+
if (!getOption("styler.quiet", FALSE)) {
66+
cli::cat_bullet(bullet = bullet)
67+
}
6468
invisible(changed)
6569
}
6670

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
styler.addins_style_transformer = "styler::tidyverse_style()",
88
styler.ignore_start = "# styler: off",
99
styler.ignore_stop = "# styler: on",
10+
styler.quiet = FALSE,
1011
styler.test_dir_writable = TRUE
1112
)
1213
toset <- !(names(op.styler) %in% names(op))

tests/testthat/test-public_api.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ test_that("messages (via cat()) of style_file are correct", {
197197
}
198198
})
199199

200+
test_that("Messages can be suppressed", {
201+
for (encoding in ls_testable_encodings()) {
202+
withr::with_options(
203+
list(cli.unicode = encoding == "utf8", styler.quiet = TRUE),
204+
{
205+
output <- catch_style_file_output(c(
206+
"public-api", "xyzdir-dirty", "dirty-sample-with-scope-spaces.R"
207+
), encoding = encoding)
208+
expect_equal(output, character(0))
209+
}
210+
)
211+
}
212+
})
213+
200214
context("public API - Rmd in style_dir()")
201215

202216
test_that("styler can style R and Rmd files via style_dir()", {

0 commit comments

Comments
 (0)