Class: TimeTrackr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/timetrackr/cli.rb

Constant Summary collapse

DEFAULTS =
{
  'backend' => 'yaml',
  'verbose' => false,
  'single_task' => false,
  'path' => File.join(ENV['HOME'],'.timetrackr.db'),
  'relative_format' => "%2<hours>dh %2<minutes>dm %2<seconds>ds",
  'absolute_time' => "%H:%M",
  'absolute_day' => "%Y-%m-%d"
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ CLI



39
40
41
42
43
# File 'lib/timetrackr/cli.rb', line 39

def initialize(config)
  @config = config
  @verbose = config['verbose']
  @trackr = TimeTrackr::Database.create(config['backend'], config)
end

Class Method Details

.run(args) ⇒ Object

static method to get config file and run the tracker



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/timetrackr/cli.rb', line 16

def self.run(args)
  config = {}
  config_file = File.join(ENV['HOME'],'.timetrackrrc')
  if File.exist?(config_file)
    require 'yaml'
    config = YAML.load_file(config_file)
  end

  # global options
  while (cmd = args.shift) && cmd.start_with?('-')
    if ['-v','--verbose'].include? cmd
      config['verbose'] = true
    end
    if ['-h','--help'].include? cmd
      cmd = 'help'
    end
  end
  config = DEFAULTS.merge(config || {})

  cli = TimeTrackr::CLI.new(config)
  cli.run(cmd, args)
end

Instance Method Details

#run(cmd, args) ⇒ Object

run a command on the tracker



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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/timetrackr/cli.rb', line 48

def run(cmd,args)
  case cmd
  when 'start','in','s'
    task = args.shift
    notes = args.join(' ')
    # switch tasks if config says so
    if @config['single_task'] && @trackr.current != task
      @trackr.current.each { |t|
        @trackr.stop(t) unless t == task
      }
      puts "Switched to task '#{task}'" if @verbose
    else
      puts "Started task '#{task}'" if @verbose
    end
    @trackr.start(task, notes)

  when 'stop','out','kill','k'
    if args[0] == 'all' || args[0].nil?
      tasks = @trackr.tasks
    else
      tasks = args
    end
    tasks.each do |task|
      @trackr.stop(task)
      puts "Stopped task '#{task}'" if @verbose
    end

  when 'switch','sw'
    task = args.shift
    notes = args.join(' ')
    @trackr.current.each do |t|
      @trackr.stop(t) unless t == task
    end
    @trackr.start(task, notes)
    puts "Switched to task '#{task}'" if @verbose

  when 'time','status',nil
    task = args.shift
    if task && @trackr.tasks.include?(task)
      tasks = [*task]
    else
      tasks = @trackr.tasks.each
    end
    tasks.each do |task|
      total = @trackr.history(task).reduce(0){ |t, period|
        t = t + period.length
      }
      name = @trackr.current.include?(task) ? task+' *' : task
      puts name.ljust(15) << format_time(total,@config['relative_format'])
    end

  when 'log'
    if args[0] == 'all' || args[0].nil?
      tasks = @trackr.tasks
    else
      tasks = args
    end
    table = []
    periods = tasks.each.collect{ |t| @trackr.history(t) }.flatten
    lastday = nil
    table << periods.sort{|x,y| x.start <=> y.start}.collect{ |period|
      currday = period.start.strftime(@config['absolute_day'])
      day = (currday == lastday) ? ' ' : currday
      lastday = currday
      name = period.current? ? period.task+' *' : period.task
      start = period.start.strftime(@config['absolute_time'])
      stop = period.current? ? ' ' : period.stop.strftime(@config['absolute_time'])
      length = format_time(period.length, @config['relative_format'])
      notes = period.notes
      "#{day.ljust(12)} #{name.ljust(15)} #{start} - #{stop.ljust(5)}  #{length}  #{notes}"
    }
    puts table


  when 'clear','delete','del'
    tasks = args
    tasks = @trackr.tasks if task == 'all'
    tasks.each do |task|
      @trackr.clear(task)
      puts "Task '#{task}' cleared" if @verbose
    end

  when 'rename','mv'
    from = args.shift
    to = args.shift
    @trackr.rename(from,to)
    puts "Renamed '#{from}' to '#{to}'" if @verbose

  when 'help'
    show_help

  else
    puts "'#{cmd}' is not a valid command"
    show_help
  end

  @trackr.close
end