Commit 76a38dcc authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add -n flag to `ls` to limit the number of todos to be printed.

If given, the -n flag overrides the list_limit configuration flag to
limit the number of todos printed by ls.

In combination with -x, -n does not do anything. Rationale is to keep
the behavior of -x intact, matching the semantics of the original
todo.txt CLI.
parent 6838c011
......@@ -226,6 +226,48 @@ class ListCommandTest(CommandTest):
self.assertFalse(self.todolist.is_dirty())
self.assertEqual(self.output, '| 1| Foo.\n')
def test_list31(self):
""" Don't show any todos with -n 0 """
command = ListCommand(["-n", "0"], self.todolist, self.out, self.error)
command.execute()
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "")
def test_list32(self):
""" Only show the top todo. """
command = ListCommand(["-n", "1"], self.todolist, self.out, self.error)
command.execute()
self.assertEqual(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n")
self.assertEqual(self.errors, "")
def test_list33(self):
""" Negative values result in showing all relevent todos. """
command = ListCommand(["-n", "-1"], self.todolist, self.out, self.error)
command.execute()
self.assertEqual(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2\n")
self.assertEqual(self.errors, "")
def test_list34(self):
""" Test non-integer value for -n """
config(p_overrides={('ls', 'list_limit'): '2'})
command = ListCommand(["-n", "foo"], self.todolist, self.out, self.error)
command.execute()
self.assertEqual(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 4| (C) Drink beer @ home\n")
self.assertEqual(self.errors, "")
def test_list35(self):
""" -x flag takes precedence over -n """
command = ListCommand(["-x", "-n", "foo"], self.todolist, self.out, self.error)
command.execute()
self.assertEqual(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 3| (C) Baz @Context1 +Project1 key:value\n| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2\n| 6| x 2014-12-12 Completed but with date:2014-12-12\n")
self.assertEqual(self.errors, "")
def test_help(self):
command = ListCommand(["help"], self.todolist, self.out, self.error)
command.execute()
......
......@@ -49,7 +49,7 @@ class ListCommand(ExpressionCommand):
return True
def _process_flags(self):
opts, args = self.getopt('f:s:x')
opts, args = self.getopt('f:n:s:x')
for opt, value in opts:
if opt == '-x':
......@@ -64,6 +64,11 @@ class ListCommand(ExpressionCommand):
self.printer = IcalPrinter(self.todolist)
else:
self.printer = None
elif opt == '-n':
try:
self.limit = int(value)
except ValueError:
pass # use default value in configuration
self.args = args
......
......@@ -37,6 +37,7 @@ class ExpressionCommand(Command):
self.sort_expression = config().sort_string()
self.show_all = False
self.limit = config().list_limit()
# Commands using last argument differently (i.e as something other than
# todo ID/expression) have to set attribute below to True.
self.last_argument = False
......@@ -75,7 +76,7 @@ class ExpressionCommand(Command):
filters += arg_filters()
if not self.show_all:
filters.append(Filter.LimitFilter(config().list_limit()))
filters.append(Filter.LimitFilter(self.limit))
return filters
......
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