bumblebee-status/modules/contrib/todo.py

52 lines
1.2 KiB
Python
Raw Normal View History

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)
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
import core.input
2020-04-13 13:53:54 +02:00
2020-04-13 13:57:24 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.output))
2020-04-13 13:53:54 +02:00
self.__doc = os.path.expanduser(self.parameter("file", "~/Documents/todo.txt"))
2020-04-13 13:57:24 +02:00
self.__todos = self.count_items()
core.input.register(
self, button=core.input.LEFT_MOUSE, cmd="xdg-open {}".format(self.__doc)
)
2020-04-13 13:53:54 +02:00
def output(self, widget):
return str(self.__todos)
2020-04-13 13:53:54 +02:00
2020-04-13 13:57:24 +02:00
def update(self):
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:
return "empty"
return "items"
2020-04-13 13:53:54 +02:00
def count_items(self):
try:
i = -1
2020-04-13 13:57:24 +02:00
with open(self.__doc) as f:
2020-04-13 13:53:54 +02:00
for i, l in enumerate(f):
pass
return i + 1
2020-04-13 13:53:54 +02:00
except Exception:
return 0
2020-04-13 13:57:24 +02:00
2020-04-13 13:57:24 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4