Sha256: 0883f03731d7e361ba78fd2b709ca318cd6ccd61aafb24dd6d83c093145ca7c7

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require_relative 'repository_command'

module Topicz::Commands

  class ReportCommand < RepositoryCommand

    def initialize(config_file = nil, arguments = [])
      super(config_file)
      @week = Date.today.cweek
      @year = Date.today.cwyear
      option_parser.order! arguments
    end

    def option_parser
      OptionParser.new do |options|
        options.banner = 'Usage: report'
        options.on('-w', '--week WEEK', 'Use week WEEK instead of the current week') do |week|
          @week = week.to_i
        end
        options.on('-y', '--year YEAR', 'Use year YEAR instead of the current year') do |year|
          @year = year.to_i
        end
        options.separator ''
        options.separator 'Generates a weekly report from all journals across all topics in a single Markdown file.'
      end
    end

    def execute
      year = @year.to_s
      week = @week.to_s.rjust(2, '0')
      path = File.join(Topicz::DIR_JOURNAL, "#{year}-week-#{week}.md")

      @repository.topics.each do |topic|
        journal = File.join(topic.fullpath, path)
        next unless File.exist? journal

        puts "## #{topic.title}"
        puts
        puts File.readlines(journal)
                 .drop(2).join()          # Drop first 2 lines: title (week) and empty line
                 .gsub(/^#(.*)/, '##\1')  # Add an extra '#' in front of every title
                 .strip                   # Remove leading and ending whitespace
        puts
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
topicz-0.3.0 lib/topicz/commands/report_command.rb