Method: Commands#search

Defined in:
lib/cnote/commands.rb

#search(term) ⇒ Object

/================================#

The Commands           #

================================/#



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cnote/commands.rb', line 6

def search(term)
  term = term.downcase.strip # Search is case insensitive
  matches = @notes

  if term.include? "+t"
    term, tags = term.split("+t")
    if tags
      tags = tags.split(" ")
      matches = matches.select do |num, note|
        note != nil && has_tags(note, tags)
      end
    else
      # +t but no tags - return all results that have at least one tag
      matches = matches.select do |num, note|
        note != nil && note.tags.length > 0
      end
    end
  elsif term.include? "-t"
    term, tags = term.split("-t")
    if tags
      tags = tags.split(" ")
      matches = matches.select do |num, note|
        note != nil && does_not_have_tags(note, tags)
      end
    else
      # Likewise, return all results with no tags
      matches = matches.select do |num, note|
        note != nil && note.tags.length == 0
      end
    end
  end

  if term && term != ""
    matches = matches.select do |num, note|
      note.title.downcase.include?(term) || note.content.downcase.include?(term)
    end
  end

  set_filtered(matches)

  # TODO: Sort by most relevant
  # TODO: Highlight keywords where found
  len = matches.length

  print_list("Found #{len} Match#{"es" if len != 1}", @filtered)
end