Module: ZTK::ANSI

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

Overview

ANSI Mixin Module

Standard use is to mix this module into String.

Examples:

Mix this module into String to enable easy ANSI coloring methods like:

"bold red".red.bold

Or

"green".green

Author:

Constant Summary

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

- (String) black(string = nil, &block)

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/ztk/ansi.rb', line 33

- (String) blue(string = nil, &block)

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/ztk/ansi.rb', line 33

- (String) bold(string = nil, &block)

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/ztk/ansi.rb', line 87

- (Boolean) build_ansi_methods(hash)

Build ANSI Methods

Dynamicly constructs our 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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ztk/ansi.rb', line 110

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

- (String) cyan(string = nil, &block)

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/ztk/ansi.rb', line 33

- (Object) goto(x = 0, y = 0)



172
173
174
# File 'lib/ztk/ansi.rb', line 172

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

- (String) green(string = nil, &block)

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/ztk/ansi.rb', line 33

- (String) magenta(string = nil, &block)

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/ztk/ansi.rb', line 33

- (String) normal(string = nil, &block)

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/ztk/ansi.rb', line 87

- (String) red(string = nil, &block)

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/ztk/ansi.rb', line 33

- (Object) reset(string = nil, &block)



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ztk/ansi.rb', line 155

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

- (String) uncolor(string = nil, &block)

Uncolor 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.



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ztk/ansi.rb', line 143

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

- (String) white(string = nil, &block)

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/ztk/ansi.rb', line 33

- (String) yellow(string = nil, &block)

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/ztk/ansi.rb', line 33