From f4a2a2cb22770f298c13904f05049f0c17c8c3c0 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Mon, 26 May 2014 19:12:26 +0200 Subject: rename current-keyboard-layout to keyboard-layout, PEP8 cleanup, implement check using setxkbmap for maximal compatibility, add french coloring --- modules/keyboard-layout.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 modules/keyboard-layout.py (limited to 'modules/keyboard-layout.py') 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) -- cgit v1.3 From 509506c4a89cab9863a25bb3e74bc10e64c0be53 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Mon, 26 May 2014 19:18:07 +0200 Subject: switch french color to solarized blue on keyboard-layout module --- modules/keyboard-layout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/keyboard-layout.py') diff --git a/modules/keyboard-layout.py b/modules/keyboard-layout.py index be1f72d..6fd4019 100644 --- a/modules/keyboard-layout.py +++ b/modules/keyboard-layout.py @@ -17,7 +17,7 @@ CACHE_TIME = 10 # refresh time to update indicator # colors of layouts, check your command's output to match keys LANG_COLORS = { - 'fr': '#668FBA', # blue + 'fr': '#268BD2', # solarized blue 'ru': '#F75252', # red 'ua': '#FCE94F', # yellow 'us': '#729FCF', # light blue -- cgit v1.3 From 277b0008c2d779a98605737bc91ca75e6b364728 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Tue, 27 May 2014 12:31:44 +0200 Subject: normalize the CACHE_TIMEOUT variable name --- modules/battery-level.py | 4 ++-- modules/keyboard-layout.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'modules/keyboard-layout.py') diff --git a/modules/battery-level.py b/modules/battery-level.py index 4d94311..6c30556 100644 --- a/modules/battery-level.py +++ b/modules/battery-level.py @@ -17,7 +17,7 @@ Requires: @license Eclipse Public License """ -CACHE_TIME = 30 # time to update battery +CACHE_TIMEOUT = 30 # time to update battery HIDE_WHEN_FULL = False # hide any information when battery is fully charged MODE = "bar" # for primitive-one-char bar, or "text" for text percentage ouput @@ -63,6 +63,6 @@ class Py3status: else: response['full_text'] = character - response['cached_until'] = time() + CACHE_TIME + response['cached_until'] = time() + CACHE_TIMEOUT return (0, response) diff --git a/modules/keyboard-layout.py b/modules/keyboard-layout.py index 6fd4019..97ef716 100644 --- a/modules/keyboard-layout.py +++ b/modules/keyboard-layout.py @@ -13,7 +13,7 @@ Requires: @license Eclipse Public License """ -CACHE_TIME = 10 # refresh time to update indicator +CACHE_TIMEOUT = 10 # refresh time to update indicator # colors of layouts, check your command's output to match keys LANG_COLORS = { @@ -57,7 +57,7 @@ class Py3status: response = { 'full_text': '', 'name': 'keyboard-layout', - 'cached_until': time() + CACHE_TIME + 'cached_until': time() + CACHE_TIMEOUT } lang = self.command().strip() -- cgit v1.3 From e42f84cb9954942b3b604a05fb1933b54ecef5f4 Mon Sep 17 00:00:00 2001 From: Ultrabug Date: Fri, 30 May 2014 11:44:36 +0200 Subject: add an information note in the keyboard module --- modules/keyboard-layout.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'modules/keyboard-layout.py') diff --git a/modules/keyboard-layout.py b/modules/keyboard-layout.py index 97ef716..1ed3cee 100644 --- a/modules/keyboard-layout.py +++ b/modules/keyboard-layout.py @@ -36,6 +36,9 @@ def xbklayout(): 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] -- cgit v1.3