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