module Notes::Tasks

Public Instance Methods

all(options = Notes::Options.defaults) click to toggle source

Return list of tasks for the provided options If no options are provided, will use default file locations and flags

Returns Array

# File lib/notes-cli/tasks.rb, line 126
def all(options = Notes::Options.defaults)
  files = Notes.valid_files(options)
  for_files(files, options)
end
for_file(filename, flags) click to toggle source

Parse a file and construct Task objects for each line matching one of the patterns specified in ‘flags`

filename - A String filename to read flags - Array of String flags to match against

Returns Array<Notes::Task>

# File lib/notes-cli/tasks.rb, line 68
def for_file(filename, flags)
  counter = 1
  tasks   = []

  begin
    lines = File.readlines(filename).map(&:chomp)

    lines.each_with_index do |line, idx|
      matched_flags = matching_flags(line, flags)

      if matched_flags.any?
        task_options = {
          filename: Notes.shortname(filename),
          line_num: counter,
          line: line,
          flags: matched_flags,
          context: context_lines(lines, idx)
        }
        # Extract line information from git
        info = line_info(filename, idx)
        task_options[:author] = info[:author]
        task_options[:date]   = info[:date]
        task_options[:sha]    = info[:sha]
        tasks << Notes::Task.new(task_options)
      end
      counter += 1
    end
  rescue
    # Error occurred reading the file (ex: invalid byte sequence in UTF-8)
    # Move on quietly
  end

  tasks
end
for_files(files, options) click to toggle source

Compute all tasks for a set of files and flags

files - Array of String filenames options - Hash of options

:flags - Array of String flags to match against

Returns Array

# File lib/notes-cli/tasks.rb, line 110
def for_files(files, options)
  flags  = options[:flags]
  result = []
  files.each do |filename|
    tasks = Notes::Tasks.for_file(filename, flags)
    result += tasks
  end

  result
end
matching_flags(line, flags) click to toggle source

Return array of flags matched in a line

line - A String to match against flags - An Array of String flags to search for

Returns Array of string flags found

# File lib/notes-cli/tasks.rb, line 56
def matching_flags(line, flags)
  words = line.split(/\W/).map(&:upcase)
  words & flags.map(&:upcase)
end

Private Instance Methods

context_lines(lines, idx) click to toggle source

Return up to 5 lines following the line at idx TODO: it might be better to have before_context and after_context

# File lib/notes-cli/tasks.rb, line 135
def context_lines(lines, idx)
  ctx = []
  1.upto(5) do |i|
    num = idx+i
    break unless lines[num]
    ctx << lines[num]
  end
  ctx.join("\n")
end
line_info(filename, idx) click to toggle source

Information about a line from git (author, date, etc)

filename - A String filename idx - a 0-based line number

Returns Hash

# File lib/notes-cli/tasks.rb, line 151
def line_info(filename, idx)
  result = {}
  return result unless Notes.git?

  fields = Notes.blame(filename, idx+1)

  author = fields["author"]
  result[:author] = author if author && !author.empty?

  time = fields["author-time"] # ISO 8601
  result[:date] = Time.at(time.to_i).to_s if time && !time.empty?

  sha = fields["sha"]
  result[:sha] = sha if sha

  result
end