r-lib/crayon

num_ansi_colors ignores stream parameter

Closed this issue · 1 comments

Hello!

num_ansi_colours() stream parameter appears to be ignored, and the number of colours reported is always for stdout (for interactive sessions at least). For example, this seems wrong:

> crayon:::num_ansi_colors(file(tempfile()))
[1] 256

If I modify num_ansi_colors to report it's environment:

moo <- edit(crayon::num_ansi_colors)
# Insert "str(as.list(environment()))", before "if (is_stderr && sink.number("message") != 2)"
# https://github.com/r-lib/crayon/blob/main/R/aab-num-ansi-colors.R#L107

And run a few example cases:

moo()
moo(stdout())
moo("message")
moo("stderr")
moo(stderr())
moo(file(tempfile()))

Besides orig_stream all display the following output, and return 256, the number of colours for my stdout:

List of 10
 $ std         : chr "stdout"
 $ is_stdout   : logi TRUE
 $ is_stderr   : logi FALSE
 $ is_std      : logi TRUE
 $ orig_stream : 'file' int 3
  ..- attr(*, "conn_id")=<externalptr>
 $ cray_opt_num: NULL
 $ cray_opt_has: NULL
 $ env         : chr ""
 $ opt         : NULL
 $ stream      : 'terminal' int 1

I'm guessing the problem is in get_real_output, which ignores the stream object it's handed, so the input parameter is never considered:

get_real_output <- function(output) {
cli_output_connection()
}

FWIW this doesn't seem to be an issue in cli, is the version here considered deprecated? Or maybe there should be some syncing of code?

> cli:::num_ansi_colors(file(tempfile()))
[1] 1

crayon in general is superseded, so yeah, please use cli instead.