From f9d531925986394ad231830b0da71a96cc2a36a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Cs=C3=A1rdi?= Date: Thu, 21 Apr 2022 12:46:57 +0200 Subject: [PATCH] Turning off ANSI also turns off hyperlinks Closes #447. --- NEWS.md | 4 ++++ R/ansi-hyperlink.R | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/NEWS.md b/NEWS.md index e951ffbfe..787c12cee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,10 @@ * `ansi_*()` functions support ANSI hyperlinks again (#444). +* Turning off ANSI colors via the `cli.num_colors` option or the + `R_CLI_NUM_COLORS` or the `NO_COLOR` environment variable now also turns off + ANSI hyperlinks (#447). + # cli 3.2.0 ## Breaking change diff --git a/R/ansi-hyperlink.R b/R/ansi-hyperlink.R index e313ee230..3f564c8dd 100644 --- a/R/ansi-hyperlink.R +++ b/R/ansi-hyperlink.R @@ -56,6 +56,15 @@ ansi_has_hyperlink_support <- function() { enabled <- Sys.getenv("R_CLI_HYPERLINKS", "") if (isTRUE(as.logical(enabled))){ return(TRUE) } + ## If ANSI support is off, then this is off as well + opt <- as.integer(getOption("cli.num_colors", NULL))[1] + if (!is.na(opt) && opt == 1) return(FALSE) + env <- as.integer(Sys.getenv("R_CLI_NUM_COLORS", ""))[1] + if (!is.na(env) && env == 1) return(FALSE) + cray_opt <- as.logical(getOption("crayon.enabled", NULL))[1] + if (!is.na(cray_opt) && !cray_opt) return(FALSE) + if (!is.na(Sys.getenv("NO_COLOR", NA_character_))) return(FALSE) + ## environment variable used by RStudio enabled <- Sys.getenv("RSTUDIO_CLI_HYPERLINKS", "") if (isTRUE(as.logical(enabled))){ return(TRUE) }