Commit 941a8956 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Make padding of IDs dependent of todo list length

When using numerical IDs, the ID width is padded to the log10 of the
todo length.

When using textual IDs, todo lists of 466 items or shorter pad to 3
characters, otherwise 4 characters.
parent f57bbc0c
This diff is collapsed.
This diff is collapsed.
......@@ -162,9 +162,9 @@ class ListFormatParser(object):
# todo ID
'i': lambda t: str(self.todolist.number(t)),
# todo ID pre-filled with 1 or 2 spaces if its length is <3
'I': lambda t: _filler(str(self.todolist.number(t)), 3),
# todo ID, padded with spaces
'I': lambda t: _filler(str(self.todolist.number(t)),
self.todolist.max_id_length()),
# list of tags (spaces) without hidden ones and due: and t:
'k': lambda t: ' '.join([u'{}:{}'.format(tag, value)
......@@ -179,8 +179,9 @@ class ListFormatParser(object):
# line number
'n': lambda t: str(self.todolist.linenumber(t)),
# line number, pre-filled with 1 or 2 spaces if its length <3
'N': lambda t: _filler(str(self.todolist.linenumber(t)), 3),
# line number, padded with spaces
'N': lambda t: _filler(str(self.todolist.linenumber(t)),
self.todolist.max_id_length()),
# priority
'p': lambda t: t.priority() if t.priority() else '',
......@@ -206,8 +207,9 @@ class ListFormatParser(object):
# unique text ID
'u': lambda t: self.todolist.uid(t),
# unique text ID, pre-filled with 1 or 2 spaces if its length <3
'U': lambda t: _filler(self.todolist.uid(t), 3),
# unique text ID, padded with spaces
'U': lambda t: _filler(self.todolist.uid(t),
self.todolist.max_id_length()),
# absolute completion date
'x': lambda t: 'x ' + t.completion_date().isoformat() if t.is_completed() else '',
......
......@@ -18,12 +18,13 @@
A list of todo items.
"""
import math
import re
from datetime import date
from topydo.lib import Filter
from topydo.lib.Config import config
from topydo.lib.HashListValues import hash_list_values
from topydo.lib.HashListValues import hash_list_values, max_id_length
from topydo.lib.printers.PrettyPrinter import PrettyPrinter
from topydo.lib.Todo import Todo
from topydo.lib.View import View
......@@ -275,6 +276,19 @@ class TodoListBase(object):
else:
return self.linenumber(p_todo)
def max_id_length(self):
"""
Returns the maximum length of a todo ID, used for formatting purposes.
"""
if config().identifiers() == "text":
return max_id_length(len(self._todos))
else:
try:
return math.ceil(math.log(len(self._todos), 10))
except ValueError:
return 0
def _update_todo_ids(self):
# the idea is to have a hash that is independent of the position of the
# todo. Use the text (without tags) of the todo to keep the id as
......
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