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
11375943
Commit
11375943
authored
Aug 01, 2015
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Shuffled around some code and added docstrings.
parent
d7d40205
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
52 deletions
+68
-52
topydo/cli/TopydoCompleter.py
topydo/cli/TopydoCompleter.py
+68
-52
No files found.
topydo/cli/TopydoCompleter.py
View file @
11375943
...
...
@@ -14,6 +14,11 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
This module provides a completer class that can be used by get_input provided
by the prompt toolkit.
"""
import
datetime
import
re
...
...
@@ -23,84 +28,95 @@ from topydo.lib.Config import config
from
topydo.Commands
import
_SUBCOMMAND_MAP
from
topydo.lib.RelativeDate
import
relative_date_to_date
def
_date_suggestions
():
"""
Returns a list of relative date that is presented to the user as auto
complete suggestions.
"""
# don't use strftime, prevent locales to kick in
days_of_week
=
{
0
:
"Monday"
,
1
:
"Tuesday"
,
2
:
"Wednesday"
,
3
:
"Thursday"
,
4
:
"Friday"
,
5
:
"Saturday"
,
6
:
"Sunday"
}
dates
=
[
'today'
,
'tomorrow'
,
]
# show days of week up to next week
dow
=
datetime
.
date
.
today
().
weekday
()
for
i
in
range
(
dow
+
2
%
7
,
dow
+
7
):
dates
.
append
(
days_of_week
[
i
%
7
])
# and some more relative days starting from next week
dates
+=
[
"1w"
,
"2w"
,
"1m"
,
"2m"
,
"3m"
,
"1y"
]
return
dates
def
_subcommands
(
p_word_before_cursor
):
""" Generator for subcommand name completion. """
subcommands
=
[
sc
for
sc
in
sorted
(
_SUBCOMMAND_MAP
.
keys
())
if
sc
.
startswith
(
p_word_before_cursor
)]
for
command
in
subcommands
:
yield
Completion
(
command
,
-
len
(
p_word_before_cursor
))
def
_dates
(
p_word_before_cursor
):
""" Generator for date completion. """
def
_date_suggestions
():
"""
Returns a list of relative date that is presented to the user as auto
complete suggestions.
"""
# don't use strftime, prevent locales to kick in
days_of_week
=
{
0
:
"Monday"
,
1
:
"Tuesday"
,
2
:
"Wednesday"
,
3
:
"Thursday"
,
4
:
"Friday"
,
5
:
"Saturday"
,
6
:
"Sunday"
}
dates
=
[
'today'
,
'tomorrow'
,
]
# show days of week up to next week
dow
=
datetime
.
date
.
today
().
weekday
()
for
i
in
range
(
dow
+
2
%
7
,
dow
+
7
):
dates
.
append
(
days_of_week
[
i
%
7
])
# and some more relative days starting from next week
dates
+=
[
"1w"
,
"2w"
,
"1m"
,
"2m"
,
"3m"
,
"1y"
]
return
dates
to_absolute
=
lambda
s
:
relative_date_to_date
(
s
).
isoformat
()
start_value_pos
=
p_word_before_cursor
.
find
(
':'
)
+
1
value
=
p_word_before_cursor
[
start_value_pos
:]
for
reldate
in
_date_suggestions
():
if
not
reldate
.
startswith
(
value
):
continue
yield
Completion
(
reldate
,
-
len
(
value
),
display_meta
=
to_absolute
(
reldate
))
class
TopydoCompleter
(
Completer
):
"""
Completer class that completes projects, contexts, dates and
subcommands.
"""
def
__init__
(
self
,
p_todolist
):
self
.
todolist
=
p_todolist
def
_subcommands
(
self
,
p_word_before_cursor
):
subcommands
=
[
sc
for
sc
in
sorted
(
_SUBCOMMAND_MAP
.
keys
())
if
sc
.
startswith
(
p_word_before_cursor
)]
for
command
in
subcommands
:
yield
Completion
(
command
,
-
len
(
p_word_before_cursor
))
def
_projects
(
self
,
p_word_before_cursor
):
projects
=
[
p
for
p
in
self
.
todolist
.
projects
()
if
p
.
startswith
(
p_word_before_cursor
[
1
:])]
""" Generator for project completion. """
projects
=
[
p
for
p
in
self
.
todolist
.
projects
()
if
p
.
startswith
(
p_word_before_cursor
[
1
:])]
for
project
in
projects
:
yield
Completion
(
"+"
+
project
,
-
len
(
p_word_before_cursor
))
def
_contexts
(
self
,
p_word_before_cursor
):
contexts
=
[
c
for
c
in
self
.
todolist
.
contexts
()
if
c
.
startswith
(
p_word_before_cursor
[
1
:])]
""" Generator for context completion. """
contexts
=
[
c
for
c
in
self
.
todolist
.
contexts
()
if
c
.
startswith
(
p_word_before_cursor
[
1
:])]
for
context
in
contexts
:
yield
Completion
(
"@"
+
context
,
-
len
(
p_word_before_cursor
))
def
_dates
(
self
,
p_word_before_cursor
):
to_absolute
=
lambda
s
:
relative_date_to_date
(
s
).
isoformat
()
start_value_pos
=
p_word_before_cursor
.
find
(
':'
)
+
1
value
=
p_word_before_cursor
[
start_value_pos
:]
for
reldate
in
_date_suggestions
():
if
not
reldate
.
startswith
(
value
):
continue
yield
Completion
(
reldate
,
-
len
(
value
),
display_meta
=
to_absolute
(
reldate
))
def
get_completions
(
self
,
p_document
,
_
):
# include all characters except whitespaces (for + and @)
word_before_cursor
=
p_document
.
get_word_before_cursor
(
True
)
is_first_word
=
not
re
.
match
(
r'\
s*
\S+\
s
', p_document.current_line_before_cursor)
if is_first_word:
return
self.
_subcommands(word_before_cursor)
return _subcommands(word_before_cursor)
elif word_before_cursor.startswith('
+
'):
return self._projects(word_before_cursor)
elif word_before_cursor.startswith('
@
'):
return self._contexts(word_before_cursor)
elif word_before_cursor.startswith(config().tag_due() + '
:
'):
return
self.
_dates(word_before_cursor)
return _dates(word_before_cursor)
elif word_before_cursor.startswith(config().tag_start() + '
:
'):
return
self.
_dates(word_before_cursor)
return _dates(word_before_cursor)
return []
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