Class: TheFox::Timr::Command::ReportCommand

Inherits:
BasicCommand show all
Includes:
Error, Helper
Defined in:
lib/timr/command/report_command.rb

Overview

This Command is very similar to LogCommand. By default it prints all [Tasks](TheFox::Timr::Model::Task) of the current month.

Man page: [timr-report(1)](../../../../man/timr-report.1.html)

Constant Summary collapse

MAN_PATH =

Path to man page.

'man/timr-report.1'

Instance Attribute Summary

Attributes inherited from BasicCommand

#cwd

Instance Method Summary collapse

Methods inherited from BasicCommand

create_command_from_argv, get_command_class_by_name, #shutdown

Constructor Details

#initialize(argv = Array.new) ⇒ ReportCommand

Returns a new instance of ReportCommand.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/timr/command/report_command.rb', line 19

def initialize(argv = Array.new)
  super()
  
  @help_opt = false
  @tasks_opt = false
  @tracks_opt = false
  @from_opt = nil
  @to_opt = nil
  @billed_opt = false
  @unbilled_opt = false
  @format_opt = nil
  @csv_opt = nil
  @force_opt = false
  
  loop_c = 0 # Limit the loop.
  while loop_c < 1024 && argv.length > 0
    loop_c += 1
    arg = argv.shift
    
    case arg
    when '-h', '--help'
      @help_opt = true
    
    when '-d', '--day'
      @from_opt, @to_opt = DateTimeHelper.parse_day_argv(argv)
    when '-m', '--month'
      @from_opt, @to_opt = DateTimeHelper.parse_month_argv(argv)
    when '-y', '--year'
      @from_opt, @to_opt = DateTimeHelper.parse_year_argv(argv)
    
    when '-a', '--all'
      @from_opt = Time.parse('1970-01-01 00:00:00')
      @to_opt   = Time.parse('2099-12-31 23:59:59')
    
    when '--tasks'
      @tasks_opt = true
    when '-t', '--tracks'
      @tracks_opt = true
    
    when '--billed'
      @billed_opt = true
    when '--unbilled'
      @unbilled_opt = true
    
    when '--format'
      @format_opt = argv.shift
    
    when '--csv'
      @csv_opt = argv.shift
      if !@csv_opt
        raise ReportCommandError, 'Invalid value for --csv option.'
      end
    when '-f', '--force' # -f inofficial, maybe used for --file?
      @force_opt = true
    
    else
      raise ReportCommandError, "Unknown argument '#{arg}'. See 'timr report --help'."
    end
  end
  
  today = Date.today
  unless @from_opt
    @from_opt = Time.new(today.year, today.month, 1, 0, 0, 0)
  end
  unless @to_opt
    month_end = Date.new(today.year, today.month, -1)
    @to_opt = Time.new(today.year, today.month, month_end.day, 23, 59, 59)
  end
  
  @billed_resolved_opt = nil
  if @billed_opt || @unbilled_opt
    if @billed_opt
      @billed_resolved_opt = true
    elsif @unbilled_opt
      @billed_resolved_opt = false
    end
  end
  
  @filter_options = {
    :format => '%y-%m-%d %H:%M',
    :from => @from_opt,
    :to => @to_opt,
    :billed => @billed_resolved_opt,
  }
  @csv_filter_options = {
    :format => '%F %T %z',
    :from => @from_opt,
    :to => @to_opt,
  }
  
  # Used by
  #  print_formatted_task_list
  #  print_formatted_track_list
  @format_options = {
    :format => @format_opt,
    :billed => @billed_resolved_opt,
  }
  
  if @csv_opt
    if @csv_opt == '-'
      # OK
    else
      @csv_opt = Pathname.new(@csv_opt).expand_path
    end
  end
end

Instance Method Details

#runObject

See BasicCommand#run.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/timr/command/report_command.rb', line 127

def run
  if @help_opt
    help
    return
  end
  
  @timr = Timr.new(@cwd)
  
  if @csv_opt
    if @tasks_opt
      export_tasks_csv
    elsif @tracks_opt
      export_tracks_csv
    else
      export_tasks_csv
    end
  elsif @format_opt
    if @tasks_opt
      print_formatted_task_list
    elsif @tracks_opt
      print_formatted_track_list
    else
      print_formatted_task_list
    end
  else
    if @tasks_opt
      print_task_table
    elsif @tracks_opt
      print_track_table
    else
      print_task_table
    end
  end
end