From 6bc01616ad2fd338c94b80a2cd517998fd7c121b Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 24 Feb 2012 17:39:57 +0000 Subject: Choose entry automatically when possible --- scripts/journal-note | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/scripts/journal-note b/scripts/journal-note index d6de160..ebbb57e 100755 --- a/scripts/journal-note +++ b/scripts/journal-note @@ -96,9 +96,14 @@ class App(object): f.close() self.gedit_file(name) - def list_entries(self, args, opts): + def find_drafts(self, opts): drafts = self.drafts(opts) for name in os.listdir(drafts): + if name.endswith('.mdwn'): + yield name[:-len('.mdwn')], name + + def list_entries(self, args, opts): + for draft_id, name in self.find_drafts(opts): if name.endswith('.mdwn'): f = open(os.path.join(drafts, name)) for line in f: @@ -110,12 +115,24 @@ class App(object): else: title = 'unknown title' f.close() - print name[:-len('.mdwn')], title + print draft_id, title + + def choose_entry(self, args, opts): + if len(args) == 0: + drafts = list(self.find_drafts(opts)) + if len(drafts) == 1: + draft_id, filename = drafts[0] + return draft_id + else: + raise AppException('Cannot choose entry automatically') + elif len(args) == 1: + return args[0] + elif len(args) > 1: + raise AppException('Must give only one entry') def edit_entry(self, args, opts): - if not args: - raise AppException('Usage: journal-note edit ID') - pathname = self.draftname(opts, args[0]) + draft_id = self.choose_entry(args, opts) + pathname = self.draftname(opts, draft_id) if not os.path.exists(pathname): raise AppException('draft %s does not exist' % args[0]) self.gedit_file(pathname) @@ -139,9 +156,8 @@ class App(object): os.remove(pathname) def finish_entry(self, args, opts): - if not args: - raise AppException('Usage: journal-note finish ID') - draft = self.draftname(opts, args[0]) + draft_id = self.choose_entry(args, opts) + draft = self.draftname(opts, draft_id) if not os.path.exists(draft): raise AppException('draft %s does not exist' % args[0]) basename = time.strftime('%Y-%m-%d-%H:%M.mdwn') -- cgit v1.2.1