Class: Nanoc::CLI::ANSIStringColorizer Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/cli/ansi_string_colorizer.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A simple ANSI colorizer for strings. When given a string and a list of attributes, it returns a colorized string.

Constant Summary collapse

CLEAR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"\e[0m"
MAPPING =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  bold: "\e[1m",

  black: "\e[30m",
  red: "\e[31m",
  green: "\e[32m",
  yellow: "\e[33m",
  blue: "\e[34m",
  magenta: "\e[35m",
  cyan: "\e[36m",
  white: "\e[37m",
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ ANSIStringColorizer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ANSIStringColorizer.



25
26
27
# File 'lib/nanoc/cli/ansi_string_colorizer.rb', line 25

def initialize(io)
  @io = io
end

Instance Method Details

#c(str, *attrs) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns A string colorized using the given attributes.



41
42
43
44
45
46
47
# File 'lib/nanoc/cli/ansi_string_colorizer.rb', line 41

def c(str, *attrs)
  if enabled?
    attrs.map { |a| MAPPING[a] }.join('') + str + CLEAR
  else
    str
  end
end

#enabled?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
# File 'lib/nanoc/cli/ansi_string_colorizer.rb', line 29

def enabled?
  return @_enabled if defined?(@_enabled)

  @_enabled = Nanoc::CLI.enable_ansi_colors?(@io)
end