0.0
No commit activity in last 3 years
No release in over 3 years
Apply ANSI terminal colors to strings
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Terminal Colors

Apply ANSI terminal colors to strings

Installation

To install the terminal_colors library using rubygems:

gem install terminal_colors

Via bundler:

gem 'terminal_colors'

Usage

Use TerminalColors::Apply.call to apply terminal colors to strings. Due to the way ruby specially handles methods named call, this can be shortened to TerminalColors::Apply.(). This method does not alter the input string. Instead, it returns a new string with the specified styling applied to it.

To apply colors:

# Foreground color
TerminalColors::Apply.('Some string', fg: 'blue')

# Background color
TerminalColors::Apply.('Some string', bg: 'magenta')

While not a color, strictly speaking, bold (or bright) foreground text can also be applied:

# Bold (bright) foreground text
TerminalColors::Apply.('Some string', bold: true)

The arguments can be combined, so that foreground colors, background colors, and boldness can all be applied:

# Foreground color
TerminalColors::Apply.('Some string', fg: 'blue', bg: 'magenta', bold: true)

The color labels known by this library can be queried:

# Returns a list of color labels
TerminalColors::Palette.list

For performance reasons, the numeric value of a label can be precomputed and reused:

blue_index = TerminalColors::Palette.fetch 'blue'

TerminalColors::Apply.(string, fg: blue_index)

License

Terminal Colors is licensed under the MIT license

Copyright © Nathan Ladd