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

Handle subcommand's output.

Also make a pretty printer for a single todo.
parent 9f314eef
...@@ -3,6 +3,7 @@ import re ...@@ -3,6 +3,7 @@ import re
import Config import Config
import Command import Command
from PrettyPrinter import pretty_print, pp_number
from RelativeDate import relative_date_to_date from RelativeDate import relative_date_to_date
class AddCommand(Command.Command): class AddCommand(Command.Command):
...@@ -62,5 +63,4 @@ class AddCommand(Command.Command): ...@@ -62,5 +63,4 @@ class AddCommand(Command.Command):
self.todo = self.todolist.add(self.text) self.todo = self.todolist.add(self.text)
self._postprocess_input_todo() self._postprocess_input_todo()
return True self.out(pretty_print(self.todo, [pp_number]))
import Command import Command
from PrettyPrinter import pretty_print, pp_number
from Utils import convert_todo_number from Utils import convert_todo_number
class AppendCommand(Command.Command): class AppendCommand(Command.Command):
...@@ -11,4 +12,4 @@ class AppendCommand(Command.Command): ...@@ -11,4 +12,4 @@ class AppendCommand(Command.Command):
self.todolist.append(number, text) self.todolist.append(number, text)
return True self.out(pretty_print(self.todo, [pp_number]))
...@@ -3,9 +3,12 @@ class Command(object): ...@@ -3,9 +3,12 @@ class Command(object):
self.args = p_args self.args = p_args
self.todolist = p_todolist self.todolist = p_todolist
self.output = []
self.errors = []
def execute(self): def execute(self):
""" The command to execute. """ """ The command to execute. """
return False return (False, None, None)
def argument(self, p_number): def argument(self, p_number):
""" Retrieves a value from the argument list. """ """ Retrieves a value from the argument list. """
...@@ -27,4 +30,10 @@ class Command(object): ...@@ -27,4 +30,10 @@ class Command(object):
return False return False
def usage(self): def usage(self):
return "" return "No usage text defined for this command."
def out(self, p_text): # TODO: make private
self.output.append(p_text)
def error(self, p_text): # TODO: make private
self.errors.append(p_text)
...@@ -41,12 +41,12 @@ class DepCommand(Command.Command): ...@@ -41,12 +41,12 @@ class DepCommand(Command.Command):
# dep ls ... to 1 # dep ls ... to 1
todos = self.todolist.parents(convert_todo_number(arg2)) todos = self.todolist.parents(convert_todo_number(arg2))
else: else:
self.usage() self.errors.append(self.usage())
if todos: if todos:
sorter = Sorter.Sorter(Config.SORT_STRING) sorter = Sorter.Sorter(Config.SORT_STRING)
view = View.View(sorter, [], todos) view = View.View(sorter, [], todos)
print view.pretty_print() # FIXME self.out(view.pretty_print())
def execute(self): def execute(self):
dispatch = { dispatch = {
...@@ -58,7 +58,6 @@ class DepCommand(Command.Command): ...@@ -58,7 +58,6 @@ class DepCommand(Command.Command):
'gc': self.todolist.clean_dependencies, 'gc': self.todolist.clean_dependencies,
} }
dispatch[self.subsubcommand]() if self.subsubcommand in dispatch \ if self.subsubcommand in dispatch:
else self.usage() dispatch[self.subsubcommand]()
return True
import re import re
import Command import Command
from PrettyPrinter import *
from Recurrence import advance_recurring_todo from Recurrence import advance_recurring_todo
from Utils import convert_todo_number from Utils import convert_todo_number
...@@ -13,23 +14,20 @@ class DoCommand(Command.Command): ...@@ -13,23 +14,20 @@ class DoCommand(Command.Command):
def _complete_children(self): def _complete_children(self):
children = [t.attributes['number'] for t in self.todolist.children(self.number) if not t.is_completed()] children = [t.attributes['number'] for t in self.todolist.children(self.number) if not t.is_completed()]
if children: if children:
for child in children: pretty_print_list(children, [pp_number])
# self.print_todo(child) # FIXME
pass
confirmation = raw_input("Also mark subtasks as done? [n] "); # FIXME confirmation = raw_input("Also mark subtasks as done? [n] "); # FIXME
if re.match('^y(es)?$', confirmation, re.I): if re.match('^y(es)?$', confirmation, re.I):
for child in children: for child in children:
self.todolist.set_todo_completed(child) self.todolist.set_todo_completed(child)
# self.print_todo(child) # FIXME self.out(pretty_print(child, [pp_number]))
def _handle_recurrence(self): def _handle_recurrence(self):
if self.todo.has_tag('rec'): if self.todo.has_tag('rec'):
new_todo = advance_recurring_todo(self.todo) new_todo = advance_recurring_todo(self.todo)
self.todolist.add_todo(new_todo) self.todolist.add_todo(new_todo)
# self.print_todo(self.todolist.count()) # FIXME self.out(pretty_print(new_todo, [pp_number]))
def execute(self): def execute(self):
if self.todo and not self.todo.is_completed(): if self.todo and not self.todo.is_completed():
......
...@@ -17,6 +17,4 @@ class ListCommand(Command.Command): ...@@ -17,6 +17,4 @@ class ListCommand(Command.Command):
if len(self.args) > 0: if len(self.args) > 0:
filters.append(Filter.GrepFilter(self.argument(0))) filters.append(Filter.GrepFilter(self.argument(0)))
print self.todolist.view(sorter, filters).pretty_print() # FIXME self.out(self.todolist.view(sorter, filters).pretty_print())
return True
...@@ -5,5 +5,5 @@ class ListContextCommand(Command.Command): ...@@ -5,5 +5,5 @@ class ListContextCommand(Command.Command):
super(ListContextCommand, self).__init__(p_args, p_todolist) super(ListContextCommand, self).__init__(p_args, p_todolist)
def execute(self): def execute(self):
for p in sorted(self.todolist.contexts()): for context in sorted(self.todolist.contexts()):
print p self.out(context)
...@@ -5,5 +5,5 @@ class ListProjectCommand(Command.Command): ...@@ -5,5 +5,5 @@ class ListProjectCommand(Command.Command):
super(ListProjectCommand, self).__init__(p_args, p_todolist) super(ListProjectCommand, self).__init__(p_args, p_todolist)
def execute(self): def execute(self):
for p in sorted(self.todolist.projects()): for project in sorted(self.todolist.projects()):
print p self.out(project)
...@@ -9,37 +9,18 @@ from DepCommand import DepCommand ...@@ -9,37 +9,18 @@ from DepCommand import DepCommand
import Config import Config
from DoCommand import DoCommand from DoCommand import DoCommand
from ListCommand import ListCommand from ListCommand import ListCommand
from ListContextCommand import ListContextCommand
from ListProjectCommand import ListProjectCommand
from PrettyPrinter import * from PrettyPrinter import *
from PriorityCommand import PriorityCommand from PriorityCommand import PriorityCommand
import TodoFile import TodoFile
import TodoList import TodoList
from Utils import convert_todo_number from Utils import convert_todo_number
def print_iterable(p_iter):
""" Prints an iterable to the standard output, one item per line. """
for item in sorted(p_iter):
print item
def usage(): def usage():
""" Prints the usage of the todo.txt CLI """ """ Prints the usage of the todo.txt CLI """
exit(1) exit(1)
def error(p_message, p_exit=True):
""" Prints a message on the standard error. """
sys.stderr.write(p_message + '\n')
if p_exit:
exit(1)
def argument(p_number):
""" Retrieves a value from the argument list. """
try:
value = sys.argv[p_number]
except IndexError:
usage()
return value
def arguments(): def arguments():
""" """
Retrieves all values from the argument list starting from the given Retrieves all values from the argument list starting from the given
...@@ -56,38 +37,6 @@ class Application(object): # TODO: rename to CLIApplication ...@@ -56,38 +37,6 @@ class Application(object): # TODO: rename to CLIApplication
def __init__(self): def __init__(self):
self.todolist = TodoList.TodoList([]) self.todolist = TodoList.TodoList([])
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], [pp_number, pp_color])
print printed[0]
def add(self):
command = AddCommand(arguments(), self.todolist)
if command.execute():
self.print_todo(self.todolist.count())
def append(self):
""" Appends a text to a todo item. """
command = AppendCommand(arguments(), self.todolist)
command.execute()
def dep(self):
command = DepCommand(arguments(), self.todolist)
command.execute()
def do(self):
command = DoCommand(arguments(), self.todolist)
command.execute()
def pri(self):
command = PriorityCommand(arguments(), self.todolist)
command.execute()
def list(self):
command = ListCommand(arguments(), self.todolist)
command.execute()
def run(self): def run(self):
""" Main entry function. """ """ Main entry function. """
todofile = TodoFile.TodoFile(Config.FILENAME) todofile = TodoFile.TodoFile(Config.FILENAME)
...@@ -102,22 +51,31 @@ class Application(object): # TODO: rename to CLIApplication ...@@ -102,22 +51,31 @@ class Application(object): # TODO: rename to CLIApplication
except IndexError: except IndexError:
subcommand = Config.DEFAULT_ACTION subcommand = Config.DEFAULT_ACTION
if subcommand == 'add': subcommand_map = {
self.add() 'add': AddCommand,
elif subcommand == 'app' or subcommand == 'append': 'app': AppendCommand,
self.append() 'append': AppendCommand,
elif subcommand == 'dep': 'dep': DepCommand,
self.dep() 'do': DoCommand,
elif subcommand == 'do': 'ls': ListCommand,
self.do() 'lscon': ListContextCommand,
elif subcommand == 'ls': 'listcon': ListContextCommand,
self.list() 'lsprj': ListProjectCommand,
elif subcommand == 'lsprj' or subcommand == 'listproj': 'lsproj': ListProjectCommand,
print_iterable(self.todolist.projects()) 'pri': PriorityCommand,
elif subcommand == 'lscon' or subcommand == 'listcon': }
print_iterable(self.todolist.contexts())
elif subcommand == 'pri': if subcommand in subcommand_map:
self.pri() command = subcommand_map[subcommand](arguments(), self.todolist)
command.execute()
if len(command.errors):
text = "\n".join(command.errors)
sys.stderr.write(text + '\n')
exit(1)
elif len(command.output):
text = "\n".join(command.output)
sys.stdout.write(text + '\n')
else: else:
usage() usage()
......
...@@ -43,10 +43,9 @@ def pp_number(p_todo_str, p_todo): ...@@ -43,10 +43,9 @@ def pp_number(p_todo_str, p_todo):
""" """
return "%3d %s" % (p_todo.attributes['number'], p_todo_str) return "%3d %s" % (p_todo.attributes['number'], p_todo_str)
def pretty_print(p_todos, p_filters=[]): def pretty_print(p_todo, p_filters=[]):
""" """
Given a list of todo items, pretty print it and return a list of Given a todo item, pretty print it and return a list of formatted strings.
formatted strings.
p_filters is a list of functions that transform the output string, each p_filters is a list of functions that transform the output string, each
function accepting two arguments: function accepting two arguments:
...@@ -57,14 +56,16 @@ def pretty_print(p_todos, p_filters=[]): ...@@ -57,14 +56,16 @@ def pretty_print(p_todos, p_filters=[]):
Examples are pp_color and pp_number in this file. Examples are pp_color and pp_number in this file.
""" """
result = [] todo_str = str(p_todo)
for todo in p_todos:
todo_str = str(todo)
for f in p_filters: for f in p_filters:
todo_str = f(todo_str, todo) todo_str = f(todo_str, p_todo)
result.append(todo_str) return todo_str
return result def pretty_print_list(p_todos, p_filters=[]):
"""
Given a list of todo items, pretty print it and return a list of
formatted strings.
"""
return [pretty_print(todo, p_filters) for todo in p_todos]
import Command import Command
from PrettyPrinter import pretty_print
from Utils import convert_todo_number, is_valid_priority from Utils import convert_todo_number, is_valid_priority
class PriorityCommand(Command.Command): class PriorityCommand(Command.Command):
...@@ -13,9 +14,7 @@ class PriorityCommand(Command.Command): ...@@ -13,9 +14,7 @@ class PriorityCommand(Command.Command):
old_priority = self.todo.priority() old_priority = self.todo.priority()
self.todolist.set_priority(self.number, self.priority) self.todolist.set_priority(self.number, self.priority)
print "Priority changed from %s to %s" \ self.out("Priority changed from %s to %s" % (old_priority, self.priority))
% (old_priority, self.priority) # FIXME self.out(pretty_print(self.todo))
# self.print_todo(number) # FIXME
else: else:
# error("Invalid priority given.") # TODO self.error("Invalid priority given.")
pass
...@@ -5,7 +5,7 @@ A list of todo items. ...@@ -5,7 +5,7 @@ A list of todo items.
import re import re
import Graph import Graph
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print_list
import Todo import Todo
import View import View
...@@ -291,5 +291,5 @@ class TodoList(object): ...@@ -291,5 +291,5 @@ class TodoList(object):
self.dirty = True self.dirty = True
def __str__(self): def __str__(self):
return '\n'.join(pretty_print(self._todos)) return '\n'.join(pretty_print_list(self._todos))
...@@ -28,7 +28,7 @@ class View(object): ...@@ -28,7 +28,7 @@ class View(object):
def pretty_print(self): def pretty_print(self):
""" Pretty prints the view. """ """ Pretty prints the view. """
return '\n'.join(pretty_print(self._viewdata, [pp_number, pp_color])) return '\n'.join(pretty_print_list(self._viewdata, [pp_number, pp_color]))
def __str__(self): def __str__(self):
return '\n'.join(pretty_print(self._viewdata)) return '\n'.join(pretty_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