Method: NA::Color.template
- Defined in:
- lib/na/colors.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
Color.template(‘RwbWarning:x wyou look a little gillx’)
Color.template(‘#f0aThis is an RGB color’)
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/na/colors.rb', line 235 def template(input) input = input.join(' ') if input.is_a? Array return input.gsub(/(?<!\\)\{#?(\w+)\}/i, '') unless NA::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).split('').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 |