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

Introduce LimitFilter which truncates todolist at given number.

parent 0a6c7eda
......@@ -21,6 +21,7 @@ This module contains some definitions to configure the application.
DEFAULT_ACTION = 'ls'
COLORS = True
HIGHLIGHT_PROJECTS_CONTEXTS = True
LIST_LIMIT=25
FILENAME = 'todo.txt'
ARCHIVE_FILENAME = 'done.txt'
......
......@@ -15,14 +15,13 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
class Filter(object):
def filter(self, p_todos, p_limit=None):
def filter(self, p_todos):
"""
Filters a list of todos. Truncates the list after p_limit todo
items (or no maximum limit if omitted).
"""
result = [t for t in p_todos if self.match(t)]
return result[:p_limit]
return [t for t in p_todos if self.match(t)]
def match(self, p_todo):
""" Default match value. """
......@@ -111,3 +110,10 @@ class InstanceFilter(Filter):
return True
except ValueError:
return False
class LimitFilter(Filter):
def __init__(self, p_limit):
self.limit = max(0, p_limit)
def filter(self, p_todos):
return p_todos[:self.limit]
......@@ -57,6 +57,8 @@ class ListCommand(Command.Command):
if len(args) > 0:
filters.append(Filter.GrepFilter(args[0]))
filters.append(Filter.LimitFilter(Config.LIST_LIMIT))
self.out(self.todolist.view(sorter, filters).pretty_print())
def usage(self):
......
......@@ -24,20 +24,6 @@ import Todo
import TodoList
class FilterTest(unittest.TestCase):
def test_filter1(self):
todo = Todo.Todo("(C) Relevant")
relevance = Filter.RelevanceFilter()
result = relevance.filter([todo], 0)
self.assertEquals(result, [])
def test_filter2(self):
todo = Todo.Todo("(C) Relevant")
relevance = Filter.RelevanceFilter()
result = relevance.filter([todo], 100)
self.assertEquals(result, [todo])
def test_filter3(self):
todo = Todo.Todo("(C) Relevant")
relevance = Filter.RelevanceFilter()
......@@ -126,3 +112,40 @@ class FilterTest(unittest.TestCase):
filtered_todos = instance_filter.filter([])
self.assertEquals([], filtered_todos)
def test_filter12(self):
""" Test limit filter. """
todos = load_file('data/FilterTest1.txt')
limit_filter = Filter.LimitFilter(0)
filtered_todos = limit_filter.filter(todos)
self.assertEquals([], filtered_todos)
def test_filter13(self):
""" Test limit filter. """
todos = load_file('data/FilterTest1.txt')
limit_filter = Filter.LimitFilter(1)
filtered_todos = limit_filter.filter(todos)
self.assertEquals(len(filtered_todos), 1)
self.assertEquals(filtered_todos[0].source(), '(C) This is part of some +Project')
def test_filter14(self):
""" Test limit filter. """
todos = load_file('data/FilterTest1.txt')
limit_filter = Filter.LimitFilter(-1)
filtered_todos = limit_filter.filter(todos)
self.assertEquals([], filtered_todos)
def test_filter15(self):
""" Test limit filter. """
todos = load_file('data/FilterTest1.txt')
limit_filter = Filter.LimitFilter(100)
filtered_todos = limit_filter.filter(todos)
self.assertEquals(len(filtered_todos), 4)
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