2010-04-13 20:20:03 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# vim:ts=4:sw=4:expandtab
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use Data::Dumper;
|
2018-03-14 11:11:53 +01:00
|
|
|
use Getopt::Long;
|
|
|
|
use Pod::Usage;
|
2010-04-13 20:20:03 +02:00
|
|
|
use AnyEvent::I3;
|
|
|
|
use File::Temp;
|
2017-09-27 14:23:32 +02:00
|
|
|
use File::Basename;
|
2010-04-13 20:20:03 +02:00
|
|
|
use v5.10;
|
2018-02-21 22:55:55 +01:00
|
|
|
use IPC::Cmd qw[can_run];
|
|
|
|
|
2018-03-14 11:11:53 +01:00
|
|
|
my %options = (
|
|
|
|
help => 0,
|
|
|
|
);
|
|
|
|
my $result = GetOptions(
|
|
|
|
"help|?" => \$options{help},
|
|
|
|
);
|
|
|
|
|
|
|
|
pod2usage(-verbose => 2, -exitcode => 0) if $options{help};
|
|
|
|
|
2018-02-21 22:55:55 +01:00
|
|
|
# prerequisites check so we can be specific about failures caused
|
|
|
|
# by not having these tools in the path
|
|
|
|
can_run('asy') or die 'Please install asymptote';
|
|
|
|
can_run('gv') or die 'Please install gv';
|
2010-04-13 20:20:03 +02:00
|
|
|
|
2012-11-23 20:28:38 +01:00
|
|
|
my $i3 = i3();
|
2010-04-13 20:20:03 +02:00
|
|
|
|
2010-11-21 21:42:28 +01:00
|
|
|
my $tree = $i3->get_tree->recv;
|
2010-04-13 20:20:03 +02: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-08 00:45:10 +01:00
|
|
|
my $o = ($n->{orientation} eq 'none' ? "u" : ($n->{orientation} eq 'horizontal' ? "h" : "v"));
|
2010-04-13 20:20:03 +02:00
|
|
|
my $w = (defined($n->{window}) ? $n->{window} : "N");
|
2018-02-21 22:55:55 +01:00
|
|
|
my $na = ($n->{name} or "[Empty]");
|
2010-04-13 20:20:03 +02:00
|
|
|
$na =~ s/#/\\#/g;
|
2013-05-01 07:12:33 +02:00
|
|
|
$na =~ s/\$/\\\$/g;
|
|
|
|
$na =~ s/&/\\&/g;
|
2012-11-23 20:29:52 +01:00
|
|
|
$na =~ s/_/\\_/g;
|
|
|
|
$na =~ s/~/\\textasciitilde{}/g;
|
|
|
|
my $type = 'leaf';
|
|
|
|
if (!defined($n->{window})) {
|
2017-09-30 19:17:20 +02:00
|
|
|
$type = $n->{layout};
|
2012-11-23 20:29:52 +01:00
|
|
|
}
|
2013-05-01 07:14:14 +02:00
|
|
|
my $name = qq|``$na'' ($type)|;
|
2010-04-13 20:20:03 +02: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 20:29:52 +01: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 20:20:03 +02:00
|
|
|
|
|
|
|
close($tmp);
|
|
|
|
my $rep = "$tmp";
|
|
|
|
$rep =~ s/asy$/eps/;
|
2017-09-27 14:23:32 +02:00
|
|
|
my $tmp_dir = dirname($rep);
|
|
|
|
system("cd $tmp_dir && asy $tmp && gv --scale=-1000 --noresize --widgetless $rep && rm $rep");
|
2018-03-14 11:11:53 +01:00
|
|
|
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
dump-asy.pl - Render the layout tree using asymptote
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
dump-asy.pl [workspace]
|
|
|
|
|
|
|
|
=head1 EXAMPLE
|
|
|
|
|
|
|
|
Render the entire tree, run:
|
|
|
|
|
|
|
|
./dump-asy.pl
|
|
|
|
|
|
|
|
Render the tree starting from the node with the specified name, run:
|
|
|
|
|
|
|
|
./dump-asy.pl 'name'
|