0.0
No commit activity in last 3 years
No release in over 3 years
Colorizes text terminal output by converting custom HTML-like tags into color ANSI escape sequences.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 2.0
 Project Readme

AnsiChameleon

AnsiChameleon is a Ruby Gem that colorizes text terminal output by converting custom HTML-like tags into color ANSI escape sequences.

Installation

As a Ruby Gem, AnsiChameleon can be installed either by running

  gem install ansi_chameleon

or adding

  gem "ansi_chameleon"

to the Gemfile and then invoking bundle install.

Usage

For single use:

  require "ansi_chameleon"

  puts AnsiChameleon.render(
    "There are <number>3</number> oranges on the table.",
    :number => { :fg_color => :blue }
  )

For multiple use this is more efficient:

  require "ansi_chameleon"

  text_renderer = AnsiChameleon::TextRenderer.new(:number => { :fg_color => :blue })

  puts text_renderer.render("There are <number>2</number> oranges on the table.")
  puts text_renderer.render("Now, there is only <number>1</number> orange.")

Details

In order to produce a colorized output AnsiChameleon needs a text and a style sheet.

Text

Text to colorize is expected to include HTML-like tags of custom names, that will then be converted according to the rules defined in provided style sheet.

Tags not included in the style sheet but found in the text being rendered are put to the output without modification.

Style sheet

A style sheet is simply a CSS-like Hash structure with keys that correspond to custom tag names and property names:

  style_sheet = {
    # default style declarations (for text not wrapped in tags)
    :fg_color => :green,
    :bg_color => :black,

    :number => {
      # style declarations for <number> tag
      :fg_color => :blue,
    },
    :message => {
      # style declarations for <message> tag
      :fg_color => :red,
      :bg_color => :white,

      :number => {
        # style declarations for <number> tag nested in <message> tag
        :fg_color => :inherit
      }
    }
  }

Tag names in a style sheet can be written as either :symbols or "strings".

Properties

  • :foreground_color (aliases: :foreground, :fg_color)

  • :background_color (aliases: :background, :bg_color)

    Available color values: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white and a number/string between 0 and 255 that represents a color in xterm 256 color scale.

  • :effect

    Available effect values: :none, :bright, :underline, :blink, :reverse.

Also, the :inherit value can be set for a property, which means that its value will be inherited from the surrounding tag (or be the default value).

Similarly to tag names, property names and values can be provided as :symbols or "strings".

License

License is included in the LICENSE file.