Add multiple colors to your console output
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
R
man
tests
.Rbuildignore
.gitattributes
.gitignore
.travis.yml
DESCRIPTION
LICENSE
LICENSE.md
NAMESPACE
NEWS.md
README.Rmd
README.md
appveyor.yml
codecov.yml
cran-comments.md
multicolor.Rproj

README.md

Travis build status AppVeyor build status Coverage status CRAN_Status_Badge rstudio mirror downloads

multicolor 🎨 egret

Apply multiple colors to your messages, warnings, and errors. Built on the crayon package. Pairs nicely with cowsay. The blog post has a bit of backstory and walkthrough of how it works.

whale

Installation

Stable, from CRAN:

install.packages("multicolor")

or the development version from GitHub:

devtools::install_github("aedobbyn/multicolor")

Note: colors cannot be applied in the R GUI and certain other environments. RStudio or any terminal should work fine1.

Usage

library(multicolor)

Supply a character vector of colors to colors. This defaults to "rainbow", i.e., c("red", "orange", "yellow", "green", "blue", "purple").

multi_color("Hello world")

hello_world

If you want to unveil your creation slowly (as @cortinah’s whale gif above), use crawl. You can vary the speed with the pause argument.

chicken

Options

The text supplied will be divided into even(ish) chunks of those colors when recycle_chars is false. If it’s true, each color will apply to a single character, and the colors vector will be recycled over the lenght of the input string supplied.

Any character vector of R colors or hex values are fair game.

hello_world

The default direction is vertical, but horizontal is also an option by setting the direction param to “horizontal”.

The default type messages the result. If you want the bare string back with color encodings attached, use type = "string", which looks like:

multi_color("Why are avocado pits so big?",
  type = "string")

avocado_q

So you can ask important questions ☝️and answer them colorfully 👇:

multi_color("The wild avocado grows in subtropical jungles, so the new sprout has to get several feet tall before it can share sunlight (to make food) with its neighbors. Until it grows out of their shadows, it relies on nutrients in the seed, so it'd better be big.",
            sample(colors(), 
                   sample(10, 1)))

avocado_a

ASCII art with cowsay

All cowsay animals are exported in multicolor::things, but to get the animals to speak, you need cowsay.

library(cowsay)

say(what = "holygrail", 
    by = "yoda",
    what_color = "olivedrab",
    by_color = colors()[which(grepl("green", colors()))])

yoda

Error in style:

my_error <- multi_color("An unknown error has occurred.", 
                        type = "string")

stop(my_error)

error

And with character:

my_msg <- 
  say(what = "Error: something went horribly wrong",
    by = "rms",
    what_color = viridisLite::magma(5)[3],
    by_color = viridisLite::magma(10),
    type = "string")

e <- simpleError(my_msg)
tryCatch(log("foo"), error = function(e) message(my_msg))

rms

Or just send messages to your users that they’ll want to read.

this_variable <- "foo"
this_option <- "bar"

say(what = 
      glue::glue("Aha, I see you set {this_variable} to {this_option}. Excellent choice."),
    by = "owl",
    what_color = c("seagreen3", "turquoise3", "seagreen3"),
    by_color = c("turquoise3", "seagreen3", "turquoise3"))

foo_to_bar

That’s it! PRs & bug reports v welcome. 🎨


1 If coloring isn’t possible, you’ll get a warning on load and every time multi_color evaluates. The type argument will auto-set to "string". To check how these environments are determined, see multicolor:::use_color. If using multicolor in another package, you might consider inserting a replacement for the case when this evaluates to FALSE.