Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.

Color coded errors/warnings/messages/printed text #61

Open
czeildi opened this issue May 15, 2018 · 9 comments
Open

Color coded errors/warnings/messages/printed text #61

czeildi opened this issue May 15, 2018 · 9 comments

Comments

@czeildi
Copy link

czeildi commented May 15, 2018

By default errors, warnings and messages are all printed red in R which can be confusing as red usually means something wrong but a message can be just informative. I think it would be nice to provide an easy way to create colored messages, like by default red for error, yellow for warning, blue for friendly information etc. With the crayon package this should not be too difficult.

I am thinking about a way that maybe the end-user should be able to control this, or the creator of the package. We could distinguish style further based on the class of the condition.

@boshek
Copy link

boshek commented May 15, 2018

Fun idea.

Packages like usethis have implemented this for many of their printed outputs:

https://github.com/r-lib/usethis/blob/bb191e95ef1b0e3aa304e7267d1d968975414450/R/style.R#L11-L13

Off the top of my head I can't quite think of a way that you could modify errors, warnings and message other than tryCatch which seems a little awkward. Probably a better way though.

I do really love coloured output though and I do think it is worth the effort.

@mpadge
Copy link
Member

mpadge commented May 15, 2018

It's all just standard ANSI colour codes. Even crayon is nothing other than simple pre- and post-pending the appropriate codes:

> col_green <- "\033[32m\033[47m" # 32m = yellow; 47m = white background
> col_blue <- "\033[34m\033[1m\033[43m" # 34m = blue; 1m = bold; 43m = Yellow BG
> col0 <- "\033[22m\033[39m\033[49m" # 22m = normal weight; 49m = normal BG
> message (col_green, "blah", col0)
> warning (col_blue, "blah", col0)

Probably best not to mess with message and warning, but col_message("my message", "blue") would do it.

@czeildi
Copy link
Author

czeildi commented May 16, 2018

@boshek thanks for the usethis reference. I can imagine a bright future for a package focusing solely on formatting messages and maybe use that in usethis as well. @jennybc what do you think? Especially since the style guide for error messages in the [tidyverse style guide].(http://style.tidyverse.org/error-messages.html)

@mpadge yes, maybe this would be a tiny package but I see value in tiny single purpose packages. And I can imagine that even if you have one core function like col_message we could create shortcuts for errors, warnings etc to encourage a unified formatting

From a development perspective I can see a way to do the formatting when creating the error/warning etc. But if possible I would welcome an option for maybe globally setting style on the user side like option(warning_color = "yellow") that could effect even errors thrown by base R code. Again, I do not know whether this is technically possible.

@jennybc
Copy link
Member

jennybc commented May 16, 2018

@jennybc what do you think? Especially since the style guide for error messages in the tidyverse style guide

Yes we do have a "todo" to extract the whole style.R file from usethis and, probably, put it in a package to facilitate making consistent user interfaces that meet the style guide. There have been multiple requests to export those functions from usethis, but we've declined, because we don't want packages to depend on usethis for that functionality.

Something I've been meaning to write down but have not is a map between, say, a function in style.R and identifiable concepts. As in, "always apply this style to a path and that style when referring to an argument name and this other style for a string ...". I think, right now, you have to use common sense or look around at usage elsewhere in the package, which is slow and inconsistent.

@jimhester
Copy link

jimhester commented May 17, 2018

The long term plans for color styling in the tidyverse is to use cli to style the outputs, which would allow us to define a default style, but would also allow users to define a custom style that would automatically be used throughout our packages.

Also there are a few RStudio IDE issues related to automatically coloring messages / warnings / errors in red rstudio/rstudio#2733, rstudio/rstudio#2574, so at least there this behavior will likely change in the future.

@lcolladotor
Copy link

Hi. I'm just curious if you've checked https://github.com/jalvesaq/colorout and if whether this solves part of the problem you describe.

@czeildi
Copy link
Author

czeildi commented May 19, 2018

Summary: package for styling messages (color errors/warnings differently + style in usethis)

(( @jimhester thanks, sounds great. Though not sure whether this means we should not bother with this at the unconf or it means that there's a clear way and we could. Maybe discuss in person? ))

@maurolepore
Copy link
Member

Something I've been meaning to write down ...

This would be great! So far I see that the tidyverse style guide includes advice about errors messages but omits colour (http://style.tidyverse.org/error-messages.html). This would be great guidance and most useful before too much of the code includes coloured output.

@czeildi
Copy link
Author

czeildi commented May 20, 2018

@jimhester : I took some more time to read the issues you linked. Good to see Rstudio already thinking
about this :) Also found this: r-lib/cli#38 Probably I should have googled myself before posting here... I am still unsure about boundaries, i.e. style.R extracted from usethis would move to cli or be its own package?

@lcolladotor thanks, I haven't used colorout before but indeed seems like a possible workaround.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants