2020-04-13 13:53:54 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the number of todo items from a text file
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* todo.file: File to read TODOs from (defaults to ~/Documents/todo.txt)
|
2020-05-08 20:58:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
contributed by `codingo <https://github.com/codingo>`_ - many thanks!
|
2020-04-13 13:53:54 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
|
2020-04-13 13:57:24 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
2020-04-17 12:53:46 +02:00
|
|
|
import core.input
|
2020-04-13 13:53:54 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-13 13:57:24 +02:00
|
|
|
class Module(core.module.Module):
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.output))
|
2020-04-13 13:53:54 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__doc = os.path.expanduser(self.parameter("file", "~/Documents/todo.txt"))
|
2021-03-20 01:18:46 +01:00
|
|
|
self.__editor = self.parameter("editor", "xdg-open")
|
2020-04-13 13:57:24 +02:00
|
|
|
self.__todos = self.count_items()
|
2020-05-03 11:15:52 +02:00
|
|
|
core.input.register(
|
2021-03-20 01:18:46 +01:00
|
|
|
self, button=core.input.LEFT_MOUSE, cmd="{} {}".format(self.__editor, self.__doc)
|
2021-08-14 19:16:45 +02:00
|
|
|
)
|
2020-04-13 13:53:54 +02:00
|
|
|
|
|
|
|
def output(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
return str(self.__todos)
|
2020-04-13 13:53:54 +02:00
|
|
|
|
2020-04-13 13:57:24 +02:00
|
|
|
def update(self):
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__todos = self.count_items()
|
2020-04-13 13:53:54 +02:00
|
|
|
|
|
|
|
def state(self, widgets):
|
2020-04-13 13:57:24 +02:00
|
|
|
if self.__todos == 0:
|
2020-05-03 11:15:52 +02:00
|
|
|
return "empty"
|
|
|
|
return "items"
|
2020-04-13 13:53:54 +02:00
|
|
|
|
|
|
|
def count_items(self):
|
|
|
|
try:
|
2021-08-14 19:14:54 +02:00
|
|
|
i = 0
|
2020-04-13 13:57:24 +02:00
|
|
|
with open(self.__doc) as f:
|
2021-08-14 19:14:54 +02:00
|
|
|
for l in f.readlines():
|
|
|
|
if l.strip() != '':
|
|
|
|
i += 1
|
|
|
|
return i
|
2020-04-13 13:53:54 +02:00
|
|
|
except Exception:
|
|
|
|
return 0
|
2020-04-13 13:57:24 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-13 13:57:24 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|