forked from justbuchanan/i3scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
53 lines (40 loc) · 1.56 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# github.com/justbuchanan/i3scripts
import re
import logging
import subprocess as proc
from collections import namedtuple
# A type that represents a parsed workspace "name".
NameParts = namedtuple('NameParts', ['num', 'shortname', 'icons'])
def focused_workspace(i3):
return [w for w in i3.get_workspaces() if w.focused][0]
# Takes a workspace 'name' from i3 and splits it into three parts:
# * 'num'
# * 'shortname' - the workspace's name, assumed to have no spaces
# * 'icons' - the string that comes after the
# Any field that's missing will be None in the returned dict
def parse_workspace_name(name):
m = re.match('(?P<num>\d+):?(?P<shortname>\w+)? ?(?P<icons>.+)?',
name).groupdict()
return NameParts(**m)
# Given a NameParts object, returns the formatted name
# by concatenating them together.
def construct_workspace_name(parts):
new_name = str(parts.num)
if parts.shortname or parts.icons:
new_name += ':'
if parts.shortname:
new_name += parts.shortname
if parts.icons:
new_name += ' ' + parts.icons
return new_name
# Return an array of values for the X property on the given window.
# Requires xorg-xprop to be installed.
def xprop(win_id, property):
try:
prop = proc.check_output(
['xprop', '-id', str(win_id), property], stderr=proc.DEVNULL)
prop = prop.decode('utf-8')
return re.findall('"([^"]*)"', prop)
except proc.CalledProcessError as e:
logging.warn("Unable to get property for window '%d'" % win_id)
return None