Code Coverage
 
Lines
Covered
29.03% covered (danger)
29.03%
45 / 155
1
require 'date'
1
require 'time'
1
module TheFox
1
module Timr
1
module Command
# By default this Command prints all todays [Tracks](rdoc-ref:TheFox::Timr::Model::Track).
#
# Man page: [timr-log(1)](../../../../man/timr-log.1.html)
1
class LogCommand < BasicCommand
1
include TheFox::Timr::Helper
1
include TheFox::Timr::Error
# Path to man page.
1
MAN_PATH = 'man/timr-log.1'
1
def initialize(argv = Array.new)
10
super()
10
@help_opt = false
10
@from_opt = nil
10
@to_opt = nil
10
@start_date = nil
10
@end_date = nil
10
@start_time = nil
10
@end_time = nil
10
loop_c = 0 # Limit the loop.
10
while loop_c < 1024 && argv.length > 0
9
loop_c += 1
9
arg = argv.shift
9
case arg
when '-h', '--help'
1
@help_opt = true
when '-s', '--from'
1
@from_opt = Time.parse(argv.shift)
when '-e', '--to'
1
@to_opt = Time.parse(argv.shift)
when '-d', '--day'
0
@from_opt, @to_opt = DateTimeHelper.parse_day_argv(argv)
when '-m', '--month'
0
@from_opt, @to_opt = DateTimeHelper.parse_month_argv(argv)
when '-y', '--year'
0
@from_opt, @to_opt = DateTimeHelper.parse_year_argv(argv)
when '-a', '--all'
1
@from_opt = Time.parse('1970-01-01 00:00:00')
1
@to_opt = Time.parse('2099-12-31 23:59:59')
when '--sd', '--start-date'
1
@start_date = Time.parse(argv.shift)
when '--ed', '--end-date'
1
@end_date = Time.parse(argv.shift)
when '--st', '--start-time'
1
@start_time = Time.parse(argv.shift)
when '--et', '--end-time'
1
@end_time = Time.parse(argv.shift)
else
1
raise LogCommandError, "Unknown argument '#{arg}'. See 'timr log --help'."
end
end
9
@daytime_filter = false
9
if @start_date && @end_date && @start_time && @end_time
0
@from_opt = Time.parse("#{@start_date.strftime('%F')} #{@start_time.strftime('%T')}")
0
@to_opt = Time.parse("#{@end_date.strftime('%F')} #{@end_time.strftime('%T')}")
0
@daytime_filter = true
end
9
today = Date.today
9
unless @from_opt
7
@from_opt = Time.new(today.year, today.month, today.day, 0, 0, 0)
end
9
unless @to_opt
7
@to_opt = Time.new(today.year, today.month, today.day, 23, 59, 59)
end
9
@filter_options = {:from => @from_opt, :to => @to_opt}
end
# See BasicCommand#run.
1
def run
0
if @help_opt
0
help
0
return
end
0
@timr = Timr.new(@cwd)
0
print_small_table
end
1
private
1
def print_small_table
0
puts 'Selected datetime range:'
0
if @daytime_filter
0
puts "On every day from #{@start_date.strftime('%F')}"
0
puts " to #{@end_date.strftime('%F')}"
0
puts " between #{@start_time.strftime('%T %z')}"
0
puts " and #{@end_time.strftime('%T %z')}"
else
0
puts " From #{@from_opt.strftime('%F %T %z')}"
0
puts " To #{@to_opt.strftime('%F %T %z')}"
end
0
puts
0
table_options = {
:headings => [
{:format => '%3s', :label => '###'},
{:format => '%-14s', :label => 'START', :padding_left => ' ', :padding_right => ' '},
{:format => '%-14s', :label => 'END', :padding_left => ' ', :padding_right => ' '},
{:format => '%7s', :label => 'DUR', :padding_left => ' ', :padding_right => ' '},
{:format => '%-6s', :label => 'TASK', :padding_right => ' '},
{:format => '%s', :label => 'TRACK'},
],
}
0
table = Table.new(table_options)
0
totals = {
:duration => Duration.new,
:task_c => 0,
:begin_datetime => nil,
:end_datetime => nil,
}
0
tmp_begin_options = {:format => '%y-%m-%d %H:%M'}
0
tmp_end_options = {:format => '%H:%M %y-%m-%d'}
0
glob_begin_options = {
:format => tmp_begin_options[:format],
:from => @from_opt,
}
0
glob_end_options = {
:format => tmp_end_options[:format],
:to => @to_opt,
}
0
table_has_rows = false
0
@timr.tracks(@filter_options).each do |track_id, track|
0
table_has_rows = true
0
task = track.task
0
if @daytime_filter
0
track.days.each do |track_day|
0
from = Time.parse("#{track_day.strftime('%F')} #{@start_time.strftime('%T')}")
0
to = Time.parse("#{track_day.strftime('%F')} #{@end_time.strftime('%T')}")
# Skip out-of-scope Tracks.
0
if track.end_datetime < from || track.begin_datetime > to
0
next
end
0
totals[:task_c] += 1
0
bdt = track.begin_datetime(@filter_options)
0
edt = track.end_datetime(@filter_options)
0
if bdt && (!totals[:begin_datetime] || bdt < totals[:begin_datetime])
0
totals[:begin_datetime] = bdt
end
0
if edt && (!totals[:end_datetime] || edt > totals[:end_datetime])
0
totals[:end_datetime] = edt
end
0
tmp_begin_options[:from] = from
0
tmp_end_options[:to] = to
0
begin_datetime_s = track.begin_datetime_s(tmp_begin_options)
0
end_datetime_s = track.end_datetime_s(tmp_end_options)
0
duration = track.duration({:from => from, :to => to})
0
if duration
0
totals[:duration] += duration
end
table << [
track_c,
begin_datetime_s,
end_datetime_s,
duration.to_human_s,
task.id_foreign_or_short,
'%s %s' % [track.short_id, track.title(15)],
0
]
end
else
0
totals[:task_c] += 1
0
bdt = track.begin_datetime(@filter_options)
0
edt = track.end_datetime(@filter_options)
0
if bdt && (!totals[:begin_datetime] || bdt < totals[:begin_datetime])
0
totals[:begin_datetime] = bdt
end
0
if edt && (!totals[:end_datetime] || edt > totals[:end_datetime])
0
totals[:end_datetime] = edt
end
0
begin_datetime_s = track.begin_datetime_s(glob_begin_options)
0
end_datetime_s = track.end_datetime_s(glob_end_options)
0
duration = track.duration(@filter_options)
0
if duration
0
totals[:duration] += duration
end
table << [
totals[:task_c],
begin_datetime_s,
end_datetime_s,
duration.to_human_s,
task.id_foreign_or_short,
'%s %s' % [track.short_id, track.title(15)],
0
]
end
end
0
table << []
0
totals[:begin_datetime_s] = totals[:begin_datetime] ? totals[:begin_datetime].localtime.strftime(glob_begin_options[:format]) : '---'
0
totals[:end_datetime_s] = totals[:end_datetime] ? totals[:end_datetime].localtime.strftime(glob_end_options[:format]) : '---'
# Add totals to the bottom.
table << [
nil, # track_c
totals[:begin_datetime_s],
totals[:end_datetime_s],
totals[:duration].to_human_s, # duration
'TOTAL', # task
nil, # track
0
]
0
if table_has_rows
0
puts table
else
0
puts 'No tracks found.'
end
end
1
def help
0
puts 'usage: timr log [-s|--from <date_time>] [-e|--to <date_time>]'
0
puts ' or: timr log --sd <date> --ed <date> --st <time> --et <time>'
0
puts ' or: timr log -d|--day <date>'
0
puts ' or: timr log -m|--month <YYYY-MM>'
0
puts ' or: timr log -y|--year <YYYY>'
0
puts ' or: timr log -a|--all'
0
puts ' or: timr log [-h|--help]'
0
puts
0
puts 'Total Filter'
0
puts " -s, --from '<date_time>' From Date/Time. Must be in quotes."
0
puts ' Default: today 00:00:00'
0
puts " -e, --to '<date_time>' To Date/Time. Must be in quotes."
0
puts ' Default: today 23:59:59'
0
puts ' -d, --day <date> A single day from 00:00 to 23:59.'
0
puts ' -m, --month <[YYYY-]MM> A single month from 01 to 31.'
0
puts ' -y, --year [<YYYY>] A single year from 01-01 to 12-31.'
0
puts ' -a, --all Show all Tracks.'
0
puts
0
puts 'Day Time Filter'
0
puts ' --sd, --start-date <date> Start Date'
0
puts ' --ed, --end-date <date> End Date'
0
puts ' --st, --start-time <time> Start Time'
0
puts ' --et, --end-time <time> End Time'
0
puts
0
puts 'If you would like to filter everything between 09:00 and 17:00 on every day'
0
puts 'in the range from Mon 2017-03-06 to Fri 2017-03-10:'
0
puts
0
puts ' timr log --sd 2017-03-06 --ed 2017-03-10 --st 09:00 --et 17:00'
0
puts
0
puts 'Columns'
0
puts ' START Track Start Date'
0
puts ' END Track End Date'
0
puts ' DUR Track Duration'
0
puts ' TASK Task ID'
0
puts ' TRACK Track ID and Title.'
0
puts
0
HelpCommand.print_datetime_help
0
puts
end
end # class LogCommand
end # module Command
end # module Timr
end # module TheFox