diff options
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/current-keyboard-layout.py | 33 | ||||
| -rw-r--r-- | modules/keyboard-layout.py | 70 |
2 files changed, 70 insertions, 33 deletions
diff --git a/modules/current-keyboard-layout.py b/modules/current-keyboard-layout.py deleted file mode 100644 index 046a2aa..0000000 --- a/modules/current-keyboard-layout.py +++ /dev/null @@ -1,33 +0,0 @@ -import subprocess -import time - -""" -Module for showing current keyboard layout. -DEPENDS ON xkblayout-state! - -@author shadowprince -@version 1.0 -@license Eclipse Public License -""" - -CACHE_TIME = 0.2 # maximum time to update indicator - -# colors of layouts -# key must be compatible with xkblayout-state print %s! -LANG_COLORS = { - "ru": "#F75252", - "us": "#729FCF", - "ua": "#FCE94F", - } - -class Py3status: - def currentLayout(self, i3status_output_json, i3status_config): - response = {'full_text': '', 'name': 'current-layout', 'cached_until': time.time()+CACHE_TIME} - lang = subprocess.check_output(["xkblayout-state", "print", "%s"]).decode('utf-8') - - return (0, { - "full_text": lang, - "name": "current-layout", - "cached_until": time.time() + CACHE_TIME, - "color": LANG_COLORS.get(lang, "#ffffff"), - }) diff --git a/modules/keyboard-layout.py b/modules/keyboard-layout.py new file mode 100644 index 0000000..be1f72d --- /dev/null +++ b/modules/keyboard-layout.py @@ -0,0 +1,70 @@ +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_TIME = 10 # refresh time to update indicator + +# colors of layouts, check your command's output to match keys +LANG_COLORS = { + 'fr': '#668FBA', # 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 + """ + 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_TIME + } + + 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) |
