Module: Hoodie::ANSI

Extended by:
ANSI
Included in:
ANSI
Defined in:
lib/hoodie/utils/ansi.rb

Overview

ANSI Mixin Module

Standard use is to mix this module into String.

like:

"bold red".red.bold

Examples:

Mix this module into String to enable easy ANSI coloring methods


Or

"green".green

Constant Summary collapse

ANSI_COLORS =

Defines our ANSI color codes

{
  black:   30,
  red:     31,
  green:   32,
  yellow:  33,
  blue:    34,
  magenta: 35,
  cyan:    36,
  white:   37
}
ANSI_ATTRIBUTES =

Defines our ANSI attribute codes

{
  normal: 0,
  bold:   1
}
ANSI_REGEX =

Defines a RegEx for stripping ANSI codes from strings

/\e\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/

Instance Method Summary collapse

Instance Method Details

#black(string = nil, &block) ⇒ String

Sets the foreground color to black for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#blue(string = nil, &block) ⇒ String

Sets the foreground color to blue for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#bold(string = nil, &block) ⇒ String

Sets the foreground color to bold for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 102


#build_ansi_methods(hash) ⇒ Boolean

Dynamicly constructs ANSI methods for the color methods based on the ANSI code hash passed in.

Parameters:

  • hash (Hash)

    A hash where the keys represent the method names and the values are the ANSI codes.

Returns:

  • (Boolean)

    True if successful.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hoodie/utils/ansi.rb', line 127

def build_ansi_methods(hash)
  hash.each do |key, value|

    define_method(key) do |string=nil, &block|
      result = Array.new

      result << %(\e[#{value}m)
      if block_given?
        result << block.call
      elsif string.respond_to?(:to_str)
        result << string.to_str
      elsif respond_to?(:to_str)
        result << to_str
      else
        return result
      end
      result << %(\e[0m)

      result.join
    end
  end

  true
end

#cyan(string = nil, &block) ⇒ String

Sets the foreground color to cyan for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#goto(x = 0, y = 0) ⇒ Object



192
193
194
# File 'lib/hoodie/utils/ansi.rb', line 192

def goto(x=0, y=0)
  %(\e[#{x};#{y}H)
end

#green(string = nil, &block) ⇒ String

Sets the foreground color to green for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#magenta(string = nil, &block) ⇒ String

Sets the foreground color to magenta for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#normal(string = nil, &block) ⇒ String

Sets the foreground color to normal for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 102


#red(string = nil, &block) ⇒ String

Sets the foreground color to red for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#reset(string = nil, &block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/hoodie/utils/ansi.rb', line 175

def reset(string = nil, &block)
  result = Array.new

  result << %(\e[2J)
  if block_given?
    result << block.call
  elsif string.respond_to?(:to_str)
    result << string.to_str
  elsif respond_to?(:to_str)
    result << to_str
  else
    return result
  end

  result.join
end

#uncolor(string = nil, &block) ⇒ String

Removes ANSI code sequences from a string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The supplied string stripped of ANSI codes.



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hoodie/utils/ansi.rb', line 163

def uncolor(string = nil, &block)
  if block_given?
    block.call.to_str.gsub(ANSI_REGEX, '')
  elsif string.respond_to?(:to_str)
    string.to_str.gsub(ANSI_REGEX, '')
  elsif respond_to?(:to_str)
    to_str.gsub(ANSI_REGEX, '')
  else
    ''
  end
end

#white(string = nil, &block) ⇒ String

Sets the foreground color to white for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48


#yellow(string = nil, &block) ⇒ String

Sets the foreground color to yellow for the supplied string.

Parameters:

  • string (String) (defaults to: nil)

    The string to operate on.

Yield Returns:

  • (String)

    The string to operate on.

Returns:

  • (String)

    The colored string.



# File 'lib/hoodie/utils/ansi.rb', line 48