Skip to content

ntl/terminal-colors

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published