Commit ddec35c2 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Let PrettyPrinter return a list of TopydoStrings

PrettyPrinter no longer outputs a string, but a TopydoString in case of a
single todo item, or a list of TopydoStrings that may contain color
information. Those who read the PrettyPrinter output should convert it to a
string that can be displayed.
parent 7e9f954c
......@@ -25,9 +25,14 @@ class CommandTest(TopydoTest):
self.errors = ""
def out(self, p_output):
if p_output:
self.output += escape_ansi(p_output + "\n")
if isinstance(p_output, list) and p_output:
self.output += escape_ansi(
"\n".join([str(s) for s in p_output]) + "\n")
elif p_output:
self.output += str(p_output) + "\n"
def error(self, p_error):
if p_error:
self.errors += escape_ansi(p_error + "\n")
if isinstance(p_error, list) and p_error:
self.errors += escape_ansi(p_error + "\n") + "\n"
elif p_error:
self.errors += str(p_error) + "\n"
......@@ -52,4 +52,4 @@ def todolist_to_string(p_list):
def print_view(p_view):
printer = PrettyPrinter()
return printer.print_list(p_view.todos)
return "\n".join([str(s) for s in printer.print_list(p_view.todos)])
......@@ -22,6 +22,8 @@ I/O on the command-line.
import getopt
import sys
from topydo.lib.TopydoString import TopydoString
MAIN_OPTS = "ac:C:d:ht:v"
READ_ONLY_COMMANDS = ('List', 'ListContext', 'ListProject')
......@@ -78,6 +80,17 @@ def write(p_file, p_string):
p_file.write(p_string + "\n")
def output(p_string):
ansi = lambda c: c.as_ansi()
if isinstance(p_string, list):
p_string = "\n".join([s.with_colors(ansi) for s in p_string])
elif isinstance(p_string, TopydoString):
# convert color codes to ANSI
p_string = p_string.with_colors(ansi)
write(sys.stdout, p_string)
def error(p_string):
""" Writes an error on the standard error. """
write(sys.stderr, p_string)
......@@ -210,7 +223,7 @@ class CLIApplicationBase(object):
command = p_command(
p_args,
self.todolist,
lambda o: write(sys.stdout, o),
output,
error,
input)
......
......@@ -30,11 +30,8 @@ class Printer(object):
raise NotImplementedError
def print_list(self, p_todos):
"""
Given a list of todo items, pretty print it and return a list of
formatted strings.
"""
return "\n".join([self.print_todo(todo) for todo in p_todos])
for todo in p_todos:
self.print_todo(todo)
class PrettyPrinter(Printer):
......@@ -69,11 +66,15 @@ class PrettyPrinter(Printer):
for ppf in self.filters:
todo_str = ppf.filter(todo_str, p_todo)
# transform color annotations to ANSI codes
if isinstance(todo_str, TopydoString):
return todo_str.with_colors(lambda c: c.as_ansi())
else:
return todo_str
return TopydoString(todo_str)
def print_list(self, p_todos):
"""
Given a list of todo items, pretty print it and return a list of
formatted TopydoStrings. The output function in the UI should convert
the colors inside properly.
"""
return [self.print_todo(todo) for todo in p_todos]
def pretty_printer_factory(p_todolist, p_additional_filters=None):
......
......@@ -279,4 +279,4 @@ class TodoListBase(object):
this list.
"""
printer = PrettyPrinter()
return printer.print_list(self._todos)
return "\n".join([str(s) for s in printer.print_list(self._todos)])
......@@ -26,8 +26,13 @@ class TopydoString(collections.UserString):
"""
def __init__(self, p_content):
super().__init__(p_content)
if isinstance(p_content, TopydoString):
# don't nest topydostrings
self.colors = p_content.colors
super().__init__(p_content.data)
else:
self.colors = {}
super().__init__(p_content)
def append(self, p_string, p_color):
""" Append a string with the given color. """
......
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