class Rbnotes::Commands::List

Defines `list` command for `rbnotes`. See the document of execute method to know about the behavior of this command.

Public Instance Methods

execute(Array, Rbnotes::Conf or Hash) → nil click to toggle source

Shows a list of notes in the repository. Arguments are optional. If several args are passed, each of them must be a timestamp pattern or a keyword.

Any order of timestamp patterns and keywords mixture is acceptable. The redundant patterns or invalid patterns are just ignored.

A timestamp pattern is a string which would match several Timestamp objects. A timestamp is an instance of Textrepo::Timestamp class.

A keyword must be one of them:

- "today"      (or "to")
- "yeasterday" (or "ye")
- "this_week"  (or "tw")
- "last_week"  (or "lw")
- "this_month" (or "tm")
- "last_month" (or "lm")
- "all"

Here is several examples of timestamp patterns.

"20201027093600_012": a complete string to represent a timestamp
- this pattern would match exactly one Timestamp object

"20201027": specifies year and date
- all Timestamps those have the same year and date would match

"202011": specifies year and month
- all Timestamps those have the same year and month would match

"2020": specifies year only
- all Timestamps those have the same year would match

"1027": specifies date only
- all Timestamps those have the same date would match, even if
  they have the different year.
# File lib/rbnotes/commands/list.rb, line 59
def execute(args, conf)
  @opts = {}
  parse_opts(args)

  if args.empty? and !@opts[:enum_week]
    default_behavior = conf[:list_default] || DEFAULT_BEHAVIOR
    args << default_behavior
  end

  utils = Rbnotes.utils
  patterns = utils.read_timestamp_patterns(args, enum_week: @opts[:enum_week])

  repo = Textrepo.init(conf)
  stamps = utils.find_notes(patterns, repo)
  output = []
  if @opts[:verbose]
    collect_timestamps_by_date(stamps).each { |date, timestamps|
      output << "#{date} (#{timestamps.size})"
      timestamps.each { |timestamp|
        pad = "  "
        output << utils.make_headline(timestamp,
                                      repo.read(timestamp), pad)
      }
    }
  else
    stamps.each { |timestamp|
      output << utils.make_headline(timestamp, repo.read(timestamp))
    }
  end
  puts output
end