Commit 57cdffa2 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Let TodoList provide a pretty printer filter for todo numbers.

A step towards elimating knowledge of the todo list in the items itself.
parent 611694ed
...@@ -5,7 +5,7 @@ import re ...@@ -5,7 +5,7 @@ import re
import Config import Config
import Command import Command
from PrettyPrinter import pretty_print, pp_number from PrettyPrinter import pretty_print
from RelativeDate import relative_date_to_date from RelativeDate import relative_date_to_date
class AddCommand(Command.Command): class AddCommand(Command.Command):
...@@ -70,4 +70,4 @@ class AddCommand(Command.Command): ...@@ -70,4 +70,4 @@ class AddCommand(Command.Command):
self.todo = self.todolist.add(self.text) self.todo = self.todolist.add(self.text)
self._postprocess_input_todo() self._postprocess_input_todo()
self.out(pretty_print(self.todo, [pp_number])) self.out(pretty_print(self.todo, [self.todolist.pp_number()]))
import Command import Command
from PrettyPrinter import pretty_print, pp_number from PrettyPrinter import pretty_print
from Utils import convert_todo_number from Utils import convert_todo_number
class AppendCommand(Command.Command): class AppendCommand(Command.Command):
...@@ -15,4 +15,4 @@ class AppendCommand(Command.Command): ...@@ -15,4 +15,4 @@ class AppendCommand(Command.Command):
self.todolist.append(number, text) self.todolist.append(number, text)
self.out(pretty_print(self.todo, [pp_number])) self.out(pretty_print(self.todo, [self.todolist.pp_number]))
...@@ -17,20 +17,20 @@ class DoCommand(Command.Command): ...@@ -17,20 +17,20 @@ class DoCommand(Command.Command):
def _complete_children(self): def _complete_children(self):
children = [t for t in self.todolist.children(self.number) if not t.is_completed()] children = [t for t in self.todolist.children(self.number) if not t.is_completed()]
if children: if children:
self.out("\n".join(pretty_print_list(children, [pp_number]))) self.out("\n".join(pretty_print_list(children, [self.todolist.pp_number()])))
confirmation = self.prompt("Also mark subtasks as done? [n] ") confirmation = self.prompt("Also mark subtasks as done? [n] ")
if re.match('^y(es)?$', confirmation, re.I): if re.match('^y(es)?$', confirmation, re.I):
for child in children: for child in children:
self.todolist.set_todo_completed(child) self.todolist.set_todo_completed(child)
self.out(pretty_print(child, [pp_number])) self.out(pretty_print(child, [self.todolist.pp_number()]))
def _handle_recurrence(self): def _handle_recurrence(self):
if self.todo.has_tag('rec'): if self.todo.has_tag('rec'):
new_todo = advance_recurring_todo(self.todo) new_todo = advance_recurring_todo(self.todo)
self.todolist.add_todo(new_todo) self.todolist.add_todo(new_todo)
self.out(pretty_print(new_todo, [pp_number])) self.out(pretty_print(new_todo, [self.todolist.pp_number()]))
def execute(self): def execute(self):
if self.todo and not self.todo.is_completed(): if self.todo and not self.todo.is_completed():
......
...@@ -37,14 +37,6 @@ def pp_color(p_todo_str, p_todo): ...@@ -37,14 +37,6 @@ def pp_color(p_todo_str, p_todo):
return p_todo_str return p_todo_str
def pp_number(p_todo_str, p_todo):
"""
Inserts the todo number at the start of the string.
Should be passed as a filter in the filter list of pretty_print()
"""
return "%3d %s" % (p_todo.attributes['number'], p_todo_str)
def pretty_print(p_todo, p_filters=[]): def pretty_print(p_todo, p_filters=[]):
""" """
Given a todo item, pretty print it and return a list of formatted strings. Given a todo item, pretty print it and return a list of formatted strings.
...@@ -55,7 +47,7 @@ def pretty_print(p_todo, p_filters=[]): ...@@ -55,7 +47,7 @@ def pretty_print(p_todo, p_filters=[]):
* the todo's text that has to be modified; * the todo's text that has to be modified;
* the todo object itself which allows for obtaining relevant information. * the todo object itself which allows for obtaining relevant information.
Examples are pp_color and pp_number in this file. Example is pp_color in this fle.
""" """
todo_str = str(p_todo) todo_str = str(p_todo)
......
...@@ -170,7 +170,7 @@ class TodoList(object): ...@@ -170,7 +170,7 @@ class TodoList(object):
defined by the end user. Todos is this list should not be modified, defined by the end user. Todos is this list should not be modified,
modifications should occur through this class. modifications should occur through this class.
""" """
return View.View(p_sorter, p_filters, self._todos) return View.View(p_sorter, p_filters, self)
def add_dependency(self, p_number1, p_number2): def add_dependency(self, p_number1, p_number2):
""" Adds a dependency from task 1 to task 2. """ """ Adds a dependency from task 1 to task 2. """
...@@ -293,6 +293,13 @@ class TodoList(object): ...@@ -293,6 +293,13 @@ class TodoList(object):
def number(self, p_todo): def number(self, p_todo):
return p_todo.attributes['number'] # TODO: do the lookup return p_todo.attributes['number'] # TODO: do the lookup
def pp_number(self):
"""
A filter for the pretty printer to append the todo number to the
printed todo.
"""
return lambda p_todo_str, p_todo: "%3d %s" % (self.number(p_todo), p_todo_str)
def __str__(self): def __str__(self):
return '\n'.join(pretty_print_list(self._todos)) return '\n'.join(pretty_print_list(self._todos))
""" A view is a list of todos, sorted and filtered. """ """ A view is a list of todos, sorted and filtered. """
from PrettyPrinter import * from PrettyPrinter import pretty_print_list, pp_color
class View(object): class View(object):
""" """
...@@ -8,8 +8,8 @@ class View(object): ...@@ -8,8 +8,8 @@ class View(object):
file. Also a sorter and a list of filters should be given that is applied file. Also a sorter and a list of filters should be given that is applied
to the list. to the list.
""" """
def __init__(self, p_sorter, p_filters, p_todos): def __init__(self, p_sorter, p_filters, p_todolist):
self._todos = p_todos self._todolist = p_todolist
self._viewdata = [] self._viewdata = []
self._sorter = p_sorter self._sorter = p_sorter
self._filters = p_filters self._filters = p_filters
...@@ -21,14 +21,15 @@ class View(object): ...@@ -21,14 +21,15 @@ class View(object):
Updates the view data. Should be called when the backing todo list Updates the view data. Should be called when the backing todo list
has changed. has changed.
""" """
self._viewdata = self._sorter.sort(self._todos) self._viewdata = self._sorter.sort(self._todolist.todos())
for _filter in self._filters: for _filter in self._filters:
self._viewdata = _filter.filter(self._viewdata) self._viewdata = _filter.filter(self._viewdata)
def pretty_print(self): def pretty_print(self):
""" Pretty prints the view. """ """ Pretty prints the view. """
return '\n'.join(pretty_print_list(self._viewdata, [pp_number, pp_color])) pp_filters = [self._todolist.pp_number(), pp_color];
return '\n'.join(pretty_print_list(self._viewdata, pp_filters))
def __str__(self): def __str__(self):
return '\n'.join(pretty_print_list(self._viewdata)) return '\n'.join(pretty_print_list(self._viewdata))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment