summaryrefslogtreecommitdiffstats
path: root/lib/hcl.rb
blob: 5505f5745268a52a47d007980dc768d089c17e7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
require 'yaml'
require 'rexml/document'
require 'net/http'
require 'net/https'

require 'rubygems'
require 'chronic'

require 'hcl/timesheet_resource'
require 'hcl/project'
require 'hcl/task'
require 'hcl/day_entry'

class HCl
  class UnknownCommand < StandardError; end

  def self.conf_file= filename
    @@conf_file = filename
  end

  def self.command *args
    command = args.shift
    hcl = new(@@conf_file).process_args *args
    if command
      if hcl.respond_to? command
        hcl.send command, *args
      else
        raise UnknownCommand, "unrecognized command `#{command}'"
      end
    else
      hcl.show
      return
    end
  end

  def initialize conf_file
    config = YAML::load File.read(conf_file)
    TimesheetResource.configure config
  end

  def self.help
    puts <<-EOM
    Usage:

    hcl show [date]
    hcl tasks
    hcl add <task> <duration> [msg]
    hcl rm [entry_id]
    hcl start <task> [msg]
    hcl stop [msg]

    Examples:

    hcl show 2009-07-15
    hcl show yesterday
    hcl show last tuesday
    EOM
  end
  def help; self.class.help; end

  def process_args *args
    # TODO process command-line args
    self
  end

  def tasks
    Task.all.each do |task|
      # TODO more information and formatting options
      puts "#{task.id}\t#{task}"
    end
  end

  def start *args
    task = Task.find args.shift
    puts "Starting timer for #{task}"
    day_entry = task.start(*args)
    puts "Time is running on #{day_entry}"
  end

  def show *args
    date = args.empty? ? nil : Chronic.parse(args.join(' '))
    total_hours = 0.0
    DayEntry.all(date).each do |day|
      # TODO more information and formatting options
      puts "\t#{as_hours day.hours}\t#{day.project} #{day.notes}"[0..78]
      total_hours = total_hours + day.hours.to_f
    end
    puts "\t" + '-' * 13
    puts "\t#{as_hours total_hours}\ttotal"
  end

  # Convert from decimal to a string of the form HH:MM.
  def as_hours hours
    minutes = hours.to_f * 60.0
    "#{(minutes / 60).to_i}:#{(minutes % 60).to_i}"
  end

  def not_implemented *args
    puts "not yet implemented"
  end

  # TODO implement the following commands
  alias stop not_implemented
  alias add not_implemented
  alias rm not_implemented

end