Module: Doing::StringHighlight

Included in:
String
Defined in:
lib/doing/string/highlight.rb

Overview

Tag and search highlighting

Instance Method Summary collapse

Instance Method Details

#highlight_search(search, distance: nil, negate: false, case_type: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



27
28
29
# 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

#highlight_tags(color = 'yellow', last_color: nil) ⇒ String

Colorize @tags with ANSI escapes

Parameters:

  • color (String) (defaults to: 'yellow')

    color (see #Color)

Returns:

  • (String)

    string with @tags highlighted



18
19
20
21
22
23
24
25
# File 'lib/doing/string/highlight.rb', line 18

def highlight_tags(color = 'yellow', last_color: nil)
  unless last_color
    color = color.split(' ') unless color.is_a?(Array)
    tag_color = color.each_with_object([]) { |c, arr| arr << Doing::Color.send(c) }.join('')
    last_color = last_color_code
  end
  gsub(/(\s|m)(@[^ ("']+)/, "\\1#{tag_color}\\2#{last_color}")
end

#highlight_tags!(color = 'yellow', last_color: nil) ⇒ Object

Parameters:

  • color (String) (defaults to: 'yellow')

    color (see #Color)



7
8
9
# File 'lib/doing/string/highlight.rb', line 7

def highlight_tags!(color = 'yellow', last_color: nil)
  replace highlight_tags(color)
end

#last_colorString

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.

Returns:

  • (String)

    All escape codes in string



73
74
75
# File 'lib/doing/string/highlight.rb', line 73

def last_color
  scan(/\e\[[\d;]+m/).join('')
end

#uncolorObject

Remove color escape codes

Returns:

  • clean string



82
83
84
# File 'lib/doing/string/highlight.rb', line 82

def uncolor
  gsub(/\e\[[\d;]+m/, '')
end

#uncolor!Object

See Also:



89
90
91
# File 'lib/doing/string/highlight.rb', line 89

def uncolor!
  replace uncolor
end