/simplecolor

SimpleColor ruby gem

Primary LanguageRubyMIT LicenseMIT

simplecolor

Gem Version Ruby test result Build Status

Description

A simple library for coloring text output. Heavily inspired by rainbow, term-ansicolor and paint.

By default this gem does not change the String class, unlike rainbow. The reason I wrote this gem is that when mixed in String it only adds two methods: color and uncolor. This is the main reason I don't use term-ansicolor which is more powerful, but adds more methods when mixed in. The gem paint is similar to this one, but with more powerful shortcuts definitions.

Features

  • No string extensions (suitable for library development)

  • Mixing the library in String only add two methods: color and uncolor.

  • Supports setting any effects (although most terminals won’t support it)

  • Simple to use

Examples

require 'simplecolor'

SimpleColor.color("blue", :blue, :bold)
SimpleColor.color(:blue,:bold) { "blue" }
SimpleColor.color(:blue,:bold) << "blue" << SimpleColor.color(:clear)

SimpleColor.mix_in_string
"blue".color(:blue,:bold)
"\e[34m\e[1mblue\e[0m".uncolor

Usage

There is a global switch SimpleColor.enabled to select the color mode. The possible values are

  • true: activate color output
  • false: desactivate all color output
  • :shell: activate color output for use in zsh prompt

When using a color escape sequence in a prompt in zsh, zsh will count the escape sequences as part of the length of a prompt. To mark them as non printable, one has to wrap them around %{ and %}. With SimpleColor.enabled=:shell this is done automatically by SimpleColor.

Requirements

None.

Install

$ gem install simplecolor

This installs v0.3.0, the current master version may not work.

When installing from git, you can regenerate the list of color names by running data/rgb_colors.rb.

TODO

  • Autodetect color capabilities (using COLORTERM)
  • Use terminfo settings when possible
  • Better color conversion (RGB is non linear)
  • Utilities to darken/lighten colors

Copyright

Copyright © 2013–2020 Damien Robert

MIT License. See LICENSE.txt for details.