Created
April 20, 2015 00:24
-
-
Save AsaAyers/8502d26aec0d48c76764 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import re | |
import sys | |
import i3 | |
def get_focused(): | |
focused = i3.filter(nodes=[], focused=True) | |
if len(focused) > 0: | |
return focused[0] | |
def get_dest_container(targetApps): | |
parents = [ i3.parent(a['id']) for a in targetApps ] | |
ids = [ p['id'] for p in parents ] | |
# Grab the first one by default | |
targetId = ids[0] | |
for id in ids: | |
if ids.count(id) > ids.count(targetId): | |
targetId = id | |
return [ p for p in parents if p['id'] == targetId ][0] | |
def setup_container(container): | |
if container['layout'] != 'tabbed': | |
print('focus', pin['name']) | |
i3.focus(id=pin['window']) | |
# If this contains any non targetApp windows, we need to split to create a | |
# new container. | |
foreignNodes = [ n for n in container['nodes'] if targetApps.count(n) == 0 ] | |
if len(foreignNodes) > 0: | |
i3.split('v') | |
container = i3.parent(pin['id']) | |
i3.layout('tabbed') | |
return container | |
def move_window(window, dest): | |
print('moving', window['name']) | |
i3.focus(id=window['window']) | |
i3.move('scratchpad') | |
print('focus', dest['name']) | |
i3.focus(id=dest['window']) | |
i3.scratchpad('show') | |
print('focus', window['name']) | |
i3.focus(id=window['window']) | |
i3.floating('toggle') | |
if len(sys.argv) == 1: | |
print("Usage: ./combine.py <window title regex>") | |
print("This should be called from:") | |
print("for_window [class=\"...\"] exec \"combine.py '...'\"") | |
exit() | |
regex = sys.argv[1] | |
newWindow = get_focused() | |
if newWindow is None: | |
print("Couldn't find a focused window") | |
exit() | |
# Find any other windows that match the regex. | |
targetApps = [ w for w in i3.filter(nodes=[]) if re.search(regex, w['name'][-4:]) and w != newWindow ] | |
if len(targetApps) == 0: | |
print("No windows found matching", regex) | |
exit() | |
destContainer = get_dest_container(targetApps) | |
# All windows will become siblings of pin | |
pin = [ a for a in targetApps if i3.parent(a['id']) == destContainer][0] | |
# Exclude the pinned window from the list. | |
targetApps = [a for a in targetApps where a != pin] | |
destContainer = setup_container(destContainer) | |
toMove = [ a for a in targetApps if i3.parent(a['id']) != destContainer ] | |
toMove.append(newWindow) | |
for m in toMove: | |
move_window(m, pin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment