Class: Rbnotes::Commands::Search

Inherits:
Command
  • Object
show all
Defined in:
lib/rbnotes/commands/search.rb

Overview

Searches a given pattern in notes those have timestamps match a given timestamp pattern. The first argument is a pattern to search. It is a String object represents a portion of text or it may a String represents a regular expression. The second argument is optional and it is a timestamp pattern to specify the search target.

A pattern for search is mandatory. If no pattern, raises Rbnotes::MissingArgumentError.

Example of PATTERN for search:

"rbnotes" (a word)
"macOS Big Sur" (a few words)
"2[0-9]{3}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])" (a regular expression)

A timestamp pattern is optional. If no timestamp pattern, all notes in the repository would be target of search.

See the document of ‘Rbnotes::Commands::List#execute` to know about a timestamp pattern.

Defined Under Namespace

Classes: SearchEntry

Instance Method Summary collapse

Instance Method Details

#descriptionObject

:nodoc:



27
28
29
# File 'lib/rbnotes/commands/search.rb', line 27

def description             # :nodoc:
  "Search a given pattern in notes"
end

#execute(args, conf) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbnotes/commands/search.rb', line 31

def execute(args, conf)
  @opts = {}
  parse_opts(args)

  pattern = args.shift
  raise MissingArgumentError, args if pattern.nil?

  timestamp_pattern = args.shift # `nil` is acceptable

  repo = Textrepo.init(conf)
  begin
    result = repo.search(pattern, timestamp_pattern)
  rescue Textrepo::InvalidSearchResultError => e
    puts e.message
  end
  print_search_result(result.map{ |e| SearchEntry.new(*e) })
end

#helpObject

:nodoc:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rbnotes/commands/search.rb', line 49

def help                    # :nodoc:
  puts <<HELP
usage:
#{Rbnotes::NAME} search [OPTIONS] PATTERN [STAMP_PATTERN]

PATTERN is a word (or words) to search, it may also be a regular
expression.

OPTIONS:
-s, --subject-only

An option "--subject-only" is acceptable.  It specifies to search in
only the subject of each note.  The subject means the first line of
the note text.

STAMP_PATTERN must be:

(a) full qualified timestamp (with suffix): "20201030160200"
(b) year and date part: "20201030"
(c) year and month part: "202010"
(d) year part only: "2020"
(e) date part only: "1030"

HELP
end