-
Notifications
You must be signed in to change notification settings - Fork 46
/
util.py
executable file
·106 lines (83 loc) · 3.21 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# github.com/justbuchanan/i3scripts
import re
import logging
import subprocess as proc
from collections import namedtuple, Counter
# 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(r'(?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
# Unicode subscript and superscript numbers
_superscript = "⁰¹²³⁴⁵⁶⁷⁸⁹"
_subscript = "₀₁₂₃₄₅₆₇₈₉"
def _encode_base_10_number(n: int, symbols: str) -> str:
"""Write a number in base 10 using symbols from a given string.
Examples:
>>> _encode_base_10_number(42, "0123456789")
"42"
>>> _encode_base_10_number(42, "abcdefghij")
"eb"
>>> _encode_base_10_number(42, "₀₁₂₃₄₅₆₇₈₉")
"₄₂"
"""
return ''.join([symbols[int(digit)] for digit in str(n)])
def format_icon_list(icon_list, icon_list_format='default'):
if icon_list_format.lower() == 'default':
# Default (no formatting)
return ' '.join(icon_list)
elif icon_list_format.lower() == 'mathematician':
# Mathematician mode
# aababa -> a⁴b²
new_list = []
for icon, count in Counter(icon_list).items():
if count > 1:
new_list.append(icon +
_encode_base_10_number(count, _superscript))
else:
new_list.append(icon)
return ' '.join(new_list)
elif icon_list_format.lower() == 'chemist':
# Chemist mode
# aababa -> a₄b₂
new_list = []
for icon, count in Counter(icon_list).items():
if count > 1:
new_list.append(icon +
_encode_base_10_number(count, _subscript))
else:
new_list.append(icon)
return ' '.join(new_list)
else:
raise ValueError("Unknown format name for the list of icons: ",
icon_list_format)