posit-dev / positron

Positron, a next-generation data science IDE
https://positron.posit.co
Other
2.82k stars 90 forks source link

Detection of ANSI color support in console has chhanged? #5445

Open strengejacke opened 7 hours ago

strengejacke commented 7 hours ago

This example used to work

Following setup

Positron Version: 2024.11.0 (system setup) build 140
Code - OSS Version: 1.93.0
Commit: e0d844b031f95acbf89f234a2cce2af9b6721f6c
Date: 2024-10-31T14:14:22.699Z
Electron: 30.4.0
Chromium: 124.0.6367.243
Node.js: 20.15.1
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.22631

Reprex

insight::print_color("test", "green")

Result (as expected)

Image

New Positron version

Positron Version: 2024.12.0 (system setup) build 41
Code - OSS Version: 1.93.0
Commit: 39536c265d6828a68291984d0d359170316d055e
Date: 2024-11-18T02:47:00.983Z
Electron: 30.4.0
Chromium: 124.0.6367.243
Node.js: 20.15.1
V8: 12.4.254.20-electron.0
OS: Windows_NT x64 10.0.22631

Result (not expected)

Image

I think it's because this code no longer works properly with the latest Positron version:

insight:::.supports_color()
#> [1] FALSE

Created on 2024-11-21 with reprex v2.1.1

.rstudio_with_ansi_support <- function() {
  if (Sys.getenv("RSTUDIO", "") == "") {
    return(FALSE)
  }

  cols <- Sys.getenv("RSTUDIO_CONSOLE_COLOR", "")
  if (cols != "" && !is.na(as.numeric(cols))) {
    return(TRUE)
  }

  requireNamespace("rstudioapi", quietly = TRUE) &&
    rstudioapi::isAvailable() &&
    rstudioapi::hasFun("getConsoleHasColor")
}

.supports_color <- function() {
  enabled <- getOption("crayon.enabled")
  if (!is.null(enabled)) {
    return(isTRUE(enabled))
  }
  if (.rstudio_with_ansi_support() && sink.number() == 0) {
    return(TRUE)
  }
  if (!isatty(stdout())) {
    return(FALSE)
  }
  if (Sys.info()["sysname"] == "windows") {
    if (Sys.getenv("ConEmuANSI") == "ON") {
      return(TRUE)
    }
    if (Sys.getenv("CMDER_ROOT") != "") {
      return(TRUE)
    }
    return(FALSE)
  }
  if ("COLORTERM" %in% names(Sys.getenv())) {
    return(TRUE)
  }
  if (Sys.getenv("TERM") == "dumb") {
    return(FALSE)
  }
  grepl("^screen|^xterm|^vt100|color|ansi|cygwin|linux", Sys.getenv("TERM"),
    ignore.case = TRUE, perl = TRUE
  )
}

Could be that this is no bug, but Positron just changed its "identification"?

juliasilge commented 5 hours ago

Interesting! When I use Positron 2024.11.0 (Universal) build 140 on a Mac, I don't see the green:

Image

Do you mind reinstalling 2024.11.0 build 140 and finding out exactly how .rstudio_with_ansi_support() is returning true? We would never expect Sys.getenv("RSTUDIO") to return 1. You can use Sys.getenv("POSITRON") though!