Puppet Function: format::colorize

Defined in:
lib/puppet/functions/format/colorize.rb
Function type:
Ruby 4.x API

Summary

Turns any string into a string with colors

Overview

Useful when dumping text to the console with flying colors. Normally this would only be used for puppet types like plans and other scenarios where text can be displayed directory on the console.

Examples:

Calling the function

$red_string = colorize('red alert', red)

With a color code

$red_string = colorize('red alert', 33)

Signatures:

  • format::colorize(String $data, Enum[red, green, yellow, warning, fatal, good] $color_code)String

    Returns The supplied string surrounded with color codes.

    Examples:

    Calling the function

    colorize('hi', red)
    colorize('red alert', fatal)

    Parameters:

    • data (String)

      The string you wish to colorize.

    • color_code (Enum[red, green, yellow, warning, fatal, good])

      The color you want to color it.

    Returns:

    • (String)

      The supplied string surrounded with color codes.

  • format::colorize(String $data, Integer $color_code)String
    Note:

    Please see this article for supplying a custom color code stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences

    Returns The supplied string surrounded with color codes.

    Examples:

    Calling the function

    colorize('red alert', red)
    colorize('red alert', 33)

    Parameters:

    • data (String)

      The string you wish to colorize.

    • color_code (Integer)

      The color you want to color it.

    Returns:

    • (String)

      The supplied string surrounded with color codes.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/functions/format/colorize.rb', line 15

Puppet::Functions.create_function(:'format::colorize') do
  # @param data The string you wish to colorize.
  # @param color_code The color you want to color it.
  # @return [String] The supplied string surrounded with color codes.
  # @example Calling the function
  #   colorize('hi', red)
  #   colorize('red alert', fatal)
  dispatch :colorize do
    param 'String', :data
    param 'Enum[red, green, yellow, warning, fatal, good]', :color_code
  end

  # @param data The string you wish to colorize.
  # @param color_code The color you want to color it.
  # @return [String] The supplied string surrounded with color codes.
  # @example Calling the function
  #   colorize('red alert', red)
  #   colorize('red alert', 33)
  # @note Please see this article for supplying a custom color code
  #       https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences
  dispatch :colorize_hex do
    param 'String', :data
    param 'Integer', :color_code
  end

  def colorize_hex(data, color_code)
    "\e[#{color_code}m#{data}\e[0m"
  end

  def colorize(data, color_code)
    send(color_code.to_sym, data)
  end

  def good(data)
    green(data)
  end

  def red(data)
    colorize_hex(data, 31)
  end

  def green(data)
    colorize_hex(data, 32)
  end

  def warning(data)
    yellow(data)
  end

  def fatal(data)
    red(data)
  end

  def yellow(data)
    colorize_hex(data, 33)
  end
end