Skip to content

Instantly share code, notes, and snippets.

@bancek
Created October 5, 2012 06:24
Show Gist options
  • Save bancek/3838394 to your computer and use it in GitHub Desktop.
Save bancek/3838394 to your computer and use it in GitHub Desktop.
Terminator layout builder
INDENT = ' '
lines = open('data').read().splitlines()
def calc_indent(line):
return len([x for x in line.split(INDENT) if not x])
_last_id = 0
def next_id(name):
global _last_id
id = _last_id
_last_id += 1
return '%s%s' % (name, id)
def elm(data):
if data == 'window':
return {'type': 'Window', 'children': [], 'id': next_id('window')}
if data == 'notebook':
return {'type': 'Notebook', 'children': [], 'id': next_id('notebook')}
if data == 'horizontal':
return {'type': 'HPaned', 'children': [], 'id': next_id('hpaned')}
if data == 'vertical':
return {'type': 'VPaned', 'children': [], 'id': next_id('vpaned')}
return {'type': 'Terminal', 'command': data, 'id': next_id('terminal')}
last_indent = 0
stack = [[]]
for line in lines:
if line.strip():
indent = calc_indent(line)
if indent < last_indent:
for _ in range(last_indent - indent):
stack.pop(-1)
last_indent = indent
data = elm(line.strip())
stack[-1].append(data)
if 'children' in data:
stack.append(data['children'])
b = ' '
def printer(data, parent=None, order=None):
print b + '[[[%s]]]' % data['id']
print b + ' type = %s' % data['type']
print b + ' order = %d' % order
if parent:
print b + ' parent = %s' % parent['id']
else:
print b + ' parent = ""'
if data['type'] == 'Terminal':
print b + ' profile = default'
print b + ' command = bashinit \'%s\'' % data['command']
if data['type'] == 'Notebook':
print b + ' labels = ' + ', '.join(['None'] * len(data['children']))
if 'children' in data:
for i, child in enumerate(data['children']):
printer(child, data, i)
for x in stack[0]:
printer(x, None, 0)
# Example data file
'''
window
notebook
horizontal
vertical
cd /tmp; echo foo
cd /tmp; echo bar
cd /tmp; echo baz
horizontal
echo lorem;
window
horizontal
echo new window;
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment