bumblebee-status/bumblebee/modules/todo.py

50 lines
1.2 KiB
Python
Raw Permalink Normal View History

2017-06-16 08:51:27 +02:00
# pylint: disable=C0111,R0903
"""Displays the number of todo items from a text file
2020-04-16 22:11:25 +02:00
Requires the following executable:
* xdg-open
Parameters:
* todo.file: File to read TODOs from (defaults to ~/Documents/todo.txt)
"""
2017-06-16 08:51:27 +02:00
import bumblebee.input
import bumblebee.output
import bumblebee.engine
2018-06-30 15:56:38 +02:00
import os.path
2017-06-16 08:51:27 +02:00
class Module(bumblebee.engine.Module):
2017-06-16 08:51:27 +02:00
def __init__(self, engine, config):
super(Module, self).__init__(engine, config,
bumblebee.output.Widget(full_text=self.output)
)
2018-06-30 15:56:38 +02:00
self._doc = os.path.expanduser(self.parameter("file", "~/Documents/todo.txt"))
self._todos = self.count_items()
2020-04-16 21:29:18 +02:00
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="xdg-open {0}".format(self._doc))
2017-06-16 08:51:27 +02:00
def output(self, widget):
2020-04-16 21:29:18 +02:00
self._todos = self.count_items()
return str(self._todos)
2017-06-16 08:51:27 +02:00
2017-06-16 08:51:27 +02:00
def state(self, widgets):
if self._todos == 0:
2017-06-16 08:51:27 +02:00
return "empty"
return "items"
2017-06-16 08:51:27 +02:00
2018-06-30 15:56:38 +02:00
def count_items(self):
2017-06-16 08:51:27 +02:00
try:
i = -1
2018-06-30 15:56:38 +02:00
with open(self._doc) as f:
2017-06-16 08:51:27 +02:00
for i, l in enumerate(f):
pass
return i+1
2017-06-16 08:51:27 +02:00
except Exception:
return 0