Git Product home page Git Product logo

crayon's Introduction



crayon


Stylish terminal output in R

Project Status: Active - The project has reached a stable, usable state and is being actively developed. R build status CRAN RStudio mirror downloads Coverage Status

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.

Installation

install.packages("crayon")

Styles

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

General 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

Screenshot on OSX

Usage

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 strings 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'))

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"))

256 colors

Most modern terminals support the ANSI standard for 256 colors, and you can define new styles that make use of them. The make_style function defines a new style. It can handle R's built in color names (see the output of colors()) as well as 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")

License

MIT @ Gábor Csárdi

crayon's People

Contributors

brodieg avatar cubranic avatar dnusinow avatar e3bo avatar fmichonneau avatar gaborcsardi avatar jimhester avatar krlmlr avatar mrchypark avatar nfultz avatar rdiaz02 avatar richfitz avatar salim-b avatar vreuter avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.