From 5d7708293a36d6b3ee1a5f41bfefd7f1e2d2513b Mon Sep 17 00:00:00 2001 From: Nic West Date: Wed, 27 Jan 2016 00:51:01 +0000 Subject: [PATCH] add key to change priority adds p and P keybinds to increment and decrement priorities implements #32 --- todotxt_machine/keys.py | 2 ++ todotxt_machine/test/todo_test.py | 17 +++++++++++++++++ todotxt_machine/todo.py | 30 ++++++++++++++++++++++-------- todotxt_machine/urwid_ui.py | 22 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/todotxt_machine/keys.py b/todotxt_machine/keys.py index b5c1cab..393792e 100644 --- a/todotxt_machine/keys.py +++ b/todotxt_machine/keys.py @@ -36,6 +36,8 @@ def fillWithDefault(self): self.key_bindings['append'] = ['n'] self.key_bindings['insert-after'] = ['o'] self.key_bindings['insert-before'] = ['O'] + self.key_bindings['priority-up'] = ['p'] + self.key_bindings['priority-down'] = ['P'] self.key_bindings['save-item'] = ['enter'] self.key_bindings['edit'] = ['enter', 'A', 'e'] self.key_bindings['delete'] = ['D'] diff --git a/todotxt_machine/test/todo_test.py b/todotxt_machine/test/todo_test.py index b9295e6..2e2e070 100644 --- a/todotxt_machine/test/todo_test.py +++ b/todotxt_machine/test/todo_test.py @@ -407,3 +407,20 @@ def test_todos_swap(todos): assert [todo.raw_index for todo in todos.todo_items] == [4, 0, 3, 2, 1] todos.swap(4, 5) assert [todo.raw_index for todo in todos.todo_items] == [1, 0, 3, 2, 4] + + +def test_change_priority(todos): + todos[0].change_priority('F') + assert todos[0].raw == "(F) Thank Mom for the dinner @phone" + todos[1].change_priority('') + assert todos[1].raw == "Schedule Goodwill pickup +GarageSale @phone" + todos[2].change_priority('C') + assert todos[2].raw == "(C) Unpack the guest bedroom +Unpacking due:2013-10-20" + todos[3].change_priority('A') + assert todos[3].raw == "(A) 2013-10-19 Post signs around the neighborhood +GarageSale" + todos[4].change_priority('B') + assert todos[4].raw == "x 2013-10-01 (B) @GroceryStore Eskimo pies" + todos[4].change_priority('C') + assert todos[4].raw == "x 2013-10-01 (C) @GroceryStore Eskimo pies" + todos[4].change_priority('') + assert todos[4].raw == "x 2013-10-01 @GroceryStore Eskimo pies" diff --git a/todotxt_machine/todo.py b/todotxt_machine/todo.py index 82dff5b..a3d2d22 100644 --- a/todotxt_machine/todo.py +++ b/todotxt_machine/todo.py @@ -9,6 +9,7 @@ class Todo: """Single Todo item""" + _priority_regex = re.compile(r'\(([A-Z])\) ') def __init__(self, item, index, colored="", priority="", contexts=[], projects=[], @@ -64,14 +65,14 @@ def highlight(self, line="", show_due_date=True, show_contexts=True, show_projec if words_to_be_highlighted: color_list = re.split("(" + "|".join([re.escape(w) for w in words_to_be_highlighted]) + ")", self.raw) for index, w in enumerate(color_list): - if w in self.contexts: - color_list[index] = ('context', w) if show_contexts else '' - elif w in self.projects: - color_list[index] = ('project', w) if show_projects else '' - elif w == "due:"+self.due_date: - color_list[index] = ('due_date', w) if show_due_date else '' - elif w == self.creation_date: - color_list[index] = ('creation_date', w) + if w in self.contexts: + color_list[index] = ('context', w) if show_contexts else '' + elif w in self.projects: + color_list[index] = ('project', w) if show_projects else '' + elif w == "due:"+self.due_date: + color_list[index] = ('due_date', w) if show_due_date else '' + elif w == self.creation_date: + color_list[index] = ('creation_date', w) if self.priority and self.priority in "ABCDEF": color_list = ("priority_{0}".format(self.priority.lower()), color_list) @@ -90,6 +91,19 @@ def highlight_search_matches(self, line=""): color_list[index] = ('search_match', w) return color_list + def change_priority(self, new_priority): + self.priority = new_priority + if new_priority: + new_priority = '({}) '.format(new_priority) + + if re.search(self._priority_regex, self.raw): + self.raw = re.sub(self._priority_regex, '{}'.format(new_priority), self.raw) + elif re.search(r'^x \d{4}-\d{2}-\d{2}', self.raw): + self.raw = re.sub(r'^(x \d{4}-\d{2}-\d{2}) ', r'\1 {}'.format(new_priority), self.raw) + else: + self.raw = '{}{}'.format(new_priority, self.raw) + self.update(self.raw) + def is_complete(self): if self.raw[0:2] == "x ": return True diff --git a/todotxt_machine/urwid_ui.py b/todotxt_machine/urwid_ui.py index 0865c81..c0edb9c 100644 --- a/todotxt_machine/urwid_ui.py +++ b/todotxt_machine/urwid_ui.py @@ -536,6 +536,12 @@ def keystroke(self, input): elif self.key_bindings.is_binded_to(input, 'insert-after'): self.add_new_todo(position='insert_after') + elif self.key_bindings.is_binded_to(input, 'priority-up'): + self.adjust_priority(focus, up=True) + + elif self.key_bindings.is_binded_to(input, 'priority-down'): + self.adjust_priority(focus, up=False) + # Save current file elif self.key_bindings.is_binded_to(input, 'save'): self.save_todos() @@ -544,6 +550,22 @@ def keystroke(self, input): elif self.key_bindings.is_binded_to(input, 'reload'): self.reload_todos_from_file() + def adjust_priority(self, focus, up=True): + priorities = ['', 'A', 'B', 'C', 'D', 'E', 'F'] + if up: + new_priority = priorities.index(focus.todo.priority) + 1 + else: + new_priority = priorities.index(focus.todo.priority) - 1 + + if new_priority < 0: + focus.todo.change_priority(priorities[len(priorities) - 1]) + elif new_priority < len(priorities): + focus.todo.change_priority(priorities[new_priority]) + else: + focus.todo.change_priority(priorities[0]) + + focus.update_todo() + def add_new_todo(self, position=False): if len(self.listbox.body) == 0: position = 'append'