GithubHelp home page GithubHelp logo

Comments (8)

gaborcsardi avatar gaborcsardi commented on June 12, 2024

The idea is that you will print those strings to stdout ASAP, and if stdout does not support color, then we don't use the ANSI escape codes. So this is as designed.

Most programs work the same way, e.g. just try

ls -G
ls -G > /tmp/x

If you want to force the ANSI sequences, use the crayon.enabled. and crayon.colors options. E.g.

withr::with_options(
  c(crayon.enabled = TRUE, crayon.colors = 256),
  red("I am red")
)

from crayon.

HenrikBengtsson avatar HenrikBengtsson commented on June 12, 2024

I see; makes sense.

Then a follow up, shouldn't there be test against stderr() if you "plan" to print to stderr? I understand this is not easy to detect / support automatically; maybe an argument output = "stdout"?

FYI, I bumped into this when trying to conditionally display yellow("1 output sink") to a customized prompt function. I did find the workaround where I do if (crayon:::has_color()) options(crayon.enabled = TRUE) when I first set up the prompt during .Rprofile startup (where I can safely assume no sinks are active).

from crayon.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

maybe an argument output = "stdout"?

argument to what function?

Maybe it could be an option, e.g. crayon.output, but that will be cumbersome to change every time...

from crayon.

HenrikBengtsson avatar HenrikBengtsson commented on June 12, 2024

maybe an argument output = "stdout"?

argument to what function?

agree == "maybe" ;) In worst case, it's needed for all functions calling has_color() ... very tedious to use.

Could such an option be encapsulated in a convenient for_stderr() wrapper, e.g. for_stderr(red("foo"))? (not sure if with_stderr() is good here).

from crayon.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

for_stderr is a good idea. How about calling it color_stderr ?

We could also have with_color to shorten the with_options call above.

from crayon.

HenrikBengtsson avatar HenrikBengtsson commented on June 12, 2024

color_stderr() is better, but like with_color() even more. Not sure what the argument / option name should be, e.g. target, type (cf. sink()), ...?

For the output target, can see something like the following use cases:

  • with_color(..., target = "stdout"))
  • with_color(..., target = "stderr"))
  • with_color(..., target = c("stdout", "stderr"))) - outputting to both(?)
  • with_color(..., target = "prompt")) - neither stdout nor stderr, but prompt and readline-ish output (non-sinkable)
  • with_color(..., target = "none")) - disable coloring
  • with_color(..., target = "always")) - enable coloring regardless of support(?)

from crayon.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

Yeah, sg. like that.

from crayon.

gaborcsardi avatar gaborcsardi commented on June 12, 2024

OK, I decided not to do this for the next patch release. It is probably not commonly needed, and I am not yet sure what would be the right way to do. For now, please force color as a workaround.

from crayon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.