r-lib / crayon

🖍️ R package for colored terminal output — now superseded by cli
http://r-lib.github.io/crayon/
Other
324 stars 38 forks source link

Shouldn't crayon be disabled in non-interactive mode? #86

Closed mattfidler closed 5 years ago

mattfidler commented 5 years ago

I think crayon should be disabled in non-interactive mode. What do you think? Its an easy enough check to add:

interactive()

If you think so, I could add a pull request.

gaborcsardi commented 5 years ago

What is the problem that you are trying to solve?

mattfidler commented 5 years ago

In running nlmixr from a command line in windows (as discussed in this issue)

https://github.com/nlmixrdevelopment/nlmixr/issues/148

Escape sequences appear in the output.

While I could set the correct option to suppress the output, I thought it really doesn't make sense to output escape sequences when running non-interactively.

mattfidler commented 5 years ago

Since many people in the pharmaceutical world are used to NONMEM where it produces output, I don't think Nick is alone in wanting to have some sort of non-interactive way to run nlmixr and parse the output.

gaborcsardi commented 5 years ago

I don't see any escape sequences there. Can you please create a reproducible example?

mattfidler commented 5 years ago

Yes.

message(crayon::blue("blue"))
options(crayon.enabled=FALSE)
message(crayon::blue("blue"))
zip("tmp.zip", c("tmp.R", "tmp.Rout"))

Then call R -e source('tmp.R') 1> tmp.Rout 2>&1 in windows tmp.zip

mattfidler commented 5 years ago

The contents of tmp.Rout


R version 3.5.3 (2019-03-11) -- "Great Truth"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> source('tmp.R')
blue
blue
  adding: tmp.R (deflated 37%)
  adding: tmp.Rout (deflated 37%)
> 
> 
gaborcsardi commented 5 years ago

crayon is usually disabled on windows, is this some GUI or some 3rd party terminal emulator like cmder?

mattfidler commented 5 years ago

No. Command line only.

On Tue, Apr 16, 2019 at 4:11 PM Gábor Csárdi notifications@github.com wrote:

crayon is usually disabled on windows, is this some GUI or some 3rd party terminal emulator like cmder?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/r-lib/crayon/issues/86#issuecomment-483844998, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfa2npXEqe7em18i3SANW7XxLffin4tks5vhjxjgaJpZM4cqKN1 .

gaborcsardi commented 5 years ago

On windows we only have colors on in these cases: https://github.com/r-lib/crayon/blob/84be62074ad5b3ede54dd035ef4003f808f34f12/R/has_color.r#L67-L74

I.e.

  1. in ConEmu
  2. in cmder
  3. if ansicon is loaded

The line before this, at https://github.com/r-lib/crayon/blob/84be62074ad5b3ede54dd035ef4003f808f34f12/R/has_color.r#L64 also checks that stdout is a terminal. In your case the output is not a terminal, so clearly, there is a bug here. Do you have the crayon.enabled options set?

mattfidler commented 5 years ago

As in the reprex, the crayon.enabled is FALSE after the first time this is run. The color was turned off once this option was set. I must say windows R and windows in general has some odd issues.

I don't know if it matters, but I run the shell from emacs and it does not have any of the environmental variables set.

To me, a simple check to interactive() would also check to see if this is a terminal.

gaborcsardi commented 5 years ago

As in the reprex, the crayon.enabled is FALSE after the first time this is run. The color was turned off once this option was set.

I understand this, but I do not know if it was already set to TRUE in your R profile for example.

I don't know if it matters, but I run the shell from emacs and it does not have any of the environmental variables set.

It might in general, although the check for emacs is after the check for windows, at least in the GitHub version of crayon, so it should not.

To me, a simple check to interactive() would also check to see if this is a terminal.

No, that's independent of whether stdout is a terminal or not. The correct check for that is isatty(stdout()) as above.

Which version of crayon is this?

mattfidler commented 5 years ago

It could be in my .Rprofile once I get back to my windows machine I will check. I might have put it there because of windows issues. If so, sorry for the misdirection.

I will also get the version of crayon. My guess is it is the latest CRAN revision.

So, you think the behavior crayon of R CMD BATCH or R -e "source(x)" is to not show the escape sequences?

mattfidler commented 5 years ago

Indeed it was set in my .Rprofile.

On Tue, Apr 16, 2019 at 5:18 PM Gábor Csárdi notifications@github.com wrote:

As in the reprex, the crayon.enabled is FALSE after the first time this is run. The color was turned off once this option was set.

I understand this, but I do know if it was already set to TRUE in your profile for example.

I don't know if it matters, but I run the shell from emacs and it does not have any of the environmental variables set.

It might in general, although the check for emacs is after the check for windows, at least in the GitHub version of crayon, so it should not.

To me, a simple check to interactive() would also check to see if this is a terminal.

No, that's independent of whether stdout is a terminal or not. The correct check for that is isatty(stdout()) as above.

Which version of crayon is this?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/r-lib/crayon/issues/86#issuecomment-483865117, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfa2sJNRDNKluWMizsSRtWm1ZX-QuzBks5vhkwwgaJpZM4cqKN1 .

gaborcsardi commented 5 years ago

Well, then if you want to turn it off in non-interactive sessions, you can do that in your .Rprofile.

Ideally, you would not force colors in your .Rprofile, but let crayon auto-detect the environment. If it does not detect it properly, then please file an issue.

mattfidler commented 5 years ago

It wasn't detecting correctly for ESS + windows, if I remember correctly

On Wed, Apr 17, 2019 at 1:13 PM Gábor Csárdi notifications@github.com wrote:

Well, then if you want to turn it off in non-interactive sessions, you can do that in your .Rprofile.

Ideally, you would not force colors in your .Rprofile, but let crayon auto-detect the environment. If it does not detect it properly, then please file an issue.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/r-lib/crayon/issues/86#issuecomment-484202826, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfa2mFZCCExAh_OeW7B7BVstsrZWAAWks5vh2RNgaJpZM4cqKN1 .

gaborcsardi commented 5 years ago

OK, see https://github.com/r-lib/crayon/issues/87

This might be tricky to solve, if you start Rscript from emacs, but I'll take a look.