module Doing::Color
Terminal output color functions.
Constants
- ATTRIBUTES
-
All available color names. Available as methods and string extensions.
@example Use a color as a method.
Color
reset will be added to end of string. Color.yellow(‘This text is yellow’) => “e[33mThis text is yellowe[0m”@example Use a color as a string extension.
Color
reset added automatically. ‘This text is green’.green => “e[1;32mThis text is greene[0m”@example Send a text string as a color Color.send(‘red’) => “e[31m”
- ATTRIBUTE_NAMES
- ESCAPE_REGEX
Attributes
Public Class Methods
Source
# File lib/doing/colors.rb, line 201 def coloring @coloring ||= true end
Enables colored output
@example Turn color on or off based on TTY Doing::Color.coloring
= STDOUT.isatty
Source
# File lib/doing/colors.rb, line 190 def coloring? @coloring end
Returns true if the coloring function of this module is switched on, false otherwise.
Source
# File lib/doing/colors.rb, line 224 def template(input) input = input.join(' ') if input.is_a? Array fmt = input.gsub(/%/, '%%') fmt = fmt.gsub(/(?<!\\u|\$)\{(\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 } fmt.empty? ? input : format(fmt, colors) end
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)
@example Convert a templated string Color.template
(‘{Rwb}Warning:{x} {w}you look a little {g}ill{x}’)
@param input [String, Array] The template string. If this is an array, the elements will be joined with a space.
@return [String] Colorized string
Public Instance Methods
Source
# File lib/doing/colors.rb, line 314 def attributes ATTRIBUTE_NAMES end
Returns an array of all Doing::Color
attributes as symbols.
Source
# File lib/doing/colors.rb, line 286 def rgb(hex) is_bg = hex.match(/^bg?#/) ? true : false hex_string = hex.sub(/^([fb]g?)?#/, '') parts = hex_string.match(/(?<r>..)(?<g>..)(?<b>..)/) t = [] %w[r g b].each do |e| t << parts[e].hex end color = "\e[#{is_bg ? '48' : '38'};2;#{t.join(';')}m" end
Source
# File lib/doing/colors.rb, line 90 def support?(feature) case feature when :clear !String.instance_methods(false).map(&:to_sym).include?(:clear) end end
Returns true if Doing::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.
Source
# File lib/doing/colors.rb, line 301 def uncolor(string = nil) # :yields: if block_given? yield.to_str.gsub(ESCAPE_REGEX, '') elsif string.respond_to?(:to_str) string.to_str.gsub(ESCAPE_REGEX, '') elsif respond_to?(:to_str) to_str.gsub(ESCAPE_REGEX, '') else '' end end
Returns an uncolored version of the string, that is all ANSI-sequences are stripped from the string.