Commit bcf68647 authored by Bram Schoenmakers's avatar Bram Schoenmakers

View can use any printer, except when pretty_print() is called.

Moreover, make print_todo of PrettyPrinter have the same signature as
the Printer class. Clients must use add_filter to specify any additional
filters when printing.

The Command class holds a PrettyPrinter instance (rather than a generic
printer). This means that add_filter is called, all future invocations
of print_todo will apply these filters. This is not always desired, so
in those cases a subcommand should instantiate its own printer (this
happens for instance at the DoCommand).
parent 06a7f892
......@@ -93,9 +93,8 @@ class AddCommand(Command):
self.todo = self.todolist.add(self.text)
self._postprocess_input_todo()
self.out(
self.printer.print_todo(self.todo,
[PrettyPrinterNumbers(self.todolist)]))
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(self.printer.print_todo(self.todo))
else:
self.error(self.usage())
......
......@@ -38,8 +38,8 @@ class AppendCommand(Command):
todo = self.todolist.todo(number)
self.todolist.append(todo, text)
self.out(self.printer.print_todo(todo,
[PrettyPrinterNumbers(self.todolist)]))
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(self.printer.print_todo(todo))
else:
self.error(self.usage())
except InvalidCommandArgument:
......
......@@ -17,6 +17,7 @@
import re
from topydo.lib.Command import Command
from topydo.lib.PrettyPrinter import PrettyPrinter
from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
from topydo.lib.TodoListBase import InvalidTodoException
......@@ -72,8 +73,9 @@ class DCommand(Command):
)
def _print_list(self, p_todos):
filters = [PrettyPrinterNumbers(self.todolist)]
self.out("\n".join(self.printer.print_list(p_todos, filters)))
printer = PrettyPrinter()
printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out("\n".join(printer.print_list(p_todos)))
def prompt_text(self):
return "Yes or no? [y/N] "
......
......@@ -97,7 +97,8 @@ class DepCommand(Command):
if todos:
sorter = Sorter(config().sort_string())
instance_filter = Filter.InstanceFilter(todos)
view = View(sorter, [instance_filter], self.todolist)
view = View(sorter, [instance_filter], self.todolist,
self.printer)
self.out(view.pretty_print())
except InvalidTodoException:
self.error("Invalid todo number given.")
......
......@@ -17,6 +17,7 @@
from datetime import date
from topydo.lib.DCommand import DCommand
from topydo.lib.PrettyPrinter import PrettyPrinter
from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
from topydo.lib.Recurrence import advance_recurring_todo, strict_advance_recurring_todo
from topydo.lib.Utils import date_string_to_date
......@@ -56,8 +57,10 @@ class DoCommand(DCommand):
self.completion_date)
self.todolist.add_todo(new_todo)
self.out(self.printer.print_todo(new_todo,
[PrettyPrinterNumbers(self.todolist)]))
printer = PrettyPrinter()
printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(printer.print_todo(new_todo))
def prompt_text(self):
return "Also mark subtasks as done? [y/N] "
......@@ -78,7 +81,9 @@ class DoCommand(DCommand):
""" Actions specific to this command. """
self._handle_recurrence(p_todo)
self.execute_specific_core(p_todo)
self.out(self.prefix() + self.printer.print_todo(p_todo))
printer = PrettyPrinter()
self.out(self.prefix() + printer.print_todo(p_todo))
def execute_specific_core(self, p_todo):
"""
......
......@@ -21,6 +21,7 @@ from topydo.lib.Config import config
from topydo.lib import Filter
from topydo.lib.PrettyPrinterFilter import PrettyPrinterIndentFilter
from topydo.lib.Sorter import Sorter
from topydo.lib.View import View
class ListCommand(Command):
def __init__(self, p_args, p_todolist,
......@@ -74,18 +75,23 @@ class ListCommand(Command):
return filters
def execute(self):
if not super(ListCommand, self).execute():
return False
self._process_flags()
def _print(self):
""" Prints the todos. """
sorter = Sorter(self.sort_expression)
filters = self._filters()
view = View(sorter, filters, self.todolist)
indent = config().list_indent()
pp_filters = [PrettyPrinterIndentFilter(indent)]
self.out(self.todolist.view(sorter, filters).pretty_print(pp_filters))
self.out(view.pretty_print([PrettyPrinterIndentFilter(indent)]))
def execute(self):
if not super(ListCommand, self).execute():
return False
self._process_flags()
self._print()
def usage(self):
return """Synopsis: ls [-x] [-s <sort_expression>] [expression]"""
......
......@@ -75,8 +75,8 @@ class PostponeCommand(Command):
todo.set_tag(config().tag_due(), new_due.isoformat())
self.todolist.set_dirty()
self.out(self.printer.print_todo(todo,
[PrettyPrinterNumbers(self.todolist)]))
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(self.printer.print_todo(todo))
else:
self.error("Invalid date pattern given.")
......
......@@ -40,28 +40,26 @@ class PrettyPrinter(Printer):
add colors, indentation, etc. These filters are found in the
PrettyPrinterFilter module.
"""
def print_todo(self, p_todo, p_filters=None):
def __init__(self):
"""
Given a todo item, pretty print it.
Constructor.
"""
super(PrettyPrinter, self).__init__()
self.filters = []
def add_filter(self, p_filter):
"""
Adds a filter to be applied when calling print_todo.
p_filters is a list of PrettyPrinterFilter objects (typically
subclasses of it, see PrettyPrinterFilter module)
p_filter is an instance of a PrettyPrinterFilter.
"""
p_filters = p_filters or []
self.filters.append(p_filter)
def print_todo(self, p_todo):
""" Given a todo item, pretty print it. """
todo_str = str(p_todo)
for ppf in p_filters:
for ppf in self.filters:
todo_str = ppf.filter(todo_str, p_todo)
return todo_str
def print_list(self, p_todos, p_filters=None):
"""
Given a list of todo items, pretty print it and return a list of
formatted strings.
This override also passes on filters.
"""
p_filters = p_filters or []
return [self.print_todo(todo, p_filters) for todo in p_todos]
......@@ -60,8 +60,8 @@ class TagCommand(Command):
self.value = ""
def _print(self):
filters = [PrettyPrinterNumbers(self.todolist)]
self.out(self.printer.print_todo(self.todo, filters))
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
self.out(self.printer.print_todo(self.todo))
def _choose(self):
"""
......
......@@ -27,13 +27,18 @@ class View(object):
A view is instantiated by a todo list, usually obtained from a todo.txt
file. Also a sorter and a list of filters should be given that is applied
to the list.
A printer can be passed, but it won't be used when pretty_print() is
called, since it will instantiate its own pretty printer instance.
"""
def __init__(self, p_sorter, p_filters, p_todolist):
def __init__(self, p_sorter, p_filters, p_todolist,
p_printer=PrettyPrinter()):
self._todolist = p_todolist
self._viewdata = []
self._sorter = p_sorter
self._filters = p_filters
self._printer = PrettyPrinter()
self._printer = p_printer
self.update()
......@@ -51,13 +56,15 @@ class View(object):
""" Pretty prints the view. """
p_pp_filters = p_pp_filters or []
pp_filters = [
PrettyPrinterNumbers(self._todolist),
PrettyPrinterColorFilter()
]
pp_filters += p_pp_filters
# since we're using filters, always use PrettyPrinter
printer = PrettyPrinter()
printer.add_filter(PrettyPrinterNumbers(self._todolist))
printer.add_filter(PrettyPrinterColorFilter())
for ppf in p_pp_filters:
printer.add_filter(ppf)
return '\n'.join(self._printer.print_list(self._viewdata, pp_filters))
return '\n'.join(printer.print_list(self._viewdata))
def __str__(self):
return '\n'.join(self._printer.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