lib/timetrap/cli.rb in timetrap-1.10.0 vs lib/timetrap/cli.rb in timetrap-1.11.0

- old
+ new

@@ -40,10 +40,13 @@ 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. * 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] @@ -72,11 +75,11 @@ 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> Alter entry with id <id> instead of the running entry + -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. @@ -237,19 +240,28 @@ Timer.current_sheet = args['-m'] end entry.update :sheet => args['-m'] end - # update notes - if unused_args =~ /.+/ - note = unused_args + if Config['note_editor'] if args['-z'] - note = [entry.note, note].join(Config['append_notes_delimiter']) + note = [entry.note, get_note_from_external_editor].join(Config['append_notes_delimiter']) + entry.update :note => note + elsif args.size == 0 # no arguments supplied + entry.update :note => get_note_from_external_editor(entry.note) end - entry.update :note => note + else + if 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 def backend exec "sqlite3 #{DB_NAME}" @@ -260,16 +272,21 @@ 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? - $stderr.print("Please enter a note for this entry:\n> ") - self.unused_args = $stdin.gets + 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 unused_args, args['-a'] + Timer.start note, args['-a'] warn "Checked into sheet #{Timer.current_sheet.inspect}." end def resume entry = case @@ -449,9 +466,24 @@ def ask_user question return true if args['-y'] $stderr.print question $stdin.gets =~ /\Aye?s?\Z/i + end + + def get_note_from_external_editor(contents = "") + file = Tempfile.new('get_note') + unless contents.empty? + file.open + file.write(contents) + file.close + end + + system("#{Config['note_editor']} #{file.path}") + file.open.read + ensure + file.close + file.unlink end extend Helpers::AutoLoad def format_entries(entries) load_formatter(args['-f'] || Config['default_formatter']).new(Array(entries)).output