bumblebee-status/modules/contrib/shortcut.py

70 lines
2.2 KiB
Python
Raw Normal View History

2020-04-21 20:34:10 +02:00
# pylint: disable=C0112,R0903
"""Shows a widget per user-defined shortcut and allows to define the behaviour
when clicking on it.
For more than one shortcut, the commands and labels are strings separated by
a demiliter (; semicolon by default).
For example in order to create two shortcuts labeled A and B with commands
cmdA and cmdB you could do:
2020-04-21 20:34:27 +02:00
./bumblebee-status -m shortcut -p shortcut.cmd='ls;ps' shortcut.label='A;B'
2020-04-21 20:34:10 +02:00
Parameters:
* shortcut.cmds : List of commands to execute
* shortcut.labels: List of widgets' labels (text)
* shortcut.delim : Commands and labels delimiter (; semicolon by default)
contributed by `cacyss0807 <https://github.com/cacyss0807>`_ - many thanks!
2020-04-21 20:34:10 +02:00
"""
import logging
LINK = "https://github.com/tobi-wan-kenobi/bumblebee-status/wiki"
LABEL = "Click me"
2020-04-21 20:34:10 +02:00
import core.module
import core.input
import core.decorators
2020-04-21 20:34:10 +02:00
class Module(core.module.Module):
@core.decorators.every(minutes=60)
def __init__(self, config, theme):
super().__init__(config, theme, [])
2020-04-21 20:34:10 +02:00
self.__labels = self.parameter("labels", "{}".format(LABEL))
self.__cmds = self.parameter("cmds", "firefox {}".format(LINK))
self.__delim = self.parameter("delim", ";")
2020-04-21 20:34:10 +02:00
self.update_widgets()
2020-04-21 20:34:10 +02:00
def update_widgets(self):
2020-04-21 20:34:10 +02:00
""" Creates a set of widget per user define shortcut."""
cmds = self.__cmds.split(self.__delim)
labels = self.__labels.split(self.__delim)
2020-04-21 20:34:10 +02:00
# to be on the safe side create as many widgets as there are data (cmds or labels)
num_shortcuts = min(len(cmds), len(labels))
# report possible problem as a warning
if len(cmds) is not len(labels):
logging.warning(
"shortcut: the number of commands does not match "
"the number of provided labels."
)
logging.warning("cmds : %s, labels : %s", cmds, labels)
2020-04-21 20:34:10 +02:00
for idx in range(0, num_shortcuts):
cmd = cmds[idx]
label = labels[idx]
widget = self.add_widget(full_text=label)
core.input.register(widget, button=core.input.LEFT_MOUSE, cmd=cmd)
2020-04-21 20:34:10 +02:00
2020-04-21 20:34:10 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4