From 8ed56a5b6e43536bd50adf167e1b549fb8b4fd9e Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Fri, 31 Jul 2020 11:20:40 -0400 Subject: [PATCH 1/8] Control color in rcmdcheck via an environment variable --- NAMESPACE | 2 -- R/comparison-summary.R | 1 - R/comparison.R | 1 - R/error.R | 2 -- R/print.R | 8 +------- R/styles.R | 20 ++++++++++++++++++-- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index b7a8d9c..f780730 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,8 +24,6 @@ importFrom(cli,cli_progress_bar) importFrom(cli,cli_progress_update) importFrom(cli,is_dynamic_tty) importFrom(cli,symbol) -importFrom(crayon,bgRed) -importFrom(crayon,bold) importFrom(crayon,col_nchar) importFrom(crayon,cyan) importFrom(crayon,green) diff --git a/R/comparison-summary.R b/R/comparison-summary.R index c45fac1..01661c7 100644 --- a/R/comparison-summary.R +++ b/R/comparison-summary.R @@ -6,7 +6,6 @@ summary.rcmdcheck_comparison <- function(object, ...) { } #' @export -#' @importFrom crayon bgRed white green print.rcmdcheck_comparison_summary <- function(x, ...) { object <- x[[1]] diff --git a/R/comparison.R b/R/comparison.R index c8604e4..7afe034 100644 --- a/R/comparison.R +++ b/R/comparison.R @@ -65,7 +65,6 @@ install_failed <- function(stdout) { #' printout. #' @param ... Additional arguments, currently ignored. #' @export -#' @importFrom crayon red green bold print.rcmdcheck_comparison <- function(x, header = TRUE, ...) { if (header) { diff --git a/R/error.R b/R/error.R index 31aa461..0732322 100644 --- a/R/error.R +++ b/R/error.R @@ -1,6 +1,4 @@ -#' @importFrom crayon yellow red underline - report_system_error <- function(msg, status) { if (status$status == 0) return() diff --git a/R/print.R b/R/print.R index 4ddec92..6f03ece 100644 --- a/R/print.R +++ b/R/print.R @@ -77,15 +77,11 @@ header_line <- function(left = "", right = "", substring(res, 1, width) } -#' @importFrom crayon cyan - -cat_head <- function(left, right = "", style = cyan) { +cat_head <- function(left, right = "", style = rcmdcheck_color(crayon::cyan)) { str <- header_line(left, right) cat_line(str, style = style) } -#' @importFrom crayon red make_style - print_entry <- function(entry, entry_style) { lines <- strsplit(entry, "\n", fixed = TRUE)[[1]] @@ -124,8 +120,6 @@ print.rcmdcheck_summary <- function(x, ..., line = TRUE) { cat("\n") } -#' @importFrom crayon green red - summary_entry <- function(x, name) { len <- length(x[[name]]) diff --git a/R/styles.R b/R/styles.R index 810de80..799f50f 100644 --- a/R/styles.R +++ b/R/styles.R @@ -1,5 +1,11 @@ - -#' @importFrom crayon red green make_style bold +rcmdcheck_color <- function(f) { + function(...) { + if (identical(Sys.getenv("RCMDCHECK_COLOR", "false"), "true")) { + withr::local_options(c(crayon.enabled = TRUE)) + } + f(...) + } +} style <- function(..., sep = "") { @@ -22,3 +28,13 @@ style <- function(..., sep = "") { paste(unlist(x), collapse = sep) } + +red <- rcmdcheck_color(crayon::red) +green <- rcmdcheck_color(crayon::green) +yellow <- rcmdcheck_color(crayon::yellow) +bold <- rcmdcheck_color(crayon::bold) +underline <- rcmdcheck_color(crayon::underline) +bgRed <- rcmdcheck_color(crayon::bgRed) +white <- rcmdcheck_color(crayon::white) +cyan <- rcmdcheck_color(crayon::cyan) +make_style <- rcmdcheck_color(crayon::make_style) From 867494c286dbd46d8837cbe6d290194d110dbef8 Mon Sep 17 00:00:00 2001 From: Jim Hester Date: Fri, 31 Jul 2020 11:21:43 -0400 Subject: [PATCH 2/8] Use cyan directly --- R/print.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/print.R b/R/print.R index 6f03ece..b1df189 100644 --- a/R/print.R +++ b/R/print.R @@ -77,7 +77,7 @@ header_line <- function(left = "", right = "", substring(res, 1, width) } -cat_head <- function(left, right = "", style = rcmdcheck_color(crayon::cyan)) { +cat_head <- function(left, right = "", style = cyan) { str <- header_line(left, right) cat_line(str, style = style) } From 8186ac48bcdda0bef69b3a45452744ba7b3a4218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 8 Sep 2021 10:57:06 +0200 Subject: [PATCH 3/8] Adjust env var name --- R/callback.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/callback.R b/R/callback.R index ef09bce..4e220bd 100644 --- a/R/callback.R +++ b/R/callback.R @@ -26,7 +26,7 @@ block_callback <- function(top_line = TRUE) { time_if_long <- function() { limit <- as.numeric(getOption( "rcmdcheck.timestamp_limit", - Sys.getenv("R_RCMDCHECK_TIMESTAMP_LIMIT", "0.33333") + Sys.getenv("RCMDCHECK_TIMESTAMP_LIMIT", "0.33333") )) elapsed <- now - line_started if (elapsed> as.difftime(limit, units = "secs")) { From d79fb93670dfe384d1d620561bb81c479dffe469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 8 Sep 2021 12:13:30 +0200 Subject: [PATCH 4/8] Fix DESCRIPTION Authors@R --- DESCRIPTION | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 940042d..f001e13 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -4,7 +4,7 @@ Version: 1.3.3.9000 Authors@R: person(given = "Gábor", family = "Csárdi", - role = "cre", + role = c("cre", "aut"), email = "csardi.gabor@gmail.com") Description: Run 'R CMD check' from 'R' and capture the results of the individual checks. Supports running checks in the background, @@ -15,7 +15,6 @@ BugReports: https://github.com/r-Lib/rcmdcheck/issues Imports: callr (>= 3.1.1.9000), cli (>= 3.0.0), - crayon, curl, desc (>= 1.2.0), digest, From ed83156685aa79e18a0ff5b6acc9d3ea503f626e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 8 Sep 2021 12:14:07 +0200 Subject: [PATCH 5/8] crayon -> cli * create style functions at load time --- NAMESPACE | 8 ------ R/comparison-summary.R | 2 +- R/styles.R | 57 ++++++++++++++++++++++++------------- R/utils.R | 8 ++++-- tests/testthat/test-color.R | 18 ++++++++++++ 5 files changed, 62 insertions(+), 31 deletions(-) create mode 100644 tests/testthat/test-color.R diff --git a/NAMESPACE b/NAMESPACE index f780730..39bbdce 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,14 +24,6 @@ importFrom(cli,cli_progress_bar) importFrom(cli,cli_progress_update) importFrom(cli,is_dynamic_tty) importFrom(cli,symbol) -importFrom(crayon,col_nchar) -importFrom(crayon,cyan) -importFrom(crayon,green) -importFrom(crayon,make_style) -importFrom(crayon,red) -importFrom(crayon,underline) -importFrom(crayon,white) -importFrom(crayon,yellow) importFrom(curl,handle_setopt) importFrom(curl,multi_add) importFrom(curl,multi_cancel) diff --git a/R/comparison-summary.R b/R/comparison-summary.R index 01661c7..0195a0b 100644 --- a/R/comparison-summary.R +++ b/R/comparison-summary.R @@ -26,7 +26,7 @@ print.rcmdcheck_comparison_summary <- function(x, ...) { change_summary(object$cmp, "error"), " | ", change_summary(object$cmp, "warning"), " | ", change_summary(object$cmp, "note"), - style = make_style("darkgrey") + style = darkgrey ) invisible(x) diff --git a/R/styles.R b/R/styles.R index 799f50f..5b2bac2 100644 --- a/R/styles.R +++ b/R/styles.R @@ -1,40 +1,59 @@ + rcmdcheck_color <- function(f) { function(...) { - if (identical(Sys.getenv("RCMDCHECK_COLOR", "false"), "true")) { - withr::local_options(c(crayon.enabled = TRUE)) + num_cols <- as_integer(getOption( + "rcmdcheck.num_colors", + Sys.getenv("RCMDCHECK_NUM_COLORS", "NA") + )) + if (! is.na(num_cols)) { + withr::local_options(c(cli.num_colors = num_cols)) } f(...) } } +the <- new.env(parent = emptyenv()) + style <- function(..., sep = "") { args <- list(...) st <- names(args) - styles <- list( - "ok" = green, - "note" = make_style("blue"), - "warn" = make_style("magenta"), - "err" = red, - "pale" = make_style("darkgrey"), - "timing" = make_style("cyan") + the$styles <- the$styles %||% list( + "ok" = rcmdcheck_color(cli::col_green), + "note" = rcmdcheck_color(cli::col_blue), + "warn" = rcmdcheck_color(cli::col_magenta), + "err" = rcmdcheck_color(cli::col_red), + "pale" = rcmdcheck_color(cli::col_grey), + "timing" = rcmdcheck_color(cli::col_cyan) ) nms <- names(args) x <- lapply(seq_along(args), function(i) { - if (nzchar(nms[i])) styles[[nms[i]]](args[[i]]) else args[[i]] + if (nzchar(nms[i])) the$styles[[nms[i]]](args[[i]]) else args[[i]] }) paste(unlist(x), collapse = sep) } -red <- rcmdcheck_color(crayon::red) -green <- rcmdcheck_color(crayon::green) -yellow <- rcmdcheck_color(crayon::yellow) -bold <- rcmdcheck_color(crayon::bold) -underline <- rcmdcheck_color(crayon::underline) -bgRed <- rcmdcheck_color(crayon::bgRed) -white <- rcmdcheck_color(crayon::white) -cyan <- rcmdcheck_color(crayon::cyan) -make_style <- rcmdcheck_color(crayon::make_style) +red <- NULL +green <- NULL +yellow <- NULL +bold <- NULL +underline <- NULL +bgRed <- NULL +white <- NULL +cyan <- NULL +darkgrey <- NULL + +.onLoad <- function(libname, pkgname) { + red <<- rcmdcheck_color(cli::col_red) + green <<- rcmdcheck_color(cli::col_green) + yellow <<- rcmdcheck_color(cli::col_yellow) + bold <<- rcmdcheck_color(cli::style_bold) + underline <<- rcmdcheck_color(cli::style_underline) + bgRed <<- rcmdcheck_color(cli::bg_red) + white <<- rcmdcheck_color(cli::col_white) + cyan <<- rcmdcheck_color(cli::col_cyan) + darkgrey <<- rcmdcheck_color(cli::col_grey) +} diff --git a/R/utils.R b/R/utils.R index 1bc04ff..788da90 100644 --- a/R/utils.R +++ b/R/utils.R @@ -107,13 +107,11 @@ get_install_out <- function(path) { } } -#' @importFrom crayon col_nchar - col_align <- function(text, width = getOption("width"), align = c("left", "center", "right")) { align <- match.arg(align) - nc <- col_nchar(text) + nc <- cli::ansi_nchar(text, type = "width") if (width <= nc) { text @@ -160,3 +158,7 @@ cat_line <- function(..., style = NULL) { duration <- function(start) { as.double(Sys.time() - start, units = "secs") } + +as_integer <- function(x) { + suppressWarnings(as.integer(x)) +} diff --git a/tests/testthat/test-color.R b/tests/testthat/test-color.R new file mode 100644 index 0000000..37f0e58 --- /dev/null +++ b/tests/testthat/test-color.R @@ -0,0 +1,18 @@ + +test_that("colors can be turned on and off", { + # clean up state + withr::local_options(rcmdcheck.num_colors = NULL) + withr::local_envvar(RCMDCHECK_NUM_COLORS = NA_character_) + + # by default let cli decide + withr:::local_options(cli.num_colors = 100) + expect_equal(rcmdcheck_color(cli::num_ansi_colors)(), 100) + + # env var overrides + withr::local_envvar(RCMDCHECK_NUM_COLORS = 200) + expect_equal(rcmdcheck_color(cli::num_ansi_colors)(), 200) + + # option overrides that + withr::local_options(rcmdcheck.num_colors = 300) + expect_equal(rcmdcheck_color(cli::num_ansi_colors)(), 300) +}) From 4194ecea337a574dcd835a20491538d6ed620d59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Wed, 8 Sep 2021 16:20:54 +0200 Subject: [PATCH 6/8] Use svglite device in test vignette To make it work on platforms without x11/Cairo. --- DESCRIPTION | 1 + tests/testthat/bad1/DESCRIPTION | 3 ++- tests/testthat/bad1/vignettes/test.Rmd | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f001e13..176322e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -32,6 +32,7 @@ Suggests: mockery, ps, rmarkdown, + svglite, testthat, webfakes Encoding: UTF-8 diff --git a/tests/testthat/bad1/DESCRIPTION b/tests/testthat/bad1/DESCRIPTION index 0b7326e..255a7c7 100644 --- a/tests/testthat/bad1/DESCRIPTION +++ b/tests/testthat/bad1/DESCRIPTION @@ -9,7 +9,8 @@ License: Public domain Suggests: testthat, knitr, - rmarkdown + rmarkdown, + svglite RoxygenNote: 6.1.0 Repository: CRAN Encoding: UTF-8 diff --git a/tests/testthat/bad1/vignettes/test.Rmd b/tests/testthat/bad1/vignettes/test.Rmd index 582b3e0..ac75d18 100644 --- a/tests/testthat/bad1/vignettes/test.Rmd +++ b/tests/testthat/bad1/vignettes/test.Rmd @@ -14,7 +14,7 @@ doloribus occaecati error. Alias rerum voluptas illo esse et rerum. Officiis aut et eligendi quia quo. Culpa inventore dicta soluta debitis ex. -```{r} +```{r dev = "svglite"} outfile <- Sys.getenv("RCMDBUILD_OUTPUT", "") if (nzchar(outfile)) saveRDS(.libPaths(), outfile) ``` From 21d1f6ba709cc5c21bd9b5d58aa230f2aeaf1a19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Thu, 9 Sep 2021 10:58:40 +0200 Subject: [PATCH 7/8] Use cli::console_width() instead of the `width` option Works better with cli. --- R/utils.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils.R b/R/utils.R index 788da90..e13df8c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -107,7 +107,7 @@ get_install_out <- function(path) { } } -col_align <- function(text, width = getOption("width"), +col_align <- function(text, width = cli::console_width(), align = c("left", "center", "right")) { align <- match.arg(align) From a068355c88bfd4252d5336fba76ede12778124e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Thu, 9 Sep 2021 11:01:13 +0200 Subject: [PATCH 8/8] Create man page for env vars and options --- R/options.R | 56 +++++++++++++++++++++++++++++++++++++++++ _pkgdown.yml | 4 +++ man/rcmdcheck-config.Rd | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 R/options.R create mode 100644 man/rcmdcheck-config.Rd diff --git a/R/options.R b/R/options.R new file mode 100644 index 0000000..69ac2a3 --- /dev/null +++ b/R/options.R @@ -0,0 +1,56 @@ + +#' rcmdcheck configuration +#' +#' Options take precedence over environment variables. E.g. if both +#' the `RCMDCHECK_NUM_COLORS` environment variables and the +#' `rcmdcheck.num_colors` option are set, then the latter is used. +#' +#' rcmdcheck uses the cli package for much of its output, so you can +#' configure the output via cli, see [cli::cli-config]. +#' +#' # Environment variables +#' +#' * `R_PROFILE_USER`: standard R environment variable to configure the +#' path to the user level R profile. See [base::R_PROFILE_USER]. +#' +#' * `RCMDCHECK_BASE_URL`: URL to the root of the CRAN check web page. +#' You can use this to select an alternative CRAN mirror. Defaults to +#' `https://cran.r-project.org/web/checks/`. +#' +#' * `RCMDCHECK_DETAILS_URL`: URL to the root of the CRAN check output +#' page. Defaults to `https://www.r-project.org/nosvn/R.check/`. +#' +#' * `RCMDCHECK_FLAVOURS_URL` URL to the CRAN check flavours page. +#' You can use this to select an alternative CRAN mirror. Defaults to +#' `https://cran.r-project.org/web/checks/check_flavors.html`. +#' +#' * `RCMDCHECK_NUM_COLORS`: the number of ANSI colors to use in the output. +#' It can be used to override the number of colors detected or configured +#' by the cli package. See [cli::num_ansi_colors()]. This configuration +#' is only used for the output of rcmdcheck and it does not affect the +#' examples and test cases (and other code) of the checked package. +#' It not set, then the default of cli is uesed. The corresponding +#' option is `rcmdcheck.num_colors`. +#' +#' * `RCMDCHECK_TIMESTAMP_LIMIT`: lower limit is seconds, above which +#' rcmdcheck adds time stamps to the individual check steps. It may be +#' fractional. Defaults to 1/3 of a second. The corresponding option is +#' `rcmdcheck.timestamp_limit`. +#' +#' # Options +#' +#' * `rcmdcheck.num_colors`: the number of ANSI colors to use in the output. +#' It can be used to override the number of colors detected or configured +#' by the cli package. See [cli::num_ansi_colors()]. This configuration +#' is only used for the output of rcmdcheck and it does not affect the +#' examples and test cases (and other code) of the checked package. +#' It not set, then the default of cli is uesed. The corresponding +#' environment variable is `RCMDCHECK_NUM_COLORS`. +#' +#' * `rcmdcheck.timestamp_limit`: lower limit is seconds, above which +#' rcmdcheck adds time stamps to the individual check steps. It may be +#' fractional. Defaults to 1/3 of a second. The corresponding environment +#' variable is `RCMDCHECK_TIMESTAMP_LIMIT`. +#' +#' @name rcmdcheck-config +NULL diff --git a/_pkgdown.yml b/_pkgdown.yml index e04b928..7e07967 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -27,3 +27,7 @@ reference: - compare_checks - compare_to_cran - print.rcmdcheck_comparison + +- title: Configuration + contents: + - rcmdcheck-config diff --git a/man/rcmdcheck-config.Rd b/man/rcmdcheck-config.Rd new file mode 100644 index 0000000..491bb96 --- /dev/null +++ b/man/rcmdcheck-config.Rd @@ -0,0 +1,45 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/options.R +\name{rcmdcheck-config} +\alias{rcmdcheck-config} +\title{rcmdcheck configuration} +\description{ +Options take precedence over environment variables. E.g. if both +the \code{RCMDCHECK_NUM_COLORS} environment variables and the +\code{rcmdcheck.num_colors} option are set, then the latter is used. +} +\details{ +rcmdcheck uses the cli package for much of its output, so you can +configure the output via cli, see \link[cli:cli-config]{cli::cli-config}. +} +\section{Environment variables}{ +\itemize{ +\item \code{R_PROFILE_USER}: standard R environment variable to configure the +path to the user level R profile. See \link[base:Startup]{base::R_PROFILE_USER}. +\item \code{RCMDCHECK_BASE_URL}: URL to the root of the CRAN check web page. +You can use this to select an alternative CRAN mirror. Defaults to +\verb{https://cran.r-project.org/web/checks/}. +\item \code{RCMDCHECK_DETAILS_URL}: URL to the root of the CRAN check output +page. Defaults to \verb{https://www.r-project.org/nosvn/R.check/}. +\item \code{RCMDCHECK_FLAVOURS_URL} URL to the CRAN check flavours page. +You can use this to select an alternative CRAN mirror. Defaults to +\verb{https://cran.r-project.org/web/checks/check_flavors.html}. +\item \code{RCMDCHECK_NUM_COLORS}: the number of ANSI colors to use in the output. +It can be used to override the number of colors detected or configured +by the cli package. See \code{\link[cli:num_ansi_colors]{cli::num_ansi_colors()}}. This configuration +is only used for the output of rcmdcheck and it does not affect the +examples and test cases (and other code) of the checked package. +It not set, then the default of cli is uesed. The corresponding +option is \code{rcmdcheck.num_colors}. +\item \code{RCMDCHECK_TIMESTAMP_LIMIT}: lower limit is seconds, above which +rcmdcheck adds time stamps to the individual check steps. It may be +fractional. Defaults to 1/3 of a second. +} +} + +\section{Options}{ +\itemize{ +\item \code{rcmdcheck.num_colors} +} +} +