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