Class: NextRails::Tint

Inherits:
Object
  • Object
show all
Defined in:
lib/next_rails/tint.rb

Overview

Lightweight ANSI color/style wrapper with chainable style methods. Wrap a string with ‘NextRails::Tint(“text”)` then chain styles:

NextRails::Tint("hello").red.bold

Instances are effectively immutable: each style method returns a new ‘Tint` rather than mutating the receiver, so a reference can be reused without styles accumulating across chains.

Constant Summary collapse

CODES =
{
  bold: 1,
  italic: 3,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(string, codes = []) ⇒ Tint

Returns a new instance of Tint.



25
26
27
28
# File 'lib/next_rails/tint.rb', line 25

def initialize(string, codes = [])
  @string = string.to_s
  @codes = codes
end

Instance Method Details

#to_sObject Also known as: to_str



36
37
38
39
40
# File 'lib/next_rails/tint.rb', line 36

def to_s
  return @string if @codes.empty?

  "\e[#{@codes.join(";")}m#{@string}\e[0m"
end