Commit 2b7ac602 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add a pretty printer for colors and todo numbers.

parent 44da5d4f
......@@ -3,6 +3,8 @@ This module contains some definitions to configure the application.
"""
DEFAULT_ACTION = 'ls'
COLORS = True
HIGHLIGHT_PROJECTS_CONTEXTS = True
FILENAME = 'todo.txt'
COMPLETED_FILENAME = 'done.txt'
......
......@@ -94,7 +94,7 @@ class Application(object):
if len(sys.argv) > 2:
filters.append(Filter.GrepFilter(sys.argv[2]))
print self.todolist.view(sorter, filters)
print self.todolist.view(sorter, filters).pretty_print()
def run(self):
""" Main entry function. """
......
""" Provides a function to pretty print a list of todo items. """
import re
import Config
PRIORITY_COLORS = {
'A': '\033[36m', # cyan
'B': '\033[33m', # yellow
'C': '\033[34m' # blue
}
def add_colors(p_todo_str, p_todo):
""" Adds colors to the todo string by inserting ANSI codes. """
color = '\033[0m'
try:
color = PRIORITY_COLORS[p_todo.priority()]
except KeyError:
pass
p_todo_str = '%s%s%s' % (color, p_todo_str, '\033[0m')
if Config.HIGHLIGHT_PROJECTS_CONTEXTS:
p_todo_str = re.sub(r'\B(\+|@)\S+', r'\033[31m\g<0>' + color, \
p_todo_str)
return p_todo_str
def pretty_print(p_todos, p_show_numbers=False, p_color=False):
"""
Given a list of todo items, pretty print it and return a list of
formatted strings.
"""
result = []
for todo in p_todos:
todo_str = str(todo)
if p_show_numbers:
todo_str = "%3d %s" % (todo.number, todo_str)
if Config.COLORS and p_color:
todo_str = add_colors(todo_str, todo)
result.append(todo_str)
return result
......@@ -4,6 +4,7 @@ A list of todo items.
import re
from PrettyPrinter import pretty_print
import Todo
import View
......@@ -100,5 +101,5 @@ class TodoList(object):
return View.View(p_sorter, p_filters, self._todos)
def __str__(self):
return '\n'.join([str(x) for x in self._todos])
return '\n'.join(pretty_print(self._todos))
from PrettyPrinter import pretty_print
class View:
def __init__(self, p_sorter, p_filters, p_todos):
self._todos = p_todos
......@@ -17,5 +19,8 @@ class View:
for _filter in self._filters:
self._viewdata = _filter.filter(self._viewdata)
def pretty_print(self):
return '\n'.join(pretty_print(self._todos, True, True))
def __str__(self):
return '\n'.join([str(x) for x in self._viewdata])
return '\n'.join(pretty_print(self._todos))
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