ropensci / unconf18

http://unconf18.ropensci.org/
44 stars 4 forks source link

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

Open czeildi opened 6 years ago

czeildi commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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 https://github.com/rstudio/rstudio/issues/2733, https://github.com/rstudio/rstudio/issues/2574, so at least there this behavior will likely change in the future.

lcolladotor commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@jimhester : I took some more time to read the issues you linked. Good to see Rstudio already thinking about this :) Also found this: https://github.com/r-lib/cli/issues/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.