Project

kolor

0.0
No release in over 3 years
Ruby library for terminal text styling using ANSI escape codes. Supports basic colors, 256-color palette, RGB/true colors, gradients, themes, and CLI.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 13.0
~> 3.12
~> 1.50
 Project Readme

Kolor

Gem Version Documentation License: BSD-3-Clause Coverage Status Ruby

Modern Ruby library for terminal text styling using ANSI escape codes with CLI support.

Installation

Add to your Gemfile:

gem 'kolor'

Or install directly:

gem install kolor

Library Usage

require 'kolor'

# Foreground colors
puts "Red text".red
puts "Green text".green
puts "Blue text".blue

# Background colors
puts "Text on red".on_red
puts "Text on blue".on_blue

# Styles
puts "Bold text".bold
puts "Underlined text".underline

# Chaining
puts "Red text on white background".red.on_white
puts "Bold green text".green.bold
puts "Complex styling".red.on_blue.bold.underline

CLI Usage

Basic Colors

kolor --red "Error message"
kolor --green "Success!"
kolor --yellow "Warning"
kolor --on-red "Alert"
kolor --white --on-blue "Information"
kolor --bold "Important"
kolor --underline "Emphasized"
kolor --bold --red "Critical"
kolor --red --on-white --bold "Error!"

Themes (requires kolor/extra)

kolor --success "Operation completed"
kolor --error "Operation failed"
kolor --warning "Be careful"
kolor --info "FYI"
kolor --debug "Debugging info"

Advanced Features

kolor --rgb 255,0,0 "Custom red"
kolor --with_hex FF5733 "Hex color"
kolor --gradient red,blue "Gradient text"
kolor --rainbow "Rainbow text"

Utility Commands

kolor --list-colors
kolor --list-styles
kolor --list-themes
kolor --demo
kolor --version
kolor --help

Available Colors

Foreground: black, red, green, yellow, blue, magenta, cyan, white
Background: on_black, on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
Styles: bold, underline, reversed

Advanced Features

Require extra features:

require 'kolor'
require 'kolor/extra'

256 Colors

"Text in color 196".color(196)
"Background color 20".on_color(20)

RGB / True Color

"RGB color".rgb(255, 100, 50)
"RGB background".on_rgb(0, 100, 200)

Hex Colors

"Hex color".with_hex("#FF5733")
"Hex background".on_hex("00BFFF")

Gradients

"Gradient text".gradient(:red, :blue)
"Rainbow colors".rainbow

Themes

Built-in themes:

"Success!".success    # green + bold
"Error!".error        # white on red + bold
"Warning!".warning    # yellow + bold
"Info".info           # cyan
"Debug".debug         # magenta

Custom themes:

Kolor::Extra.theme(:custom, :red, :on_white, :bold)
"Custom styled".custom

Utilities

Disable Colors

kolor --no-color "Plain text"
Kolor.disable!
"Text".red  # => "Text" (no ANSI codes)

Kolor.enable!
"Text".red  # => colored text

Strip ANSI Codes

colored = "Text".red.bold
Kolor.strip(colored)  # => "Text"
colored.uncolorize    # => "Text"

CLI Examples

kolor --green "+ Added line"
kolor --red "- Removed line"
kolor --success "✓ Build passed"
kolor --error "✗ Build failed"
kolor --warning "⚠ Deprecated"
kolor --cyan "[INFO]" "Application started"
kolor --yellow "[WARN]" "Low memory"
kolor --red --bold "[ERROR]" "Connection failed"
kolor --rainbow "Celebrate!"
kolor --gradient red,yellow "Sunset vibes"

Configuration File

Kolor supports a single configuration format:

Ruby DSL (~/.kolorrc.rb)

# Load extra features
require 'kolor/extra'

# Define custom themes
Kolor::Extra.theme(:success, :green, :bold)
Kolor::Extra.theme(:error, :white, :on_red, :bold)
Kolor::Extra.theme(:warning, :yellow, :bold)
Kolor::Extra.theme(:highlight, :black, :on_yellow)
Kolor::Extra.theme(:alert, :yellow, :on_red, :bold, :underline)

# Conditional configuration
Kolor.disable! if ENV['CI']

# Platform-specific themes
if RUBY_PLATFORM.match?(/win32/)
  Kolor::Extra.theme(:win_info, :blue, :bold)
end

Use custom themes from config

kolor --highlight "Important!"
kolor --alert "Warning!"
require 'kolor'
require 'kolor/extra'
require 'kolor/config'

Kolor::Config.init

puts "Success!".success
puts "Error!".error

Environment Variables

  • NO_COLOR or NO_COLORS: Disable colors automatically
  • KOLOR_FORCE: Force colors even when output is redirected to a file

Redirection Support

kolor --red "Error" > log.txt
KOLOR_FORCE=1 kolor --red "Error" > log.txt
echo "Hello World" | kolor --red --bold
cat file.txt | grep "error" | kolor --red

Requirements

  • Ruby >= 3.0.0
  • Windows: Automatically loads win32/console/ansi if available

Development

bundle exec rspec

License

BSD-3-Clause-Attribution

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -am 'Add feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Create a Pull Request