Skip to content

Commit

Permalink
#64 - implement NO_COLOR (#65)
Browse files Browse the repository at this point in the history
* implement NO_COLOR

* Update roxygen comment

* roxygenise

* test and NEWS

* Unset crayon option in NO_COLOR test
  • Loading branch information
nfultz authored and gaborcsardi committed Feb 8, 2018
1 parent b5221ab commit 7008001
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions R/has_color.r
Expand Up @@ -9,6 +9,8 @@
#' with [options()], then `TRUE` is returned. If it is
#' set to something else than `TRUE` (typically `FALSE`),
#' then `FALSE` is returned.
#' \item Otherwise, if the `NO_COLOR` environment variable is
#' set, `FALSE` is returned.
#' \item Otherwise, if the standard output is not a terminal, then
#' `FALSE` is returned.
#' \item Otherwise, if the platform is Windows, `TRUE` is returned
Expand Down Expand Up @@ -49,6 +51,10 @@ has_color <- function() {
enabled <- getOption("crayon.enabled")
if (!is.null(enabled)) { return(isTRUE(enabled)) }

if (!is.na(Sys.getenv("NO_COLOR", NA))) {
return(FALSE)
}

## RStudio with (potential) ANSI support?
if (rstudio_with_ansi_support() && sink.number() == 0) {
return(TRUE)
Expand Down
3 changes: 3 additions & 0 deletions inst/NEWS.md
@@ -1,3 +1,6 @@
# Next

* `NO_COLOR` enviroment variable disables color (#64)

# 1.3.4

Expand Down
2 changes: 2 additions & 0 deletions man/has_color.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-has-color.r
Expand Up @@ -89,3 +89,13 @@ test_that("tput errors are handled gracefully", {
mockery::stub(get_terminal_colors, "system", function(...) 16)
expect_equal(get_terminal_colors(), 16)
})

test_that("has_color, NO_COLOR=1", {
withr::with_options(
list(crayon.enabled = NULL),
withr::with_envvar(
c(NO_COLOR="1"),
expect_false(has_color())
)
)
})

0 comments on commit 7008001

Please sign in to comment.