class TimeSheet::Time::Cmd

Public Instance Methods

command() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 134
def command
  options.arguments.shift || 'default'
end
convert_to_time() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 138
def convert_to_time
  if options[:from]
    options[:from] = options[:from].to_time
  end
  if options[:to].is_a?(Date)
    options[:to] = options[:to].to_time + 24 * 60 * 60
  end
end
default_location() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 86
def default_location
  result = []
  config_file = "#{ENV['HOME']}/.time-sheet.conf"
  if File.exists?(config_file)
    File.read(config_file).split("\n").each do |line|
      if m = line.match(/^([a-z_]+):(.*)$/)
        result << m[2].strip if m[1] == 'location'
      end
    end
  end
  result << "#{ENV['HOME']}/time-sheet" if result.empty?
  result
end
invoice() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 147
def invoice
  convert_to_time

  data = TimeSheet::Time.invoice(options)

  data.each do |package|
    tp = TimeSheet::TablePrinter.new package, options
    puts tp.generate
    puts "\n"
  end

  if options[:package]
    package = (data.last.nil? ? 0 : data.last.map{|entry| entry[1]}.sum)
    total = options[:package] * 60
    percent = (package / total.to_f * 100)

    puts "last package duration: #{package}/#{total} (#{percent.round 2}%)"
  end
end
options() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 100
def options
  @options ||= Slop.parse do |o|
    o.banner = [
      "usage: time.rb [command] [options]\n",
      "visit https://github.com/moritzschepp/time-sheet for further information\n",
      'available commands:',
      "  report (default): list entries conforming to given criteria",
      "  invoice: compress similar entries and filter petty ones. Optionally package for e.g. monthly invoicing",
      "\n  general options:"
    ].join("\n")

    o.boolean '-h', '--help', 'show help'
    o.boolean '--version', 'show the version'
    o.array('-l', '--location', 'a location to gather data from (file, directory or google docs share-url)',
      default: default_location
    )
    o.string '-f', '--from', 'ignore entries older than the date given'
    o.string '-t', '--to', 'ignore entries more recent than the date given'
    o.string '-p', '--project', 'take only entries of this project into account'
    o.string '-a', '--activity', 'take only entries of this activity into account'
    o.string '--tags', 'take only entries with these tags into account (comma separated, not case sensitive)'
    o.string '-d', '--description', 'consider only entries matching this description'
    o.string '-e', '--employee', 'consider only entries for this employee'
    o.float '-r', '--rate', 'use an alternative hourly rate (default: 80.0)', default: 80.00
    o.boolean '-s', '--summary', 'when reporting, add summary section'
    o.boolean '--trim', 'compact the output for processing as CSV', default: false
    o.boolean '-v', '--verbose', 'be more verbose'
    o.boolean '--debug', 'drop into a REPL on errors'
    o.separator "\n  invoice options:"
    o.integer '--package', 'for invoice output: build packages of this duration in hours', default: 0
    o.integer '--petty', 'fold records under a certain threshold into a "misc" activity', default: 0
  end
end
report() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 167
def report
  convert_to_time

  data = TimeSheet::Time.report(options)
  tp = TimeSheet::TablePrinter.new data['entries'], options
  puts tp.generate


  if options[:summary]
    puts "\nSummary:"

    tdata = [['project', 'activity', 'time [m]', 'time [h]', 'price [€]']]
    tdata << [
      'all',
      '',
      TimeSheet::Time::Util.minutes(data['total']),
      TimeSheet::Time::Util.hours(data['total']),
      TimeSheet::Time::Util.price(data['total'], options[:rate])
    ]

    data['projects'].sort_by{|k, v| v['total']}.reverse.to_h.each do |pname, pdata|
      previous = nil

      tdata << '-'
      tdata << [
        pname,
        'all',
        TimeSheet::Time::Util.minutes(pdata['total']),
        TimeSheet::Time::Util.hours(pdata['total']),
        TimeSheet::Time::Util.price(pdata['total'], options[:rate])
      ]
      
      pdata['activities'].sort_by{|k, v| v}.reverse.to_h.each do |aname, atotal|
        tdata << [
          '',
          aname,
          TimeSheet::Time::Util.minutes(atotal),
          TimeSheet::Time::Util.hours(atotal),
          TimeSheet::Time::Util.price(atotal, options[:rate])
        ]
        previous = pname
      end
    end

    tdata << '-'

    tp = TimeSheet::TablePrinter.new tdata, options
    puts tp.generate

    puts [
      "days: #{data['averages']['days']}",
      "worked: h/day: #{data['averages']['hours_per_day'].round(2)}",
      "h/workday: #{data['averages']['hours_per_workday'].round(2)}",
      "h/week: #{data['averages']['hours_per_week'].round(2)}",
      "h/month(30 days): #{data['averages']['hours_per_month'].round(2)}"
    ].join(', ')
  end
end
run() click to toggle source
# File lib/time_sheet/time/cmd.rb, line 5
def run
  TimeSheet.options = options

  if d = options[:from]
    if d.match(/^\d\d?-\d\d?$/)
      d = "#{TimeSheet::Time::Util.now.year}-#{d}"
    end

    if d.match(/^\d{4}$/)
      d = "#{d}-01-01"
    end

    options[:from] = Time.parse(d)
  end

  if d = options[:to]
    if d.match(/^\d\d?-\d\d?$/)
      d = "#{TimeSheet::Time::Util.now.year}-#{d}"
    end

    if d.match(/^\d{4}$/)
      d = "#{d}-12-31"
    end

    options[:to] = Time.parse(d)
  end

  if options[:help]
    puts options
  elsif options[:version]
    puts TimeSheet::VERSION
  else
    case command
      when 'invoice'
        invoice
      when 'report', 'default'
        report
      when 'today', 't'
        options[:from] = TimeSheet::Time::Util.today
        options[:summary] = true
        report
      when 'yesterday', 'y'
        options[:from] = TimeSheet::Time::Util.yesterday
        options[:to] = TimeSheet::Time::Util.yesterday
        options[:summary] = true
        report
      when 'week', 'w'
        options[:from] = TimeSheet::Time::Util.week_start
        options[:summary] = true
        report
      when 'last-week', 'lw'
        options[:from] = TimeSheet::Time::Util.week_start(-1)
        options[:to] = TimeSheet::Time::Util.week_end(-1)
        options[:summary] = true
        report
      when 'month', 'm'
        options[:from] = TimeSheet::Time::Util.month_start
        options[:summary] = true
        report
      when 'last-month', 'lm'
        options[:from] = TimeSheet::Time::Util.month_start(-1)
        options[:to] = TimeSheet::Time::Util.month_end(-1)
        options[:summary] = true
        report
      when 'year-to-day', 'year'
        options[:from] = TimeSheet::Time::Util.year_start(-1)
        options[:summary] = true
        report
      when 'overview'
        overview
      else
        raise "unknown command: #{command}"
    end

    if options[:verbose]
      puts "\noptions:"
      p options.to_h
    end
  end
end