2 # vim:ts=4:sw=4:expandtab
14 use IPC::Cmd qw[can_run];
21 my $result = GetOptions(
22 "gv!" => \$options{gv},
23 "save=s" => \$options{save},
24 "help|?" => \$options{help},
27 pod2usage(-verbose => 2, -exitcode => 0) if $options{help};
29 # prerequisites check so we can be specific about failures caused
30 # by not having these tools in the path
31 can_run('asy') or die 'Please install asymptote';
32 can_run('gv') or die 'Please install gv' unless !$options{gv};
36 my $tree = $i3->get_tree->recv;
38 my $tmp = File::Temp->new(UNLINK => 0, SUFFIX => '.asy');
40 say $tmp "import drawtree;";
42 say $tmp "treeLevelStep = 2cm;";
45 my ($n, $parent) = @_;
47 my $o = ($n->{orientation} eq 'none' ? "u" : ($n->{orientation} eq 'horizontal' ? "h" : "v"));
48 my $w = (defined($n->{window}) ? $n->{window} : "N");
49 my $na = ($n->{name} or "[Empty]");
54 $na =~ s/~/\\textasciitilde{}/g;
56 if (!defined($n->{window})) {
59 my $name = qq|``$na'' ($type)|;
61 print $tmp "TreeNode n" . $n->{id} . " = makeNode(";
63 print $tmp "n" . $parent->{id} . ", " if defined($parent);
64 print $tmp "\"" . $name . "\");\n";
66 dump_node($_, $n) for @{$n->{nodes}};
69 sub find_node_with_name {
70 my ($node, $name) = @_;
72 return $node if ($node->{name} eq $name);
73 for my $child (@{$node->{nodes}}) {
74 my $res = find_node_with_name($child, $name);
75 return $res if defined($res);
83 # Find the specified node in the tree
84 $root = find_node_with_name($tree, $start);
89 say $tmp "draw(n" . $root->{id} . ", (0, 0));";
95 my $tmp_dir = dirname($rep);
96 $options{save} = File::Spec->rel2abs($options{save}) if $options{save};
99 $rep = basename($rep); # Output in current dir.
101 system("asy $tmp"); # Create the .eps file.
102 system("gv --scale=-1000 --noresize --widgetless $rep") if $options{gv};
103 if ($options{save}) {
104 system("mv $rep ${options{save}}");
105 } elsif ($options{gv}) {
114 dump-asy.pl - Render the layout tree using asymptote
118 dump-asy.pl [workspace]
122 Render the entire tree, run:
126 Render the tree starting from the node with the specified name, run:
130 Render the entire tree, save in file 'file.eps' and show using gv, run:
132 ./dump-asy.pl --save file.eps
142 Enable or disable showing the result through gv. If disabled, an .eps file will
143 be saved in the current working directory. Enabled by default.
147 Save result using the specified file-name. If omitted, no file will be saved
148 when using '--gv' or a random name will be used when using '--no-gv'.