Skip to contents

With crayon it is easy to add color to terminal output, create styles for notes, warnings, errors; and combine styles.

Usage

## Simple styles
red(...)
bold(...)
# ...

## See more styling below

Arguments

...

Strings to style.

Details

ANSI color support is automatically detected and used. Crayon was largely inspired by chalk https://github.com/chalk/chalk.

Crayon defines several styles, that can be combined. Each style in the list has a corresponding function with the same name.

Genaral styles

  • reset

  • bold

  • blurred (usually called ‘dim’, renamed to avoid name clash)

  • italic (not widely supported)

  • underline

  • inverse

  • hidden

  • strikethrough (not widely supported)

Text colors

  • black

  • red

  • green

  • yellow

  • blue

  • magenta

  • cyan

  • white

  • silver (usually called ‘gray’, renamed to avoid name clash)

Background colors

  • bgBlack

  • bgRed

  • bgGreen

  • bgYellow

  • bgBlue

  • bgMagenta

  • bgCyan

  • bgWhite

Styling

The styling functions take any number of character vectors as arguments, and they concatenate and style them:

  library(crayon)
  cat(blue("Hello", "world!\n"))

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 $ operator:

  cat(yellow$bgMagenta$bold('Hello world!\n'))

See also combine_styles().

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 $ bold
  warn <- magenta $ underline
  note <- cyan
  cat(error("Error: subscript out of bounds!\n"))
  cat(warn("Warning: shorter argument was recycled.\n"))
  cat(note("Note: no such directory.\n"))

Author

Maintainer: Gábor Csárdi csardi.gabor@gmail.com

Other contributors:

Examples

cat(blue("Hello", "world!"))
#> Hello world!

cat("... to highlight the " %+% red("search term") %+%
    " in a block of text")
#> ... to highlight the search term in a block of text

cat(yellow$bgMagenta$bold('Hello world!'))
#> Hello world!

cat(green(
 'I am a green line ' %+%
 blue$underline$bold('with a blue substring') %+%
 ' that becomes green again!'
))
#> I am a green line with a blue substring that becomes green again!

error <- red $ bold
warn <- magenta $ underline
note <- cyan
cat(error("Error: subscript out of bounds!\n"))
#> Error: subscript out of bounds!
#> 
cat(warn("Warning: shorter argument was recycled.\n"))
#> Warning: shorter argument was recycled.
#> 
cat(note("Note: no such directory.\n"))
#> Note: no such directory.
#>