Skip to content

Instantly share code, notes, and snippets.

@lf-
Last active August 19, 2016 08:44
Show Gist options
  • Save lf-/95a1ffb742b01bf1e07c070fb6ad535b to your computer and use it in GitHub Desktop.
Save lf-/95a1ffb742b01bf1e07c070fb6ad535b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import subprocess
import re
import sys
OUTPUT_RE = r'(.+) = (.+)'
PROP_CURRENT = '_NET_CURRENT_DESKTOP(CARDINAL)'
PROP_DESKTOPS = '_NET_DESKTOP_NAMES(UTF8_STRING)'
current_desktop = None
desktops = None
def format_desktops(desks, current):
desks = desks[:]
desks[current] = '*' + desks[current]
return ' | '.join(desks)
result = subprocess.Popen(['/usr/bin/xprop', '-spy', '-root'], stdout=subprocess.PIPE)
for line in result.stdout:
match = re.match(OUTPUT_RE, line.decode())
if not match:
continue
prop = match.group(1)
val = match.group(2)
if prop == PROP_CURRENT:
current_desktop = int(val)
if not desktops:
continue
else:
print(format_desktops(desktops, current_desktop))
sys.stdout.flush()
elif prop == PROP_DESKTOPS:
desktops = [desk[1:-1] for desk in val.split(', ')]
print(format_desktops(desktops, current_desktop))
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment