Module: Ace::Support::Items::Atoms::AnsiColors

Defined in:
lib/ace/support/items/atoms/ansi_colors.rb

Overview

Simple ANSI color helpers for terminal output. Automatically skips color codes when stdout is not a TTY.

Constant Summary collapse

RED =
"\e[31m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
CYAN =
"\e[36m"
DIM =
"\e[2m"
BOLD =
"\e[1m"
RESET =
"\e[0m"

Class Method Summary collapse

Class Method Details

.colorize(text, color_code) ⇒ String

Wrap text in ANSI color codes. Returns plain text when stdout is not a TTY.

Parameters:

  • text (String) —

    Text to colorize

  • color_code (String) —

    ANSI escape code (e.g. AnsiColors::GREEN)

Returns:

  • (String)


23
24
25
26
27
# File 'lib/ace/support/items/atoms/ansi_colors.rb', line 23

def self.colorize(text, color_code)
  return text unless tty?

  "#{color_code}#{text}#{RESET}"
end

.tty? ⇒ Boolean

Returns Whether stdout is a TTY.

Returns:

  • (Boolean) —

    Whether stdout is a TTY



30
31
32
# File 'lib/ace/support/items/atoms/ansi_colors.rb', line 30

def self.tty?
  $stdout.tty?
end