Commit 8d33b9fc authored by Bram Schoenmakers's avatar Bram Schoenmakers

Refactor the pretty printer to make it more flexible.

Instead of numerous booleans that steer the pretty printer, let the
caller pass on the desired transformation function.
parent 50aa61ac
......@@ -9,7 +9,7 @@ from DepCommand import DepCommand
import Config
from DoCommand import DoCommand
from ListCommand import ListCommand
from PrettyPrinter import pretty_print
from PrettyPrinter import *
from PriorityCommand import PriorityCommand
import TodoFile
import TodoList
......@@ -59,7 +59,7 @@ class Application(object): # TODO: rename to CLIApplication
def print_todo(self, p_number):
""" Prints a single todo item to the standard output. """
todo = self.todolist.todo(p_number)
printed = pretty_print([todo], True, True)
printed = pretty_print([todo], [pp_number, pp_color])
print printed[0]
def add(self):
......
......@@ -13,27 +13,48 @@ PRIORITY_COLORS = {
PROJECT_COLOR = '\033[31m' # red
NEUTRAL_COLOR = '\033[0m'
def add_colors(p_todo_str, p_todo):
""" Adds colors to the todo string by inserting ANSI codes. """
def pp_color(p_todo_str, p_todo):
"""
Adds colors to the todo string by inserting ANSI codes.
Should be passed as a filter in the filter list of pretty_print()
"""
color = NEUTRAL_COLOR
try:
color = PRIORITY_COLORS[p_todo.priority()]
except KeyError:
pass
if Config.COLORS:
color = NEUTRAL_COLOR
try:
color = PRIORITY_COLORS[p_todo.priority()]
except KeyError:
pass
p_todo_str = '%s%s%s' % (color, p_todo_str, NEUTRAL_COLOR)
p_todo_str = '%s%s%s' % (color, p_todo_str, NEUTRAL_COLOR)
if Config.HIGHLIGHT_PROJECTS_CONTEXTS:
p_todo_str = re.sub(r'\B(\+|@)\S+', PROJECT_COLOR + r'\g<0>' + color, \
p_todo_str)
if Config.HIGHLIGHT_PROJECTS_CONTEXTS:
p_todo_str = re.sub(r'\B(\+|@)\S+', PROJECT_COLOR + r'\g<0>' + color, \
p_todo_str)
return p_todo_str
def pretty_print(p_todos, p_show_numbers=False, p_color=False):
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_todos, p_filters=[]):
"""
Given a list of todo items, pretty print it and return a list of
formatted strings.
p_filters is a list of functions that transform the output string, each
function accepting two arguments:
* the todo's text that has to be modified;
* the todo object itself which allows for obtaining relevant information.
Examples are pp_color and pp_number in this file.
"""
result = []
......@@ -41,11 +62,8 @@ def pretty_print(p_todos, p_show_numbers=False, p_color=False):
for todo in p_todos:
todo_str = str(todo)
if p_show_numbers:
todo_str = "%3d %s" % (todo.attributes['number'], todo_str)
if Config.COLORS and p_color:
todo_str = add_colors(todo_str, todo)
for f in p_filters:
todo_str = f(todo_str, todo)
result.append(todo_str)
......
""" A view is a list of todos, sorted and filtered. """
from PrettyPrinter import pretty_print
from PrettyPrinter import *
class View(object):
"""
......@@ -28,7 +28,7 @@ class View(object):
def pretty_print(self):
""" Pretty prints the view. """
return '\n'.join(pretty_print(self._viewdata, True, True))
return '\n'.join(pretty_print(self._viewdata, [pp_number, pp_color]))
def __str__(self):
return '\n'.join(pretty_print(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