Class: TimeTrackr::CLI

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

Constant Summary collapse

DEFAULTS =
{
  'verbose' => false,
  'single_task' => true,
  '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

Returns a new instance of CLI.



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

def initialize(config)
  @config = config
  @trackr = TimeTrackr::Database.new(config)
end

Class Method Details

.run(args) ⇒ Object

static method to get config file and run the tracker



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

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 ‘args’ is everything after the command



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
# File 'lib/timetrackr/cli.rb', line 47

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 @config['verbose']
    else
      puts "Started task '#{task}'" if @config['verbose']
    end
    @trackr.start(task, notes)

  when 'stop','out','kill','k'
    get_tasks(args).each do |task|
      @trackr.stop(task)
      puts "Stopped task '#{task}'" if @config['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 @config['verbose']

  when 'time','status',nil
    tasks = get_tasks(args)
    puts create_log(tasks,'t')

  when 'log'
    group = args.shift[1] if ['-d','-t','-s'].include?(args[0])

    tasks = get_tasks(args)
    puts create_log(tasks,group)

  when 'clear','delete','del'
    get_tasks(args).each do |task|
      @trackr.clear(task)
      puts "Task '#{task}' cleared" if @config['verbose']
    end

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

  when 'mark','note','n'
    notes = args.join(' ')
    @trackr.current.each do |t|
      @trackr.stop(t)
      @trackr.start(t, notes)
    end
    puts "Annotated task(s) '#{@trackr.current.join(' ')}'" if @config['verbose']

  when 'help'
    show_help

  when 'config'
    puts @config.to_yaml

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

  @trackr.close
end