Commit 4e9c8b54 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add pretty printer filter for hiding certain tags.

topydo may introduce some internal tags, such as 'id' and 'p' for
dependency tracking, and 'ical' for keeping track of unique ids when
outputting iCalendar files.

Add a pretty printer filter that prevents certain tags from displaying
in the 'ls' output. These tags can be configured with the 'hide_tags'
configuration option in the 'ls' section, a comma-separated list of
tags (default: id,p,ical). To always show all tags, leave an empty
value.
parent 82ab00a6
# Topydo - A todo.txt client written in Python. # Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 Bram Schoenmakers <me@bramschoenmakers.nl> # Copyright (C) 2014 - 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
...@@ -190,7 +190,7 @@ class AddCommandTest(CommandTest.CommandTest): ...@@ -190,7 +190,7 @@ class AddCommandTest(CommandTest.CommandTest):
command = ListCommand.ListCommand(["Bar"], self.todolist, self.out, self.error) command = ListCommand.ListCommand(["Bar"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertEquals(self.output, "|5dh| {today} Bar p:1 +Project\n|5dh| {today} Bar p:1 +Project\n".format(today=self.today)) self.assertEquals(self.output, "|5dh| {today} Bar p:1 +Project\n|5dh| {today} Bar +Project\n".format(today=self.today))
def test_add_reldate1(self): def test_add_reldate1(self):
command = AddCommand.AddCommand(["Foo due:today"], self.todolist, self.out, self.error) command = AddCommand.AddCommand(["Foo due:today"], self.todolist, self.out, self.error)
......
...@@ -31,7 +31,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -31,7 +31,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(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 p:1\n") self.assertEquals(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.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list3(self): def test_list3(self):
...@@ -39,7 +39,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -39,7 +39,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list4(self): def test_list4(self):
...@@ -47,7 +47,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -47,7 +47,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value id:1\n| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value\n| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list5(self): def test_list5(self):
...@@ -55,7 +55,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -55,7 +55,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 3| (C) Baz @Context1 +Project1 key:value id:1\n| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2 p:1\n| 6| x 2014-12-12 Completed but with date:2014-12-12\n") self.assertEquals(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.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list6(self): def test_list6(self):
...@@ -71,7 +71,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -71,7 +71,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value id:1\n| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n") self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value\n| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list8(self): def test_list8(self):
...@@ -79,7 +79,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -79,7 +79,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list9(self): def test_list9(self):
...@@ -87,7 +87,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -87,7 +87,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 5| (C) 13 + 29 = 42\n| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list10(self): def test_list10(self):
...@@ -95,7 +95,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -95,7 +95,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list11(self): def test_list11(self):
...@@ -115,7 +115,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -115,7 +115,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 3| (C) Baz @Context1 +Project1 key:value id:1\n| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 3| (C) Baz @Context1 +Project1 key:value\n| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list13(self): def test_list13(self):
...@@ -123,7 +123,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -123,7 +123,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n| 3| (C) Baz @Context1 +Project1 key:value id:1\n| 4| (C) Drink beer @ home\n| 5| (C) 13 + 29 = 42\n| 6| x 2014-12-12 Completed but with date:2014-12-12\n") self.assertEquals(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| 6| x 2014-12-12 Completed but with date:2014-12-12\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list14(self): def test_list14(self):
...@@ -133,7 +133,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -133,7 +133,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(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 p:1\n") self.assertEquals(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.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list15(self): def test_list15(self):
...@@ -141,7 +141,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -141,7 +141,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 2| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "| 2| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list16(self): def test_list16(self):
...@@ -151,7 +151,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -151,7 +151,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "|6iu| (C) Foo @Context2 Not@Context +Project1 Not+Project\n|til| (C) Drink beer @ home\n| c5| (C) 13 + 29 = 42\n|xvb| (D) Bar @Context1 +Project2 p:1\n") self.assertEquals(self.output, "|6iu| (C) Foo @Context2 Not@Context +Project1 Not+Project\n|til| (C) Drink beer @ home\n| c5| (C) 13 + 29 = 42\n|xvb| (D) Bar @Context1 +Project2\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list17(self): def test_list17(self):
...@@ -159,7 +159,7 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -159,7 +159,7 @@ class ListCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value id:1\n") self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_list18(self): def test_list18(self):
...@@ -169,6 +169,17 @@ class ListCommandTest(CommandTest.CommandTest): ...@@ -169,6 +169,17 @@ class ListCommandTest(CommandTest.CommandTest):
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 6| x 2014-12-12 Completed but with date:2014-12-12\n") self.assertEquals(self.output, "| 6| x 2014-12-12 Completed but with date:2014-12-12\n")
def test_list19(self):
""" Force showing all tags. """
config('test/data/listcommand-tags.conf')
command = ListCommand(["-s", "text", "-x", "Project1"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "| 3| (C) Baz @Context1 +Project1 key:value id:1\n| 1| (C) Foo @Context2 Not@Context +Project1 Not+Project\n")
self.assertEquals(self.errors, "")
def test_help(self): def test_help(self):
command = ListCommand(["help"], self.todolist, self.out, self.error) command = ListCommand(["help"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -9,6 +9,7 @@ highlight_projects_contexts = 1 ...@@ -9,6 +9,7 @@ highlight_projects_contexts = 1
identifiers = linenumber ; or: text identifiers = linenumber ; or: text
[ls] [ls]
hide_tags = id,p,ical
indent = 0 indent = 0
list_limit = -1 list_limit = -1
......
# Topydo - A todo.txt client written in Python. # Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 Bram Schoenmakers <me@bramschoenmakers.nl> # Copyright (C) 2014 - 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
...@@ -45,6 +45,7 @@ class _Config: ...@@ -45,6 +45,7 @@ class _Config:
'identifiers': 'linenumber', 'identifiers': 'linenumber',
# ls # ls
'hide_tags': 'id,p,ical',
'indent': 0, 'indent': 0,
'list_limit': '-1', 'list_limit': '-1',
...@@ -164,6 +165,11 @@ class _Config: ...@@ -164,6 +165,11 @@ class _Config:
def tag_star(self): def tag_star(self):
return self._get_tag('tag_star') return self._get_tag('tag_star')
def hidden_tags(self):
""" Returns a list of tags to be hidden from the 'ls' output. """
hidden_tags = self.cp.get('ls', 'hide_tags')
return [] if hidden_tags == '' else hidden_tags.split(',')
def config(p_path=None): def config(p_path=None):
""" """
Retrieve the config instance. Retrieve the config instance.
......
...@@ -196,3 +196,4 @@ class OrdinalTagFilter(Filter): ...@@ -196,3 +196,4 @@ class OrdinalTagFilter(Filter):
return operand1 != operand2 return operand1 != operand2
return False return False
...@@ -19,7 +19,10 @@ import re ...@@ -19,7 +19,10 @@ import re
from topydo.lib.Command import Command from topydo.lib.Command import Command
from topydo.lib.Config import config from topydo.lib.Config import config
from topydo.lib import Filter from topydo.lib import Filter
from topydo.lib.PrettyPrinterFilter import PrettyPrinterIndentFilter from topydo.lib.PrettyPrinterFilter import (
PrettyPrinterIndentFilter,
PrettyPrinterHideTagFilter
)
from topydo.lib.Sorter import Sorter from topydo.lib.Sorter import Sorter
from topydo.lib.View import View from topydo.lib.View import View
...@@ -84,7 +87,13 @@ class ListCommand(Command): ...@@ -84,7 +87,13 @@ class ListCommand(Command):
def _print(self): def _print(self):
""" Prints the todos. """ """ Prints the todos. """
indent = config().list_indent() indent = config().list_indent()
self.out(self._view().pretty_print([PrettyPrinterIndentFilter(indent)])) hidden_tags = config().hidden_tags()
filters = []
filters.append(PrettyPrinterIndentFilter(indent))
filters.append(PrettyPrinterHideTagFilter(hidden_tags))
self.out(self._view().pretty_print(filters))
def execute(self): def execute(self):
if not super(ListCommand, self).execute(): if not super(ListCommand, self).execute():
......
...@@ -88,3 +88,17 @@ class PrettyPrinterNumbers(PrettyPrinterFilter): ...@@ -88,3 +88,17 @@ class PrettyPrinterNumbers(PrettyPrinterFilter):
def filter(self, p_todo_str, p_todo): def filter(self, p_todo_str, p_todo):
""" Prepends the number to the todo string. """ """ Prepends the number to the todo string. """
return "|{:>3}| {}".format(self.todolist.number(p_todo), p_todo_str) return "|{:>3}| {}".format(self.todolist.number(p_todo), p_todo_str)
class PrettyPrinterHideTagFilter(PrettyPrinterFilter):
""" Removes all occurences of the given tags from the text. """
def __init__(self, p_hidden_tags):
super(PrettyPrinterHideTagFilter, self).__init__()
self.hidden_tags = p_hidden_tags
def filter(self, p_todo_str, _):
for hidden_tag in self.hidden_tags:
# inspired from remove_tag in TodoBase
p_todo_str = re.sub(r'\s?\b' + hidden_tag + r':\S+\b', '',
p_todo_str)
return p_todo_str
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