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 $marks = $n->{marks} ? ' [' . join('][', @{$n->{marks}}) . ']' : '';
60 my $name = qq|``$na'' ($type)$marks|;
62 print $tmp "TreeNode n" . $n->{id} . " = makeNode(";
64 print $tmp "n" . $parent->{id} . ", " if defined($parent);
65 print $tmp "\"" . $name . "\");\n";
67 dump_node($_, $n) for @{$n->{nodes}};
70 sub find_node_with_name {
71 my ($node, $name) = @_;
73 return $node if ($node->{name} eq $name);
74 for my $child (@{$node->{nodes}}) {
75 my $res = find_node_with_name($child, $name);
76 return $res if defined($res);
84 # Find the specified node in the tree
85 $root = find_node_with_name($tree, $start);
90 say $tmp "draw(n" . $root->{id} . ", (0, 0));";
96 my $tmp_dir = dirname($rep);
97 $options{save} = File::Spec->rel2abs($options{save}) if $options{save};
100 $rep = basename($rep); # Output in current dir.
102 system("asy $tmp"); # Create the .eps file.
103 system("gv --scale=-1000 --noresize --widgetless $rep") if $options{gv};
104 if ($options{save}) {
105 system("mv $rep ${options{save}}");
106 } elsif ($options{gv}) {
115 dump-asy.pl - Render the layout tree using asymptote
119 dump-asy.pl [workspace]
123 Render the entire tree, run:
127 Render the tree starting from the node with the specified name, run:
131 Render the entire tree, save in file 'file.eps' and show using gv, run:
133 ./dump-asy.pl --save file.eps
143 Enable or disable showing the result through gv. If disabled, an .eps file will
144 be saved in the current working directory. Enabled by default.
148 Save result using the specified file-name. If omitted, no file will be saved
149 when using '--gv' or a random name will be used when using '--no-gv'.