Skip to content

Commit

Permalink
Merge pull request #34 from nicwest/priority-keybinds
Browse files Browse the repository at this point in the history
add key to change priority
  • Loading branch information
AnthonyDiGirolamo authored Dec 22, 2016
2 parents 6f02655 + 5d77082 commit 0d8e503
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
2 changes: 2 additions & 0 deletions todotxt_machine/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
17 changes: 17 additions & 0 deletions todotxt_machine/test/todo_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
30 changes: 22 additions & 8 deletions todotxt_machine/todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[],
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions todotxt_machine/urwid_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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'
Expand Down

0 comments on commit 0d8e503

Please sign in to comment.