2013-04-13 20:33:06 +00:00
|
|
|
"""
|
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
This plugin defines three new global commands: :term:`/remind`,
|
|
|
|
:term:`/done`, and :term:`/tasks`.
|
|
|
|
|
|
|
|
.. glossary::
|
|
|
|
|
|
|
|
/remind
|
|
|
|
**Usage:** ``/remind <time> <todo>``
|
|
|
|
|
|
|
|
This command will remind you to do ``todo`` every ``time``.
|
|
|
|
|
|
|
|
/done
|
|
|
|
**Usage:** ``/done <id>``
|
|
|
|
Remove a reminder.
|
|
|
|
|
|
|
|
The ``id`` is found using :term:`/tasks`.
|
|
|
|
|
|
|
|
|
|
|
|
/tasks
|
|
|
|
|
|
|
|
Print a list of the tasks, their ids, and their frequency, into the
|
|
|
|
information buffer.
|
|
|
|
|
|
|
|
Time format
|
|
|
|
-----------
|
|
|
|
|
|
|
|
In seconds:
|
|
|
|
|
|
|
|
.. code-block:: none
|
|
|
|
|
|
|
|
/remind 600 Work!
|
|
|
|
|
|
|
|
Will remind you to work every 10 minutes.
|
|
|
|
|
|
|
|
Defining the time in seconds is not really practical, so you can describe it
|
|
|
|
with days, hours, and minutes, in a time-string, e.g:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
/remind 1h23m "Get up"
|
|
|
|
|
|
|
|
Will remind you to get up every 1 hour 23 minutes.
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2016-08-21 13:27:53 +00:00
|
|
|
from poezio.core.structs import Completion
|
2016-06-27 23:10:52 +00:00
|
|
|
from poezio.plugin import BasePlugin
|
|
|
|
from poezio import timed_events
|
2016-08-21 13:27:53 +00:00
|
|
|
from poezio import common
|
|
|
|
import curses
|
2011-11-13 20:55:45 +00:00
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
|
|
|
def init(self):
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_command('remind', self.command_remind,
|
2013-03-01 18:25:31 +00:00
|
|
|
usage='<seconds> <todo>',
|
|
|
|
help='Remind you of <todo> every <time> seconds.',
|
|
|
|
short='Remind you of a task',
|
|
|
|
completion=self.completion_remind)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_command('done', self.command_done,
|
2013-03-01 18:25:31 +00:00
|
|
|
usage='<id>',
|
|
|
|
help='Stop reminding you do the task identified by <id>.',
|
|
|
|
short='Remove a task',
|
|
|
|
completion=self.completion_done)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_command('tasks', self.command_tasks,
|
2013-03-01 18:25:31 +00:00
|
|
|
usage='',
|
|
|
|
help='List all the current tasks and their ids.',
|
|
|
|
short='List current tasks')
|
2011-11-13 20:55:45 +00:00
|
|
|
self.tasks = {}
|
|
|
|
self.count = 0
|
|
|
|
|
2011-11-14 19:46:19 +00:00
|
|
|
for option in self.config.options(self.__module__):
|
|
|
|
id, secs = option.split(',')
|
|
|
|
id = int(id)
|
|
|
|
if id > self.count:
|
|
|
|
self.count = id
|
|
|
|
value = self.config.get(option, '')
|
|
|
|
self.tasks[id] = (int(secs), value)
|
|
|
|
self.config.remove_section(self.__module__)
|
|
|
|
self.config.add_section(self.__module__)
|
|
|
|
if self.tasks:
|
|
|
|
self.count += 1
|
|
|
|
self.command_tasks('', nocommand=True)
|
|
|
|
|
2011-11-13 20:55:45 +00:00
|
|
|
def command_remind(self, arg):
|
|
|
|
args = common.shell_split(arg)
|
|
|
|
if len(args) < 2:
|
|
|
|
return
|
2011-11-15 20:42:10 +00:00
|
|
|
time = common.parse_str_to_secs(args[0])
|
|
|
|
if not time:
|
2011-11-13 20:55:45 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self.tasks[self.count] = (time, args[1])
|
|
|
|
timed_event = timed_events.DelayedEvent(time, self.remind, self.count)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_timed_event(timed_event)
|
|
|
|
self.api.information('Task %s added: %s every %s.' % (self.count, args[1],
|
2011-11-15 20:42:10 +00:00
|
|
|
common.parse_secs_to_str(time)), 'Info')
|
2011-11-13 20:55:45 +00:00
|
|
|
self.count += 1
|
|
|
|
|
2011-11-14 19:46:19 +00:00
|
|
|
def completion_remind(self, the_input):
|
|
|
|
txt = the_input.get_text()
|
|
|
|
args = common.shell_split(txt)
|
|
|
|
n = len(args)
|
|
|
|
if txt.endswith(' '):
|
|
|
|
n += 1
|
|
|
|
if n == 2:
|
2016-08-21 13:27:53 +00:00
|
|
|
return Completion(the_input.auto_completion, ["60", "5m", "15m", "30m", "1h", "10h", "1d"], '')
|
2011-11-14 19:46:19 +00:00
|
|
|
|
|
|
|
def completion_done(self, the_input):
|
2016-08-21 13:27:53 +00:00
|
|
|
return Completion(the_input.auto_completion, ["%s" % key for key in self.tasks], '')
|
2011-11-14 19:46:19 +00:00
|
|
|
|
2011-11-13 20:55:45 +00:00
|
|
|
def command_done(self, arg="0"):
|
|
|
|
try:
|
2017-02-13 18:50:02 +00:00
|
|
|
id_ = int(arg)
|
2011-11-13 20:55:45 +00:00
|
|
|
except:
|
|
|
|
return
|
2017-10-08 14:36:10 +00:00
|
|
|
if id_ not in self.tasks:
|
2011-11-13 20:55:45 +00:00
|
|
|
return
|
|
|
|
|
2017-02-13 18:50:02 +00:00
|
|
|
self.api.information('Task %s: %s [DONE]' % (id_, self.tasks[id_][1]), 'Info')
|
|
|
|
del self.tasks[id_]
|
2011-11-13 20:55:45 +00:00
|
|
|
|
2011-11-14 19:46:19 +00:00
|
|
|
def command_tasks(self, arg, nocommand=None):
|
|
|
|
if nocommand:
|
|
|
|
s = 'The following tasks were loaded:\n'
|
|
|
|
else:
|
|
|
|
s = 'The following tasks are active:\n'
|
2011-11-13 20:55:45 +00:00
|
|
|
for key in self.tasks:
|
2011-11-15 20:42:10 +00:00
|
|
|
s += 'Task %s: %s every %s.\n' % (key, repr(self.tasks[key][1]),
|
|
|
|
common.parse_secs_to_str(self.tasks[key][0]))
|
2011-11-13 20:55:45 +00:00
|
|
|
if s:
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.information(s, 'Info')
|
2011-11-13 20:55:45 +00:00
|
|
|
|
2017-02-13 18:50:02 +00:00
|
|
|
def remind(self, id_=0):
|
2017-10-08 14:36:10 +00:00
|
|
|
if id_ not in self.tasks:
|
2011-11-13 20:55:45 +00:00
|
|
|
return
|
2017-02-13 18:50:02 +00:00
|
|
|
self.api.information('Task %s: %s' % (id_, self.tasks[id_][1]), 'Info')
|
2011-11-13 20:55:45 +00:00
|
|
|
if self.config.get('beep', '') == 'true':
|
|
|
|
curses.beep()
|
2017-02-13 18:50:02 +00:00
|
|
|
timed_event = timed_events.DelayedEvent(self.tasks[id_][0], self.remind, id_)
|
2013-03-08 21:53:35 +00:00
|
|
|
self.api.add_timed_event(timed_event)
|
2011-11-13 20:55:45 +00:00
|
|
|
|
2011-11-14 19:46:19 +00:00
|
|
|
def cleanup(self):
|
|
|
|
if self.tasks:
|
|
|
|
self.config.remove_section(self.__module__)
|
|
|
|
self.config.add_section(self.__module__)
|
|
|
|
for task in self.tasks:
|
|
|
|
self.config.set('%s,%s' % (task, self.tasks[task][0]), self.tasks[task][1])
|
|
|
|
self.config.write()
|