Skip to content

Commit 7008001

Browse files
nfultzgaborcsardi
authored andcommitted
#64 - implement NO_COLOR (#65)
* implement NO_COLOR * Update roxygen comment * roxygenise * test and NEWS * Unset crayon option in NO_COLOR test
1 parent b5221ab commit 7008001

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

R/has_color.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#' with [options()], then `TRUE` is returned. If it is
1010
#' set to something else than `TRUE` (typically `FALSE`),
1111
#' then `FALSE` is returned.
12+
#' \item Otherwise, if the `NO_COLOR` environment variable is
13+
#' set, `FALSE` is returned.
1214
#' \item Otherwise, if the standard output is not a terminal, then
1315
#' `FALSE` is returned.
1416
#' \item Otherwise, if the platform is Windows, `TRUE` is returned
@@ -49,6 +51,10 @@ has_color <- function() {
4951
enabled <- getOption("crayon.enabled")
5052
if (!is.null(enabled)) { return(isTRUE(enabled)) }
5153

54+
if (!is.na(Sys.getenv("NO_COLOR", NA))) {
55+
return(FALSE)
56+
}
57+
5258
## RStudio with (potential) ANSI support?
5359
if (rstudio_with_ansi_support() && sink.number() == 0) {
5460
return(TRUE)

inst/NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Next
2+
3+
* `NO_COLOR` enviroment variable disables color (#64)
14

25
# 1.3.4
36

man/has_color.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-has-color.r

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ test_that("tput errors are handled gracefully", {
8989
mockery::stub(get_terminal_colors, "system", function(...) 16)
9090
expect_equal(get_terminal_colors(), 16)
9191
})
92+
93+
test_that("has_color, NO_COLOR=1", {
94+
withr::with_options(
95+
list(crayon.enabled = NULL),
96+
withr::with_envvar(
97+
c(NO_COLOR="1"),
98+
expect_false(has_color())
99+
)
100+
)
101+
})

0 commit comments

Comments
 (0)