Colorizer

Download .zip   Download .tar.gz   View on GitHub

This repository is a Groovy library for colorizing terminal output.

Installation

Download the JAR file and add it to your classpath, or clone the project and build the JAR yourself:

$ git clone https://github.com/caseyscarborough/groovy-colorizer
$ cd groovy-colorizer
$ ./gradlew build

Usage

Add colors to your strings using the syntax [blue]foo [red]bar. Then colorize them using the Colorizer#colorize method. You can change the background color by using an underscore before the color, like so: [_lightBlue][black]hello.

import com.caseyscarborough.colorizer.Colorizer

def sentence = "[blue]Hello [_yellow][red]world!"
println Colorizer.colorize(sentence)

In addition to this, mixin methods have been created on the Groovy String class to allow you to call the color names as methods on a String. These methods can also be chained. To use these methods, you must first call Colorizer#initializeMixins().

// Add the methods to the String metaclass
Colorizer.initializeMixins()

"Hello, world!".red()  // Outputs the string red
"Foo"._darkGray()      // Outputs the string with dark gray background
"Bar".black()._white() // Outputs black text on white background

The following are the colors that can be used for text and background coloring:

  • default
  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • lightGray
  • darkGray
  • lightRed
  • lightGreen
  • lightYellow
  • lightBlue
  • lightMagenta
  • lightCyan
  • white

Testing

$ ./gradlew test

Credits

Inspired by colorstring for Go.