From 48191474101299a458dbfc4603a8a10655aa67f2 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 27 Nov 2016 19:48:12 +0100 Subject: [PATCH] [modules/layout] Add autodetection and auto-adding of languages Whenever the language is changed outside the bar, update the bar accordingly. Also, when a new language is used, automatically append it to the list of available languages. --- bumblebee/modules/layout.py | 22 ++++++++++++++++++++++ bumblebee/util.py | 4 +++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/layout.py b/bumblebee/modules/layout.py index 79bd8d8..084d27d 100644 --- a/bumblebee/modules/layout.py +++ b/bumblebee/modules/layout.py @@ -39,7 +39,29 @@ class Module(bumblebee.module.Module): bumblebee.util.execute("setxkbmap -layout {} {}".format(layout, variant)) def widgets(self): + res = bumblebee.util.execute("setxkbmap -query") + layout = None + variant = None + for line in res.split("\n"): + if not line: + continue + if "layout" in line: + layout = line.split(":")[1].strip() + if "variant" in line: + variant = line.split(":")[1].strip() + if variant: + layout += ":" + variant + lang = self._languages[self._idx] + + if lang != layout: + if layout in self._languages: + self._idx = self._languages.index(layout) + else: + self._languages.append(layout) + self._idx = len(self._languages) - 1 + lang = layout + return bumblebee.output.Widget(self, lang) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/util.py b/bumblebee/util.py index feedabf..c38bb79 100644 --- a/bumblebee/util.py +++ b/bumblebee/util.py @@ -24,7 +24,9 @@ def durationfmt(duration): def execute(cmd): args = shlex.split(cmd) p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - out = p.communicate() + out, err = p.communicate() if p.returncode != 0: raise RuntimeError("{} exited with {}".format(cmd, p.returncode)) + + return out.decode("utf-8")