module Doing::StringHighlight
Tag and search highlighting
Public Instance Methods
highlight_search(search, distance: nil, negate: false, case_type: nil)
click to toggle source
# File lib/doing/string/highlight.rb, line 31 def highlight_search(search, distance: nil, negate: false, case_type: nil) out = dup matching = Doing.setting('search.matching', 'pattern').normalize_matching distance ||= Doing.setting('search.distance', 3).to_i case_type ||= Doing.setting('search.case', 'smart').normalize_case if search.rx? || matching == :fuzzy rx = search.to_rx(distance: distance, case_type: case_type) out.gsub!(rx) { |m| m.bgyellow.black } else query = search.strip.to_phrase_query if query[:must].nil? && query[:must_not].nil? query[:must] = query[:should] query[:should] = [] end qs = [] qs.concat(query[:must]) if query[:must] qs.concat(query[:should]) if query[:should] qs.each do |s| rx = Regexp.new(s.wildcard_to_rx, ignore_case(s, case_type)) out.gsub!(rx) do m = Regexp.last_match last = m.pre_match.last_color_code "#{m[0].bgyellow.black}#{last}" end end end out end
highlight_search!(search, distance: nil, negate: false, case_type: nil)
click to toggle source
# File lib/doing/string/highlight.rb, line 27 def highlight_search!(search, distance: nil, negate: false, case_type: nil) replace highlight_search(search, distance: distance, negate: negate, case_type: case_type) end
last_color()
click to toggle source
Returns the last escape sequence from a string.
Actually returns all escape codes, with the assumption that the result of inserting them will generate the same color as was set at the end of the string. Because you can send modifiers like dark and bold separate from color codes, only using the last code may not render the same style.
@return [String] All escape codes in string
# File lib/doing/string/highlight.rb, line 73 def last_color scan(/\e\[[\d;]+m/).join('') end
uncolor()
click to toggle source
Remove color escape codes
@return clean string
# File lib/doing/string/highlight.rb, line 82 def uncolor gsub(/\e\[[\d;]+m/, '') end
uncolor!()
click to toggle source
@see uncolor
# File lib/doing/string/highlight.rb, line 89 def uncolor! replace uncolor end