Module: NA::Color
Overview
Terminal output color functions.
Constant Summary collapse
- ESCAPE_REGEX =
Regexp to match excape sequences
/(?<=\[)(?:(?:(?:[349]|10)[0-9]|[0-9])?;?)+(?=m)/.freeze
- ATTRIBUTES =
All available color names. Available as methods and string extensions.
[ [:clear, 0], # String#clear is already used to empty string in Ruby 1.9 [:reset, 0], # synonym for :clear [:bold, 1], [:dark, 2], [:italic, 3], # not widely implemented [:underline, 4], [:underscore, 4], # synonym for :underline [:blink, 5], [:rapid_blink, 6], # not widely implemented [:negative, 7], # no reverse because of String#reverse [:concealed, 8], [:strikethrough, 9], # not widely implemented [:strike, 9], # not widely implemented [:black, 30], [:red, 31], [:green, 32], [:yellow, 33], [:blue, 34], [:magenta, 35], [:purple, 35], [:cyan, 36], [:white, 37], [:bgblack, 40], [:bgred, 41], [:bggreen, 42], [:bgyellow, 43], [:bgblue, 44], [:bgmagenta, 45], [:bgpurple, 45], [:bgcyan, 46], [:bgwhite, 47], [:boldblack, 90], [:boldred, 91], [:boldgreen, 92], [:boldyellow, 93], [:boldblue, 94], [:boldmagenta, 95], [:boldpurple, 95], [:boldcyan, 96], [:boldwhite, 97], [:boldbgblack, 100], [:boldbgred, 101], [:boldbggreen, 102], [:boldbgyellow, 103], [:boldbgblue, 104], [:boldbgmagenta, 105], [:boldbgpurple, 105], [:boldbgcyan, 106], [:boldbgwhite, 107], [:softpurple, '0;35;40'], [:hotpants, '7;34;40'], [:knightrider, '7;30;40'], [:flamingo, '7;31;47'], [:yeller, '1;37;43'], [:whiteboard, '1;30;47'], [:chalkboard, '1;37;40'], [:led, '0;32;40'], [:redacted, '0;30;40'], [:alert, '1;31;43'], [:error, '1;37;41'], [:default, '0;39'] ].map(&:freeze).freeze
- ATTRIBUTE_NAMES =
Array of attribute keys only
ATTRIBUTES.transpose.first
- COLORED_REGEXP =
Regular expression that is used to scan for ANSI-sequences while uncoloring strings.
/\e\[(?:(?:(?:[349]|10)[0-9]|[0-9])?;?)+m/.freeze
Class Attribute Summary collapse
-
.coloring ⇒ Object
Enables colored output.
Class Method Summary collapse
-
.clear_template_cache ⇒ void
Clears the cached compiled color templates.
-
.coloring? ⇒ Boolean
Returns true if the coloring function of this module is switched on, false otherwise.
-
.colors_hash ⇒ Object
Pre-computed colors hash (expensive to create, so we cache it).
-
.template(input) ⇒ String
Convert a template string to a colored string.
-
.template_cache ⇒ Object
Cache for compiled templates to avoid repeated regex processing.
Instance Method Summary collapse
-
#attributes ⇒ Object
Returns an array of all NA::Color attributes as symbols.
-
#rgb(hex) ⇒ String
Generate escape codes for hex colors.
-
#support?(feature) ⇒ Boolean
Returns true if Color supports the
feature. -
#uncolor(string = nil) ⇒ Object
Returns an uncolored version of the string, that is all ANSI-sequences are stripped from the string.
Class Attribute Details
.coloring ⇒ Object
Enables colored output
227 228 229 |
# File 'lib/na/colors.rb', line 227 def coloring @coloring ||= true end |
Class Method Details
.clear_template_cache ⇒ void
This method returns an undefined value.
Clears the cached compiled color templates.
209 210 211 |
# File 'lib/na/colors.rb', line 209 def clear_template_cache @template_cache = {} end |
.coloring? ⇒ Boolean
Returns true if the coloring function of this module is switched on, false otherwise.
194 195 196 |
# File 'lib/na/colors.rb', line 194 def coloring? @coloring end |
.colors_hash ⇒ Object
Pre-computed colors hash (expensive to create, so we cache it)
214 215 216 217 218 219 220 |
# File 'lib/na/colors.rb', line 214 def colors_hash @colors_hash ||= { 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 } end |
.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(‘{Rwb\}Warning:{x\} {w}you look a little {g\}ill{x\}’)
Color.template(‘{#f0a\}This is an RGB color’)
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/na/colors.rb', line 258 def template(input) input = input.join(' ') if input.is_a? Array return input.gsub(/(?<!\\)\{#?(\w+)\}/i, '') unless NA::Color.coloring? # Check cache first cache_key = input.hash return template_cache[cache_key] if template_cache.key?(cache_key) # Process hex colors first processed_input = input.gsub(/(?<!\\)\{((?:[fb]g?)?#[a-f0-9]{3,6})\}/i) do hex = Regexp.last_match(1) rgb(hex) end # Convert to format string fmt = processed_input.gsub('%', '%%') fmt = fmt.gsub(/(?<!\\)\{(\w+)\}/i) do Regexp.last_match(1).chars.map { |c| "%<#{c}>s" }.join end # Use pre-computed colors hash result = format(fmt, colors_hash) # Cache the result template_cache[cache_key] = result result end |
.template_cache ⇒ Object
Cache for compiled templates to avoid repeated regex processing
201 202 203 |
# File 'lib/na/colors.rb', line 201 def template_cache @template_cache ||= {} end |
Instance Method Details
#attributes ⇒ Object
Returns an array of all NA::Color attributes as symbols.
383 384 385 |
# File 'lib/na/colors.rb', line 383 def attributes ATTRIBUTE_NAMES end |
#rgb(hex) ⇒ String
Generate escape codes for hex colors
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/na/colors.rb', line 341 def rgb(hex) is_bg = hex.match(/^bg?#/) ? true : false hex_string = hex.sub(/^([fb]g?)?#/, '') if hex_string.length == 3 parts = hex_string.match(/(?<r>.)(?<g>.)(?<b>.)/) t = [] %w[r g b].each do |e| t << parts[e] t << parts[e] end hex_string = t.join end parts = hex_string.match(/(?<r>..)(?<g>..)(?<b>..)/) t = %w[r g b].map do |e| parts[e].hex end "\e[#{is_bg ? '48' : '38'};2;#{t.join(';')}m" end |
#support?(feature) ⇒ Boolean
Returns true if Color supports the feature.
The feature :clear, that is mixing the clear color attribute into String, is only supported on ruby implementations, that do not already implement the String#clear method. It’s better to use the reset color attribute instead.
96 97 98 99 100 101 |
# File 'lib/na/colors.rb', line 96 def support?(feature) case feature when :clear !String.instance_methods(false).map(&:to_sym).include?(:clear) end end |
#uncolor(string = nil) ⇒ Object
Returns an uncolored version of the string, that is all ANSI-sequences are stripped from the string.
370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/na/colors.rb', line 370 def uncolor(string = nil) # :yields: if block_given? yield.to_str.gsub(COLORED_REGEXP, '') elsif string.respond_to?(:to_str) string.to_str.gsub(COLORED_REGEXP, '') elsif respond_to?(:to_str) to_str.gsub(COLORED_REGEXP, '') else '' end end |