Sha256: 47dd25ad2715eae7117c69738769a888cd8ae641614629d88c0a86214b07ef8b

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

require_relative 'repository_command'
require 'json'

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
      @filter = arguments.join ' '
    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.'
      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

3 entries across 3 versions & 1 rubygems

Version Path
topicz-0.2.0 lib/topicz/commands/report_command.rb
topicz-0.1.1 lib/topicz/commands/report_command.rb
topicz-0.1.0 lib/topicz/commands/report_command.rb