Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
topydo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
topydo
Commits
f856c006
Commit
f856c006
authored
Nov 14, 2015
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved out a few functions from the parser class
Also marked a few functions private.
parent
6b204b4b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
40 deletions
+43
-40
topydo/lib/ListFormat.py
topydo/lib/ListFormat.py
+43
-40
No files found.
topydo/lib/ListFormat.py
View file @
f856c006
...
...
@@ -29,7 +29,11 @@ MAIN_PATTERN = (r'^({{(?P<before>.+?)}})?'
r'({{(?P<after>.+?)}})?'
r'(?P<whitespace> *)'
)
def
filler
(
p_str
,
p_len
):
def
_columns
():
""" Returns the number of columns of the terminal. """
return
get_terminal_size
().
columns
def
_filler
(
p_str
,
p_len
):
"""
Returns p_str preceded by additional spaces if p_str is shorter than p_len.
"""
...
...
@@ -64,7 +68,7 @@ def humanize_dates(p_due=None, p_start=None, p_creation=None):
return
', '
.
join
(
dates_list
)
def
strip_placeholder_braces
(
p_matchobj
):
def
_
strip_placeholder_braces
(
p_matchobj
):
"""
Returns string with conditional braces around placeholder stripped and
percent sign glued into placeholder character.
...
...
@@ -86,25 +90,52 @@ def strip_placeholder_braces(p_matchobj):
return
before
+
'%'
+
placeholder
+
after
+
whitespace
def
unescape_percent_sign
(
p_str
):
def
_
unescape_percent_sign
(
p_str
):
""" Strips backslashes from escaped percent signs in p_str. """
unescaped_str
=
re
.
sub
(
r'\\%'
,
'%'
,
p_str
)
return
unescaped_str
def
remove_redundant_spaces
(
p_str
):
def
_
remove_redundant_spaces
(
p_str
):
""" Removes spaces surrunding <TAB> character (
\
t
) from p_str. """
clean_str
=
re
.
sub
(
' *
\
t
*'
,
'
\
t
'
,
p_str
)
return
clean_str
def
_truncate
(
p_str
,
p_repl
):
"""
Returns p_str with truncated and ended with '...' version of p_repl.
Place of the truncation is calculated depending on p_max_width.
"""
# 4 is for '...' and an extra space at the end
text_lim
=
_columns
()
-
len
(
p_str
)
-
4
truncated_str
=
re
.
sub
(
re
.
escape
(
p_repl
),
p_repl
[:
text_lim
]
+
'...'
,
p_str
)
return
truncated_str
def
_right_align
(
p_str
):
"""
Returns p_str with content after <TAB> character aligned right.
Right alignment is done using proper number of spaces calculated from
'line_width' attribute.
"""
to_fill
=
_columns
()
-
len
(
p_str
)
if
to_fill
>
0
:
p_str
=
re
.
sub
(
'
\
t
'
,
' '
*
to_fill
,
p_str
)
else
:
p_str
=
re
.
sub
(
'
\
t
'
,
' '
,
p_str
)
return
p_str
class
ListFormatParser
(
object
):
""" Parser of format string. """
def
__init__
(
self
,
p_todolist
,
p_format
=
None
):
self
.
format_string
=
re
.
sub
(
r'\\t'
,
'
\
t
'
,
p_format
or
config
().
list_format
())
self
.
todolist
=
p_todolist
self
.
one_line
=
False
self
.
line_width
=
get_terminal_size
().
columns
self
.
placeholders
=
{
# absolute creation date
'c'
:
lambda
t
:
t
.
creation_date
().
isoformat
()
if
t
.
creation_date
()
else
''
,
...
...
@@ -127,7 +158,7 @@ class ListFormatParser(object):
'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
),
'I'
:
lambda
t
:
_
filler
(
str
(
self
.
todolist
.
number
(
t
)),
3
),
# list of tags (spaces) without hidden ones and due: and t:
...
...
@@ -206,34 +237,6 @@ class ListFormatParser(object):
return preprocessed_format
def truncate(self, p_str, p_repl):
"""
Returns p_str with truncated and ended with '
...
' version of p_repl.
Place of the truncation is calculated depending on '
line_width
'
attribute.
"""
text_lim = self.line_width - len(p_str) - 4
truncated_str = re.sub(re.escape(p_repl), p_repl[:text_lim] + '
...
', p_str)
return truncated_str
def right_align(self, p_str):
"""
Returns p_str with content after <TAB> character aligned right.
Right alignment is done using proper number of spaces calculated from
'
line_width
' attribute.
"""
to_fill = self.line_width - len(p_str)
if to_fill > 0:
p_str = re.sub('
\
t
', '
'*to_fill, p_str)
else:
p_str = re.sub('
\
t
', '
', p_str)
return p_str
def parse(self, p_todo):
"""
Returns fully parsed string from '
format_string
' attribute with all
...
...
@@ -256,18 +259,18 @@ class ListFormatParser(object):
if repl == '':
substr = re.sub(pattern, '', substr)
else:
substr = re.sub(pattern, strip_placeholder_braces, substr)
substr = re.sub(pattern,
_
strip_placeholder_braces, substr)
substr = re.sub(r'
(
?
<
!
\\
)
%
({
ph
}
|
\
[{
ph
}
\
])
'.format(ph=placeholder), repl, substr)
parsed_list.append(substr)
parsed_str = unescape_percent_sign(''.join(parsed_list))
parsed_str = remove_redundant_spaces(parsed_str)
parsed_str =
_
unescape_percent_sign(''.join(parsed_list))
parsed_str =
_
remove_redundant_spaces(parsed_str)
if self.one_line and len(parsed_str) >=
self.line_width
:
parsed_str =
self.
truncate(parsed_str, repl_trunc)
if self.one_line and len(parsed_str) >=
_columns()
:
parsed_str =
_
truncate(parsed_str, repl_trunc)
if re.search('
.
*
\
t
', parsed_str):
parsed_str =
self.
right_align(parsed_str)
parsed_str =
_
right_align(parsed_str)
return parsed_str.rstrip()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment