With crayon it is easy to add color to terminal output, create styles for notes, warnings, errors; and combine styles.
ANSI color support is automatically detected and used. Crayon was largely inspired by chalk.
Crayon defines several styles, that can be combined. Each style in the list has a corresponding function with the same name.
dim, renamed to avoid name clash)
italic(not widely supported)
strikethrough(not widely supported)
gray, renamed to avoid name clash)
The styling functions take any number of character vectors as arguments, and they concatenate and style them:
Crayon defines the
%+% string concatenation operator, to make it easy
to assemble stings with different styles.
cat("... to highlight the " %+% red("search term") %+% " in a block of text\n")
Styles can be combined using the
Styles can also be nested, and then inner style takes precedence:
cat(green('I am a green line ' %+%blue$underline$bold('with a blue substring') %+%' that becomes green again!\n'))
It is easy to define your own themes:
error <- red $ boldwarn <- magenta $ underlinenote <- cyancat(error("Error: subscript out of bounds!\n"))cat(warn("Warning: shorter argument was recycled.\n"))cat(note("Note: no such directory.\n"))
Most modern terminals support the ANSI standard for 256 colors,
and you can define new styles that make use of them. The
function defines a new style. It can handle R's built in color names
(see the output of
colors()), and also RGB specifications, via the
rgb() function. It automatically chooses the ANSI colors that
are closest to the specified R and RGB colors, and it also has
a fallback to terminals with 8 ANSI colors only.
ivory <- make_style("ivory")bgMaroon <- make_style("maroon", bg = TRUE)fancy <- combine_styles(ivory, bgMaroon)cat(fancy("This will have some fancy colors"), "\n")
MIT @ Gábor Csárdi
Style fucntions convert arguments to character now
Autodetect RStudio ANSI support
type argument, default
Removed dependency to
memoise (@brodieG, #25)
Fixed a test case that changed the
setting, potentially (@brodieG)
crayon.colors option, to specify the number of
tput colors=8 will use 256 colors,
as 256 colors are usually supported in this case (#17)
Support colors in ConEmu and cmder, on Windows
Fix color detection in Emacs tramp
col_substr corner cases:
handle empty chunks at beginning or end of strings
base::strsplit (@brodieG, #26)
explicitly deal with 'split' values that are not length 1 as that is not currently supported
handle zero length
x argument in
add more explicit error messages for corner cases
Some performance improvements to
Change rgb to ANSI code mapping, based on the "paint" ruby gem (@richfitz, #33, #34)
R CMD checkproblems.
Colors are turned on by default in Emacs ESS 23.x and above.
Functions to turn on and off a style:
tput corner cases (@jimhester, #21)
tputexists, but fails with an error and/or does not return anything useful. (@jimhester, #18, #19)
Fix detection of number of colors, it was cached from installation time (#17).
Color aware string operations. They are slow and experimental currently.
show_ansi_colors() prints all supported colors on the screen.
256 colors, on terminals that support it.
Disable colors on Windows, they are not supported in the default setup.