Skip to content

Instantly share code, notes, and snippets.

@cazacugmihai
Created February 7, 2017 12:47
Show Gist options
  • Save cazacugmihai/c4d04eb3b6a59cbe1d73092bfe5d7347 to your computer and use it in GitHub Desktop.
Save cazacugmihai/c4d04eb3b6a59cbe1d73092bfe5d7347 to your computer and use it in GitHub Desktop.
Layout generator for https://launchpad.net/terminator
# +-----------+-----------+
# | 1_1 | 2_1 |
# | | |
# +-----+-----+-----+-----+
# | 1_2 | 1_3 | 2_2 | 2_3 |
# | | | | |
# +-----+-----+-----+-----+
# | 3_1 | 4_1 |
# +-----------+ |
# | 3_2 +-----+-----+
# +-----------+ 4_2 | 4_3 |
# | 3_3 | | |
# +-----------+-----+-----+
v:
- h:
- v:
- '1_1': {cmd: echo "1_1"}
- h:
- '1_2': {cmd: echo "1_2"}
- '1_3': {cmd: echo "1_3"}
- v:
- '2_1': {cmd: echo "2_1"}
- h:
- '2_2': {cmd: echo "2_2"}
- '2_3': {cmd: echo "2_3"}
- h:
- v:
- '3_1': {cmd: echo "3_1"}
- '3_2': {cmd: echo "3_2"}
- '3_3': {cmd: echo "3_3"}
- v:
- '4_1': {cmd: echo "4_1"}
- h:
- '4_2': {cmd: echo "4_2"}
- '4_3': {cmd: echo "4_3"}
@Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
import java.util.concurrent.atomic.AtomicInteger
import static NodeType.*
enum NodeType {
Window,
HPaned,
VPaned,
Terminal;
static NodeType decode(String key) {
switch(key) {
case 'h': return HPaned
case 'v': return VPaned
default : return Terminal
}
}
}
class Node {
private static final AtomicInteger ID = new AtomicInteger(-1)
final int id = ID.incrementAndGet()
NodeType type
Node parent
int order = 0
String title
String cmd
Boolean fullscreen
Boolean maximized
}
String createLayout(Map yaml) {
Node root = new Node(type: Window, maximized: true, fullscreen: true)
List<Node> layout = [root]
populateLayout(root, yaml, layout)
return layoutToString('gen', layout)
}
void populateLayout(Node root, Map yaml, List<Node> layout, int order = 0) {
yaml.eachWithIndex { k, v, i ->
Node node = new Node(
type: NodeType.decode(k),
parent: root,
order: order + i
)
layout << node
if (node.type == Terminal) {
node.title = v.title
node.cmd = v.cmd
node.fullscreen = v.fullscreen
node.maximized = v.maximized
} else if (v) {
populateLayout(node, v[0], layout, 0)
if (v.size() == 2) {
populateLayout(node, v[1], layout, 1)
} else {
populateLayout(node, [(k): v[1..-1]], layout, 1)
}
}
}
}
String layoutToString(String name, List<Node> nodes) {
String indent = ' '
String indent2 = indent * 2
String indent3 = indent * 3
"$indent[[$name]]\n" +
nodes.collect { Node node ->
List<String> list = []
list << indent2 + '[[[' + (node.type == Terminal ? 'terminal' : 'child') + node.id + ']]]'
list << indent3 + 'type = ' + node.type
list << indent3 + 'order = ' + node.order
list << indent3 + 'parent = ' + (node.parent ? ('child' + node.parent.id) : '""')
if (node.title)
list << indent3 + 'title = ' + node.title
if (node.cmd)
list << indent3 + 'command = ' + node.cmd + '; bash'
if (node.maximized == true)
list << indent3 + 'maximized = True'
if (node.fullscreen == true)
list << indent3 + 'fullscreen = True'
list.join('\n')
}.join('\n')
}
// println new Yaml().dump(yaml)
String config = new File(args ? args[0] : 'config.yaml').text
Map yaml = new Yaml().load(config)
String layout = createLayout(yaml)
println layout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment