Refactor i3wm segment and lemonbar binding

Move get_i3_connection and get_xrandr_outputs into bindings.wm
This commit is contained in:
S0lll0s 2016-01-15 15:37:37 +01:00 committed by Foo
parent 24fa03567e
commit db99ad1d90
5 changed files with 65 additions and 45 deletions

View File

@ -10,7 +10,7 @@ from threading import Lock, Timer
from powerline.lemonbar import LemonbarPowerline
from powerline.commands.lemonbar import get_argparser
from powerline.listers.i3wm import get_connected_xrandr_outputs
from powerline.bindings.wm import get_connected_xrandr_outputs
if __name__ == '__main__':

View File

@ -0,0 +1,38 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import re
from powerline.theme import requires_segment_info
from powerline.lib.shell import run_cmd
conn = None
def get_i3_connection():
'''Return a valid, cached i3 Connection instance
'''
global conn
if not conn:
try:
import i3ipc
except ImportError:
import i3 as conn
else:
conn = i3ipc.Connection()
return conn
XRANDR_OUTPUT_RE = re.compile(r'^(?P<name>[0-9A-Za-z-]+) connected (?P<width>\d+)x(?P<height>\d+)\+(?P<x>\d+)\+(?P<y>\d+)', re.MULTILINE)
def get_connected_xrandr_outputs(pl):
'''Iterate over xrandr outputs
Outputs are represented by a dictionary with ``name``, ``width``,
``height``, ``x`` and ``y`` keys.
'''
return (match.groupdict() for match in XRANDR_OUTPUT_RE.finditer(
run_cmd(pl, ['xrandr', '-q'])
))

View File

@ -1,19 +1,9 @@
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import re
from powerline.theme import requires_segment_info
from powerline.lib.shell import run_cmd
from powerline.lib.dict import updated
conn = None
XRANDR_OUTPUT_RE = re.compile(r'^(?P<name>[0-9A-Za-z-]+) connected (?P<width>\d+)x(?P<height>\d+)\+(?P<x>\d+)\+(?P<y>\d+)', re.MULTILINE)
def get_connected_xrandr_outputs(pl):
return (match.groupdict() for match in XRANDR_OUTPUT_RE.finditer(
run_cmd(pl, ['xrandr', '-q'])
))
from powerline.bindings.wm import get_i3_connection, get_connected_xrandr_outputs
@requires_segment_info
@ -72,7 +62,7 @@ def workspace_lister(pl, segment_info, only_show=None, output=None):
'draw_inner_divider': None
}
)
for w in conn.get_workspaces()
for w in get_i3_connection().get_workspaces()
if (((not only_show or any(w[typ] for typ in only_show))
and (not output or w['output'] == output)))
and (not output or w['output'] == output)))
)

View File

@ -2,9 +2,7 @@
from __future__ import (unicode_literals, division, absolute_import, print_function)
from powerline.theme import requires_segment_info
conn = None
from powerline.bindings.wm import get_i3_connection
def calcgrp(w):
@ -39,23 +37,16 @@ def workspaces(pl, segment_info, only_show=None, output=None, strip=0):
Highlight groups used: ``workspace`` or ``w_visible``, ``workspace`` or ``w_focused``, ``workspace`` or ``w_urgent``.
'''
global conn
if not conn:
try:
import i3ipc
except ImportError:
import i3 as conn
else:
conn = i3ipc.Connection()
output = output or segment_info.get('output')
return [{
'contents': w['name'][strip:],
'highlight_groups': calcgrp(w)
} for w in conn.get_workspaces()
return [
{
'contents': w['name'][strip:],
'highlight_groups': calcgrp(w)
}
for w in get_i3_connection().get_workspaces()
if ((not only_show or any(w[typ] for typ in only_show))
and (not output or w['output'] == output))
and (not output or w['output'] == output))
]
@ -65,7 +56,8 @@ def workspace(pl, segment_info, workspace=None, strip=0):
:param str workspace:
Specifies which workspace to show. If unspecified, may be set by the
``list_workspaces`` lister.
``list_workspaces`` lister if used, otherwise falls back to
currently focused workspace.
:param int strip:
Specifies how many characters from the front of each workspace name
@ -73,18 +65,7 @@ def workspace(pl, segment_info, workspace=None, strip=0):
Highlight groups used: ``workspace`` or ``w_visible``, ``workspace`` or ``w_focused``, ``workspace`` or ``w_urgent``.
'''
workspace = workspace or segment_info.get('workspace')['name']
if segment_info.get('workspace') and segment_info.get('workspace')['name'] == workspace:
w = segment_info.get('workspace')
else:
global conn
if not conn:
try:
import i3ipc
except ImportError:
import i3 as conn
else:
conn = i3ipc.Connection()
if workspace:
try:
w = next((
w for w in get_i3_connection().get_workspaces()
@ -92,6 +73,17 @@ def workspace(pl, segment_info, workspace=None, strip=0):
))
except StopIteration:
return None
elif segment_info.get('workspace'):
w = segment_info['workspace']
else:
try:
w = next((
w for w in get_i3_connection().get_workspaces()
if w['focused']
))
except StopIteration:
return None
return [{
'contents': w['name'][min(len(w['name']), strip):],
'highlight_groups': calcgrp(w)

View File

@ -885,7 +885,7 @@ class TestWthr(TestCommon):
class TestI3WM(TestCase):
def test_workspaces(self):
pl = Pl()
with replace_attr(i3wm, 'conn', Args(get_workspaces=lambda: iter([
with replace_attr(i3wm, 'conn', Args(get_i3_workspaces=lambda: iter([
{'name': '1: w1', 'output': 'LVDS1', 'focused': False, 'urgent': False, 'visible': False},
{'name': '2: w2', 'output': 'LVDS1', 'focused': False, 'urgent': False, 'visible': True},
{'name': '3: w3', 'output': 'HDMI1', 'focused': False, 'urgent': True, 'visible': True},