0.0
No commit activity in last 3 years
No release in over 3 years
The cli-colorize gem is a command-line interface colorization library.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

CLIColorize¶ ↑

Description:¶ ↑

A simple Ruby module for use when colorizing output to the terminal.

Installation:¶ ↑

gem install cli-colorize

Usage:¶ ↑

Supported colors (for :foreground and :background):¶ ↑

- :black
- :red
- :green
- :yellow
- :blue
- :magenta
- :cyan
- :white
- :default (use the console's default color)

Supported additional :config options:¶ ↑

- :reset        - Turn off all attributes
- :bright       - Set bright mode
- :underline    - Set underline mode
- :blink        - Set blink mode
- :swap         - Swap foreground and background color
- :hide         - Hide text (set foreground to background color)

String with CLIColorize¶ ↑

require 'cli-colorize'
str = 'The ' + 'Quick'.bright + ' Red'.red + ' Fox ' + 'Jumps'.blink + ' Over the ' + 'Lazy '.underline + 'Brown'.brown + ' Dog'

Using the CLIColorize module directly:¶ ↑

CLIColorize.off
# Prints in blue.
puts CLIColorize.colorize('TEST TEXT', :blue)
# Prints in the default color, NOT in blue
puts CLIColorize.safe_colorize('TEST TEXT', :blue)
CLIColorize.on
# Prints in blue.
puts CLIColorize.safe_colorize('TEST TEXT', :blue)
# Prints in red with a yellow background.
CLIColorize.puts('Is this text in red with a yellow background? (Y/n):', :foreground => :red, :background => :yellow)
CLIColorize.print('Is this text in red with a yellow background? (Y/n):', :foreground => :red, :background => :yellow)
# Prints in red with a yellow background on TTY devices, not to files.
CLIColorize.puts_colorized_if_tty('Is this text in red with a yellow background? (Y/n):', :foreground => :red, :background => :yellow)
CLIColorize.print_colorized_if_tty('Is this text in red with a yellow background? (Y/n):', :foreground => :red, :background => :yellow)

Using the CLIColorize module as a mixin:¶ ↑

require 'cli-colorize'

class HelloConsole
  include CLIColorize

  def initialize
    # set the default foreground color (initially :blue)
    CLIColorize.default_color = :red
  end

  def say_hello
    puts colorize('Hello Pretty Console!')
  end

  def fancy_greeting
    puts colorize('Hello Console.', { :foreground => :blue, :background => :yellow, :config => :underline } )
  end

  def in_color(set)
    if set == true
      CLIColorize.on
    else
      CLIColorize.off
    end
  end
end

console = HelloConsole.new
# prints "Hello Pretty Console!" in red
console.say_hello
# prints "Hello Console." in blue, underlined, with a yellow background
console.fancy_greeting
console.in_color(false)
# not colorized
console.safe_colorize('Hi again.')
console.in_color(true)
# prints "Let's try that again." in red.
console.safe_colorize("Let's try that again.")

Extending the CLIColorize module:¶ ↑

require 'cli-colorize'

class HelloConsole
  extend CLIColorize

  # set the default foreground color (initially :blue)
  default_color = :red

  def self.say_hello
    puts colorize('Hello Pretty Console!')
  end
end

HelloConsole.say_hello
HelloConsole.safe_puts('Is this text in red with a yellow background? (Y/n):', :foreground => :red, :background => :yellow)

License:¶ ↑

Copyright © 2010 Chris St. John

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.