diff options
| author | Ultrabug <ultrabug@ultrabug.net> | 2014-05-30 11:47:00 +0200 |
|---|---|---|
| committer | Ultrabug <ultrabug@ultrabug.net> | 2014-05-30 11:47:00 +0200 |
| commit | 5915d144132b9d4c614cb7d2e3a3610777b4548e (patch) | |
| tree | 734e05cee6d6d9e88255bba61764d6c6856f9ac9 /modules/keyboard-layout.py | |
| parent | f1ea709cd33acecc382e56b02d0f36ba747b8892 (diff) | |
| parent | e42f84cb9954942b3b604a05fb1933b54ecef5f4 (diff) | |
Merge pull request #33 from ShadowPrince/master
new modules keyboard-layout, battery-level, mpd_status, scratchpad-counter and window-title by @ShadowPrince
Diffstat (limited to 'modules/keyboard-layout.py')
| -rw-r--r-- | modules/keyboard-layout.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/modules/keyboard-layout.py b/modules/keyboard-layout.py new file mode 100644 index 0000000..1ed3cee --- /dev/null +++ b/modules/keyboard-layout.py @@ -0,0 +1,73 @@ +from subprocess import check_output +from time import time + +""" +Module for showing current keyboard layout. + +Requires: + - xkblayout-state + or + - setxkbmap + +@author shadowprince +@license Eclipse Public License +""" + +CACHE_TIMEOUT = 10 # refresh time to update indicator + +# colors of layouts, check your command's output to match keys +LANG_COLORS = { + 'fr': '#268BD2', # solarized blue + 'ru': '#F75252', # red + 'ua': '#FCE94F', # yellow + 'us': '#729FCF', # light blue +} + + +def xbklayout(): + """ + check using xkblayout-state (preferred method) + """ + return check_output( + ["xkblayout-state", "print", "%s"] + ).decode('utf-8') + + +def setxkbmap(): + """ + check using setxkbmap >= 1.3.0 + + Please read issue 33 for more information : + https://github.com/ultrabug/py3status/pull/33 + """ + q = check_output(['setxkbmap', '-query']).decode('utf-8') + return q.replace(' ', '').split(':')[-1] + + +class Py3status: + def __init__(self): + """ + find the best implementation to get the keyboard's layout + """ + try: + xbklayout() + except: + self.command = setxkbmap + else: + self.command = xbklayout + + def keyboard_layout(self, i3status_output_json, i3status_config): + response = { + 'full_text': '', + 'name': 'keyboard-layout', + 'cached_until': time() + CACHE_TIMEOUT + } + + lang = self.command().strip() + lang_color = LANG_COLORS.get(lang) + + response['full_text'] = lang or '??' + if lang_color: + response['color'] = lang_color + + return (0, response) |
