2010-04-13 14:20:03 -04:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# vim:ts=4:sw=4:expandtab
|
|
|
|
# renders the layout tree using asymptote
|
2012-11-23 14:29:52 -05:00
|
|
|
#
|
|
|
|
# ./dump-asy.pl
|
|
|
|
# will render the entire tree
|
|
|
|
# ./dump-asy.pl 'name'
|
|
|
|
# will render the tree starting from the node with the specified name,
|
|
|
|
# e.g. ./dump-asy.pl 2 will render workspace 2 and below
|
2010-04-13 14:20:03 -04:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Data::Dumper;
|
|
|
|
use AnyEvent::I3;
|
|
|
|
use File::Temp;
|
|
|
|
use v5.10;
|
|
|
|
|
2012-11-23 14:28:38 -05:00
|
|
|
my $i3 = i3();
|
2010-04-13 14:20:03 -04:00
|
|
|
|
2010-11-21 15:42:28 -05:00
|
|
|
my $tree = $i3->get_tree->recv;
|
2010-04-13 14:20:03 -04:00
|
|
|
|
|
|
|
my $tmp = File::Temp->new(UNLINK => 0, SUFFIX => '.asy');
|
|
|
|
|
|
|
|
say $tmp "import drawtree;";
|
|
|
|
|
|
|
|
say $tmp "treeLevelStep = 2cm;";
|
|
|
|
|
|
|
|
sub dump_node {
|
|
|
|
my ($n, $parent) = @_;
|
|
|
|
|
2011-01-07 18:45:10 -05:00
|
|
|
my $o = ($n->{orientation} eq 'none' ? "u" : ($n->{orientation} eq 'horizontal' ? "h" : "v"));
|
2010-04-13 14:20:03 -04:00
|
|
|
my $w = (defined($n->{window}) ? $n->{window} : "N");
|
|
|
|
my $na = $n->{name};
|
|
|
|
$na =~ s/#/\\#/g;
|
2013-05-01 01:12:33 -04:00
|
|
|
$na =~ s/\$/\\\$/g;
|
|
|
|
$na =~ s/&/\\&/g;
|
2012-11-23 14:29:52 -05:00
|
|
|
$na =~ s/_/\\_/g;
|
|
|
|
$na =~ s/~/\\textasciitilde{}/g;
|
|
|
|
my $type = 'leaf';
|
|
|
|
if (!defined($n->{window})) {
|
|
|
|
$type = $n->{orientation} . '-split';
|
|
|
|
}
|
2013-05-01 01:14:14 -04:00
|
|
|
my $name = qq|``$na'' ($type)|;
|
2010-04-13 14:20:03 -04:00
|
|
|
|
|
|
|
print $tmp "TreeNode n" . $n->{id} . " = makeNode(";
|
|
|
|
|
|
|
|
print $tmp "n" . $parent->{id} . ", " if defined($parent);
|
|
|
|
print $tmp "\"" . $name . "\");\n";
|
|
|
|
|
|
|
|
dump_node($_, $n) for @{$n->{nodes}};
|
|
|
|
}
|
|
|
|
|
2012-11-23 14:29:52 -05:00
|
|
|
sub find_node_with_name {
|
|
|
|
my ($node, $name) = @_;
|
|
|
|
|
|
|
|
return $node if ($node->{name} eq $name);
|
|
|
|
for my $child (@{$node->{nodes}}) {
|
|
|
|
my $res = find_node_with_name($child, $name);
|
|
|
|
return $res if defined($res);
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
my $start = shift;
|
|
|
|
my $root;
|
|
|
|
if ($start) {
|
|
|
|
# Find the specified node in the tree
|
|
|
|
$root = find_node_with_name($tree, $start);
|
|
|
|
} else {
|
|
|
|
$root = $tree;
|
|
|
|
}
|
|
|
|
dump_node($root);
|
|
|
|
say $tmp "draw(n" . $root->{id} . ", (0, 0));";
|
2010-04-13 14:20:03 -04:00
|
|
|
|
|
|
|
close($tmp);
|
|
|
|
my $rep = "$tmp";
|
|
|
|
$rep =~ s/asy$/eps/;
|
2012-11-23 14:29:08 -05:00
|
|
|
system("cd /tmp && asy $tmp && gv --scale=-1000 --noresize --widgetless $rep && rm $rep");
|