Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/na/colors.rb,
lib/na/string.rb
Overview
Template coloring
Instance Method Summary collapse
-
#highlight_tags(color: '{m}', value: '{y}', parens: '{m}', last_color: '{g}') ⇒ String
Colorize @tags with ANSI escapes.
- #indent_level ⇒ Object
-
#last_color_code ⇒ Object
Get the calculated ANSI color at the end of the string.
- #matches(any: [], all: [], none: []) ⇒ Object
- #matches_all(regexes) ⇒ Object
- #matches_any(regexes) ⇒ Object
- #matches_none(regexes) ⇒ Object
-
#normalize_color ⇒ String
Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg.
-
#validate_color ⇒ String
Extract the longest valid %color name from a string.
Instance Method Details
#highlight_tags(color: '{m}', value: '{y}', parens: '{m}', last_color: '{g}') ⇒ String
Colorize @tags with ANSI escapes
20 21 22 23 24 25 |
# File 'lib/na/string.rb', line 20 def (color: '{m}', value: '{y}', parens: '{m}', last_color: '{g}') tag_color = NA::Color.template(color) paren_color = NA::Color.template(parens) value_color = NA::Color.template(value) gsub(/(\s|m)(@[^ ("']+)(?:(\()(.*?)(\)))?/, "\\1#{tag_color}\\2#{paren_color}\\3#{value_color}\\4#{paren_color}\\5#{last_color}") end |
#indent_level ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/na/string.rb', line 4 def indent_level prefix = match(/(^[ \t]+)/) return 0 if prefix.nil? tabs = prefix[1].gsub(/ /, "\t").scan(/\t/).count tabs end |
#last_color_code ⇒ Object
Get the calculated ANSI color at the end of the string
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/na/colors.rb', line 138 def last_color_code m = scan(ESCAPE_REGEX) em = ['0'] fg = nil bg = nil rgbf = nil rgbb = nil m.each do |c| case c when '0' em = ['0'] fg, bg, rgbf, rgbb = nil when /^[34]8/ case c when /^3/ fg = nil rgbf = c when /^4/ bg = nil rgbb = c end else c.split(/;/).each do |i| x = i.to_i if x <= 9 em << x elsif x >= 30 && x <= 39 rgbf = nil fg = x elsif x >= 40 && x <= 49 rgbb = nil bg = x elsif x >= 90 && x <= 97 rgbf = nil fg = x elsif x >= 100 && x <= 107 rgbb = nil bg = x end end end end escape = "\e[#{em.join(';')}m" escape += "\e[#{rgbb}m" if rgbb escape += "\e[#{rgbf}m" if rgbf escape + "\e[#{[fg, bg].delete_if(&:nil?).join(';')}m" end |
#matches(any: [], all: [], none: []) ⇒ Object
27 28 29 |
# File 'lib/na/string.rb', line 27 def matches(any: [], all: [], none: []) matches_any(any) && matches_all(all) && matches_none(none) end |
#matches_all(regexes) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/na/string.rb', line 45 def matches_all(regexes) regexes.each do |rx| return false unless match(Regexp.new(rx, Regexp::IGNORECASE)) end true end |
#matches_any(regexes) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/na/string.rb', line 38 def matches_any(regexes) regexes.each do |rx| return true if match(Regexp.new(rx, Regexp::IGNORECASE)) end false end |
#matches_none(regexes) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/na/string.rb', line 31 def matches_none(regexes) regexes.each do |rx| return false if match(Regexp.new(rx, Regexp::IGNORECASE)) end true end |
#normalize_color ⇒ String
Normalize a color name, removing underscores, replacing “bright” with “bold”, and converting bgbold to boldbg
129 130 131 |
# File 'lib/na/colors.rb', line 129 def normalize_color gsub(/_/, '').sub(/bright/i, 'bold').sub(/bgbold/, 'boldbg') end |
#validate_color ⇒ String
Extract the longest valid %color name from a string.
Allows %colors to bleed into other text and still be recognized, e.g. %greensomething still finds %green.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/na/colors.rb', line 111 def validate_color valid_color = nil compiled = '' normalize_color.split('').each do |char| compiled += char valid_color = compiled if Color.attributes.include?(compiled.to_sym) || compiled =~ /^([fb]g?)?#([a-f0-9]{6})$/i end valid_color end |