Module: Timetrap::CLI
Constant Summary
collapse
- USAGE =
<<-EOF
Timetrap - Simple Time Tracking
Usage: #{File.basename $0} COMMAND [OPTIONS] [ARGS...]
COMMAND can be abbreviated. For example `t in` and `t i` are equivalent.
COMMAND is one of:
* archive - Move entries to a hidden sheet (by default named '_[SHEET]') so
they're out of the way.
usage: t archive [--start DATE] [--end DATE] [SHEET]
-s, --start <date:qs> Include entries that start on this date or later
-e, --end <date:qs> Include entries that start on this date or earlier
-g, --grep <regexp> Include entries where the note matches this regexp.
* backend - Open an sqlite shell to the database.
usage: t backend
* configure - Write out a YAML config file. Print path to config file. The
file may contain ERB.
usage: t configure
Currently supported options are:
round_in_seconds: The duration of time to use for rounding with
the -r flag
database_file: The file path of the sqlite database
append_notes_delimiter: delimiter used when appending notes via
t edit --append
formatter_search_paths: an array of directories to search for user
defined fomatter classes
default_formatter: The format to use when display is invoked without a
`--format` option
default_command: The default command to run when calling t.
auto_checkout: Automatically check out of running entries when
you check in or out
require_note: Prompt for a note if one isn't provided when
checking in
note_editor: Command to launch notes editor or false if no editor use.
If you use a non terminal based editor (e.g. sublime, atom)
please read the notes in the README.
week_start: The day of the week to use as the start of the
week for t week.
* display - Display the current timesheet or a specific. Pass `all' as SHEET
to display all unarchived sheets or `full' to display archived and
unarchived sheets.
usage: t display [--ids] [--start DATE] [--end DATE] [--format FMT] [SHEET | all | full]
-v, --ids Print database ids (for use with edit)
-s, --start <date:qs> Include entries that start on this date or later
-e, --end <date:qs> Include entries that start on this date or earlier
-f, --format <format> The output format. Valid built-in formats are
ical, csv, json, ids, factor, and text (default).
Documentation on defining custom formats can be
found in the README included in this
distribution.
-g, --grep <regexp> Include entries where the note matches this regexp.
* edit - Alter an entry's note, start, or end time. Defaults to the active
entry. Defaults to the last entry to be checked out of if no entry is active.
usage: t edit [--id ID] [--start TIME] [--end TIME] [--append] [NOTES]
-i, --id <id:i> Alter entry with id <id> instead of the running entry
-s, --start <time:qs> Change the start time to <time>
-e, --end <time:qs> Change the end time to <time>
-z, --append Append to the current note instead of replacing it
the delimiter between appended notes is
configurable (see configure)
-c, --clear Allow an empty note, can be used to clear existing notes
-m, --move <sheet> Move to another sheet
* in - Start the timer for the current timesheet.
usage: t in [--at TIME] [NOTES]
-a, --at <time:qs> Use this time instead of now
* kill - Delete a timesheet or an entry.
usage: t kill [--id ID] [TIMESHEET]
-i, --id <id:i> Delete entry with id <id> instead of timesheet
* list - Show the available timesheets.
usage: t list
* now - Show all running entries.
usage: t now
* out - Stop the timer for a timesheet.
usage: t out [--at TIME] [TIMESHEET]
-a, --at <time:qs> Use this time instead of now
* resume - Start the timer for the current time sheet for an entry. Defaults
to the active entry.
usage: t resume [--id ID] [--at TIME]
-i, --id <id:i> Resume entry with id <id> instead of the last entry
-a, --at <time:qs> Use this time instead of now
* sheet - Switch to a timesheet creating it if necessary. When no sheet is
specified list all sheets. The special sheetname '-' will switch to the
last active sheet.
usage: t sheet [TIMESHEET]
* today - Shortcut for display with start date as the current day
usage: t today [--ids] [--format FMT] [SHEET | all]
* yesterday - Shortcut for display with start and end dates as the day before the current day
usage: t yesterday [--ids] [--format FMT] [SHEET | all]
* week - Shortcut for display with start date set to a day of this week.
The default start of the week is Monday.
.
usage: t week [--ids] [--end DATE] [--format FMT] [SHEET | all]
* month - Shortcut for display with start date set to the beginning of either
this month or a specified month.
usage: t month [--ids] [--start MONTH] [--format FMT] [SHEET | all]
OTHER OPTIONS
-h, --help Display this help.
-r, --round Round output to 15 minute start and end times.
-y, --yes Noninteractive, assume yes as answer to all prompts.
--debug Display stack traces for errors.
EXAMPLES
# create the "MyTimesheet" timesheet
$ t sheet MyTimesheet
# check in 5 minutes ago with a note
$ t in --at '5 minutes ago' doing some stuff
# check out
$ t out
# view current timesheet
$ t display
Submit bugs and feature requests to http://github.com/samg/timetrap/issues
EOF
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helpers
format_date, format_date_if_new, format_seconds, format_time, format_total, same_day?, selected_entries, sheet_name_from_string
auto_load, load_auto_sheet, load_formatter
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
4
5
6
|
# File 'lib/timetrap/cli.rb', line 4
def args
@args
end
|
Instance Method Details
#archive ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/timetrap/cli.rb', line 203
def archive
ee = selected_entries
if ask_user "Archive #{ee.count} entries? "
ee.each do |e|
next unless e.end
e.update :sheet => "_#{e.sheet}"
end
else
warn "archive aborted!"
end
end
|
#backend ⇒ Object
275
276
277
|
# File 'lib/timetrap/cli.rb', line 275
def backend
exec "sqlite3 #{DB_NAME}"
end
|
#commands ⇒ Object
157
158
159
|
# File 'lib/timetrap/cli.rb', line 157
def commands
Timetrap::CLI::USAGE.scan(/\* \w+/).map{|s| s.gsub(/\* /, '')}
end
|
215
216
217
218
|
# File 'lib/timetrap/cli.rb', line 215
def configure
Config.configure!
puts "Config file is at #{Config::PATH.inspect}"
end
|
#deprecated_commands ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/timetrap/cli.rb', line 161
def deprecated_commands
{
'switch' => 'sheet',
'running' => 'now',
'format' => 'display'
}
end
|
#display ⇒ Object
375
376
377
378
379
380
381
382
|
# File 'lib/timetrap/cli.rb', line 375
def display
entries = selected_entries
if entries == []
warn "No entries were selected to display."
else
puts format_entries(entries)
end
end
|
#edit ⇒ Object
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/timetrap/cli.rb', line 220
def edit
entry = case
when args['-i']
warn "Editing entry with id #{args['-i'].inspect}"
Entry[args['-i']]
when Timer.active_entry
warn "Editing running entry"
Timer.active_entry
when Timer.last_checkout
warn "Editing last entry you checked out of"
Timer.last_checkout
end
unless entry
warn "Can't find entry"
return
end
warn ""
entry.update :start => args['-s'] if args['-s'] =~ /.+/
entry.update :end => args['-e'] if args['-e'] =~ /.+/
if args['-m'] =~ /.+/
if entry == Timer.active_entry
Timer.current_sheet = args['-m']
end
entry.update :sheet => args['-m']
end
if Config['note_editor']
if args['-c']
entry.update :note => ''
elsif args['-z']
note = [entry.note, get_note_from_external_editor].join(Config['append_notes_delimiter'])
entry.update :note => note
elsif editing_a_note?
entry.update :note => get_note_from_external_editor(entry.note)
end
else
if args['-c']
entry.update :note => ''
elsif unused_args =~ /.+/
note = unused_args
if args['-z']
note = [entry.note, note].join(Config['append_notes_delimiter'])
end
entry.update :note => note
end
end
puts format_entries(entry)
end
|
#handle_invalid_command(command) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/timetrap/cli.rb', line 186
def handle_invalid_command(command)
if !command
puts USAGE
elsif mapping = deprecated_commands.detect{|(k,v)| k =~ %r|^#{command}|}
deprecated, current = *mapping
warn "The #{deprecated.inspect} command is deprecated in favor of #{current.inspect}. Sorry for the inconvenience."
send current
else
warn "Invalid command: #{command.inspect}"
end
end
|
#in ⇒ Object
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
# File 'lib/timetrap/cli.rb', line 279
def in
if Config['auto_checkout']
Timer.stop_all(args['-a']).each do |checked_out_of|
warn "Checked out of sheet #{checked_out_of.sheet.inspect}."
end
end
note = unused_args
if Config['require_note'] && !Timer.running? && unused_args.empty?
if Config['note_editor']
note = get_note_from_external_editor
else
$stderr.print("Please enter a note for this entry:\n> ")
note = $stdin.gets.strip
end
end
Timer.start note, args['-a']
warn "Checked into sheet #{Timer.current_sheet.inspect}."
end
|
#invoke ⇒ Object
149
150
151
152
153
154
155
|
# File 'lib/timetrap/cli.rb', line 149
def invoke
args['-h'] ? puts(USAGE) : invoke_command_if_valid
rescue StandardError, LoadError => e
raise e if args['--debug']
warn e.message
exit 1 unless defined? TEST_MODE
end
|
#invoke_command_if_valid ⇒ Object
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/timetrap/cli.rb', line 169
def invoke_command_if_valid
if args.unused.empty? && Timetrap::Config['default_command']
self.args = Getopt::Declare.new(USAGE.dup, Timetrap::Config['default_command'])
end
command = args.unused.shift
set_global_options
case (valid = commands.select{|name| name =~ %r|^#{command}|}).size
when 1 then send valid[0]
else
handle_invalid_command(command)
end
end
|
#kill ⇒ Object
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
# File 'lib/timetrap/cli.rb', line 351
def kill
if e = Entry[args['-i']]
out = "are you sure you want to delete entry #{e.id}? "
out << "(#{e.note}) " if e.note.to_s =~ /.+/
if ask_user out
e.destroy
warn "it's dead"
else
warn "will not kill"
end
elsif (sheets = Entry.map{|e| e.sheet }.uniq).include?(sheet = unused_args)
victims = Entry.filter(:sheet => sheet).count
if ask_user "are you sure you want to delete #{victims} entries on sheet #{sheet.inspect}? "
Entry.filter(:sheet => sheet).destroy
warn "killed #{victims} entries"
else
warn "will not kill"
end
else
victim = args['-i'] ? args['-i'].to_s.inspect : sheet.inspect
warn ["can't find #{victim} to kill", 'sheets:', *sheets].join("\n")
end
end
|
#list ⇒ Object
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# File 'lib/timetrap/cli.rb', line 409
def list
sheets = ([Timer.current_sheet] | Entry.sheets).map do |sheet|
sheet_atts = {:total => 0, :running => 0, :today => 0}
entries = Timetrap::Entry.filter(:sheet => sheet)
if entries.empty?
sheet_atts.merge(:name => sheet)
else
entries.inject(sheet_atts) do |m, e|
e_end = e.end_or_now
m[:name] ||= sheet
m[:total] += (e_end.to_i - e.start.to_i)
m[:running] += (e_end.to_i - e.start.to_i) unless e.end
m[:today] += (e_end.to_i - e.start.to_i) if same_day?(Time.now, e.start)
m
end
end
end.sort_by{|sheet| sheet[:name].downcase}
width = sheets.sort_by{|h|h[:name].length }.last[:name].length + 4
width = 10 if width < 10
puts " %-#{width}s%-12s%-12s%s" % ["Timesheet", "Running", "Today", "Total Time"]
sheets.each do |sheet|
star = sheet[:name] == Timer.current_sheet ? '*' : sheet[:name] == Timer.last_sheet ? '-' : ' '
puts "#{star}%-#{width}s%-12s%-12s%s" % [
sheet[:running],
sheet[:today],
sheet[:total]
].map(&method(:format_seconds)).unshift(sheet[:name])
end
end
|
#month ⇒ Object
475
476
477
478
479
480
481
482
483
484
485
486
487
|
# File 'lib/timetrap/cli.rb', line 475
def month
d = Chronic.parse( args['-s'] || Date.today )
beginning_of_month = Date.new( d.year, d.month )
end_of_month = if d.month == 12 Date.new( d.year + 1, 1) - 1
else
Date.new( d.year, d.month+1 ) - 1
end
args['-s'] = beginning_of_month.to_s
args['-e'] = end_of_month.to_s
display
end
|
#now ⇒ Object
439
440
441
442
443
444
445
446
447
448
449
450
|
# File 'lib/timetrap/cli.rb', line 439
def now
if !Timer.running?
warn "*#{Timer.current_sheet}: not running"
end
Timer.running_entries.each do |entry|
current = entry.sheet == Timer.current_sheet
out = current ? '*' : ' '
out << "#{entry.sheet}: #{format_duration(entry.duration)}".gsub(/ /, ' ')
out << " (#{entry.note})" if entry.note =~ /.+/
puts out
end
end
|
#out ⇒ Object
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
# File 'lib/timetrap/cli.rb', line 329
def out
if Config['auto_checkout']
stopped = Timer.stop_all(args['-a']).each do |checked_out_of|
note = Timer.last_checkout.note
entry = note_blank?(note) ? Timer.last_checkout.id : note.inspect
warn "Checked out of entry #{entry} in sheet #{checked_out_of.sheet.inspect}."
end
if stopped.empty?
warn "No running entries to stop."
end
else
sheet = sheet_name_from_string(unused_args)
if Timer.stop sheet, args['-a']
note = Timer.last_checkout.note
entry = note_blank?(note) ? Timer.last_checkout.id : note.inspect
warn "Checked out of entry #{entry} in sheet #{sheet.inspect}."
else
warn "No running entry on sheet #{sheet.inspect}."
end
end
end
|
#parse(arguments) ⇒ Object
145
146
147
|
# File 'lib/timetrap/cli.rb', line 145
def parse arguments
args.parse arguments
end
|
#resume ⇒ Object
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
# File 'lib/timetrap/cli.rb', line 300
def resume
entry = case
when args['-i']
entry = Entry[args['-i']]
unless entry
warn "No such entry (id #{args['-i'].inspect})!"
return
end
warn "Resuming entry with id #{args['-i'].inspect} (#{entry.note})"
entry
else
last_entry = Timer.entries(Timer.current_sheet).order(:id).last
last_entry ||= Timer.entries("_#{Timer.current_sheet}").order(:id).last
warn "No entry yet on this sheet yet. Started a new entry." unless last_entry
note = (last_entry ? last_entry.note : nil)
warn "Resuming #{note.inspect} from entry ##{last_entry.id}" if note
last_entry
end
unless entry
warn "Can't find entry"
return
end
self.unused_args = entry.note || unused_args
self.in
end
|
#set_global_options ⇒ Object
currently just sets whether output should be rounded to 15 min intervals
199
200
201
|
# File 'lib/timetrap/cli.rb', line 199
def set_global_options
Timetrap::Entry.round = true if args['-r']
end
|
#sheet ⇒ Object
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
# File 'lib/timetrap/cli.rb', line 384
def sheet
sheet = unused_args
case sheet
when nil, ''
list
return
when '-'
if Timer.last_sheet
sheet = Timer.last_sheet
else
warn 'LAST_SHEET is not set'
return
end
end
Timer.current_sheet = sheet
if Timer.last_sheet == sheet
warn "Already on sheet #{sheet.inspect}"
elsif Entry.sheets.include?(sheet)
warn "Switching to sheet #{sheet.inspect}"
else
warn "Switching to sheet #{sheet.inspect} (new sheet)"
end
end
|
#today ⇒ Object
452
453
454
455
|
# File 'lib/timetrap/cli.rb', line 452
def today
args['-s'] = Date.today.to_s
display
end
|
#valid_command(command) ⇒ Object
182
183
184
|
# File 'lib/timetrap/cli.rb', line 182
def valid_command(command)
return commands.include?(command)
end
|
#week ⇒ Object
464
465
466
467
468
469
470
471
472
473
|
# File 'lib/timetrap/cli.rb', line 464
def week
d = Chronic.parse( args['-s'] || Date.today )
today = Date.new( d.year, d.month, d.day )
end_of_week = today + 6
last_week_start = Date.parse(Chronic.parse('last '.concat(Config['week_start']).to_s, :now => today).to_s)
args['-s'] = today.wday == Date.parse(Config['week_start']).wday ? today.to_s : last_week_start.to_s
args['-e'] = end_of_week.to_s
display
end
|
#yesterday ⇒ Object
457
458
459
460
461
462
|
# File 'lib/timetrap/cli.rb', line 457
def yesterday
yesterday = (Date.today - 1).to_s
args['-s'] = yesterday
args['-e'] = yesterday
display
end
|