Skip to content

Commit

Permalink
Implement the CommandSet extension (qtile#1231)
Browse files Browse the repository at this point in the history
  • Loading branch information
zordsdavini authored and ramnes committed Feb 24, 2019
1 parent 5329d75 commit 01313b4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions libqtile/extension/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ def safe_import(module_name, class_name):
safe_import("base", "RunCommand")
safe_import("dmenu", ["Dmenu", "DmenuRun", "J4DmenuDesktop"])
safe_import("window_list", "WindowList")
safe_import("command_set", "CommandSet")
81 changes: 81 additions & 0 deletions libqtile/extension/command_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright (C) 2018, zordsdavini
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from os import system
from .dmenu import Dmenu


class CommandSet(Dmenu):
"""
Give list of commands to be executed in dmenu style.
ex. manage mocp deamon:
.. code-block:: python
Key([mod], 'm', lazy.run_extension(extension.CommandSet(
commands={
'play/pause': '[ $(mocp -i | wc -l) -lt 2 ] && mocp -p || mocp -G',
'next': 'mocp -f',
'previous': 'mocp -r',
'quit': 'mocp -x',
'open': 'urxvt -e mocp',
'shuffle': 'mocp -t shuffle',
'repeat': 'mocp -t repeat',
},
pre_commands=['[ $(mocp -i | wc -l) -lt 1 ] && mocp -S'],
**Theme.dmenu))),
"""

defaults = [
("commands", None, "dictionary of commands where key is runable command"),
("pre_commands", None, "list of commands to be executed before getting dmenu answer"),
]

def __init__(self, **config):
Dmenu.__init__(self, **config)
self.add_defaults(CommandSet.defaults)

def _configure(self, qtile):
Dmenu._configure(self, qtile)

def run(self):
if not self.commands:
return

if self.pre_commands:
for cmd in self.pre_commands:
system(cmd)

out = super(CommandSet, self).run(items=self.commands.keys())

try:
sout = out.rstrip('\n')
except AttributeError:
# out is not a string (for example it's a Popen object returned
# by super(WindowList, self).run() when there are no menu items to
# list
return

if sout not in self.commands:
return

system(self.commands[sout])
7 changes: 2 additions & 5 deletions libqtile/extension/dmenu.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,8 @@ def _configure(self, qtile):
self.configured_command.extend(("-h", str(self.dmenu_height)))

def run(self, items=None):
if items:
if self.dmenu_lines:
lines = min(len(items), self.dmenu_lines)
else:
lines = len(items)
if items and self.dmenu_lines:
lines = min(len(items), self.dmenu_lines)
self.configured_command.extend(("-l", str(lines)))

proc = super().run()
Expand Down
1 change: 1 addition & 0 deletions libqtile/extension/window_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class WindowList(Dmenu):
defaults = [
("item_format", "{group}.{id}: {window}", "the format for the menu items"),
("all_groups", True, "If True, list windows from all groups; otherwise only from the current group"),
("dmenu_lines", "80", "Give lines vertically. Set to None get inline"),
]

def __init__(self, **config):
Expand Down

0 comments on commit 01313b4

Please sign in to comment.