Method: Planter::Color.template

Defined in:
lib/planter/color.rb

.template(input) ⇒ String

Convert a template string to a colored string. Colors are specified with single letters inside curly braces. Uppercase changes background color.

w: white, k: black, g: green, l: blue, y: yellow, c: cyan, m: magenta, r: red, b: bold, u: underline, i: italic, x: reset (remove background, color, emphasis)

Also accepts ((#)RGB and ((#)RRGGBB strings. Put a b before the hash to make it a background color

Examples:

Convert a templated string

Color.template('{Rwb}Warning:{x} {w}you look a little {g}ill{x}')

Convert using RGB colors

Color.template('{#f0a}This is an RGB color')


248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/planter/color.rb', line 248

def template(input)
  input = input.join(' ') if input.is_a? Array
  return input.strip_template unless Color.coloring?

  input = input.gsub(/(?<!\\)\{((?:[fb]g?)?#[a-f0-9]{3,6})\}/i) do
    hex = Regexp.last_match(1)
    rgb(hex)
  end

  fmt = input.gsub(/%/, '%%')
  fmt = fmt.gsub(/(?<!\\)\{(\w+)\}/i) do
    Regexp.last_match(1).chars.map { |c| "%<#{c}>s" }.join('')
  end

  colors = { w: white, k: black, g: green, l: blue,
             y: yellow, c: cyan, m: magenta, r: red,
             W: bgwhite, K: bgblack, G: bggreen, L: bgblue,
             Y: bgyellow, C: bgcyan, M: bgmagenta, R: bgred,
             d: dark, b: bold, u: underline, i: italic, x: reset }

  format(fmt, colors)
end