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 ($n->{type} eq "floating_con" ? "[Floating con]" : "[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}};
68 dump_node($_, $n) for @{$n->{floating_nodes}};
71 sub find_node_with_name {
72 my ($node, $name) = @_;
74 return $node if ($node->{name} eq $name);
75 for my $child (@{$node->{nodes}}) {
76 my $res = find_node_with_name($child, $name);
77 return $res if defined($res);
85 # Find the specified node in the tree
86 $root = find_node_with_name($tree, $start);
91 say $tmp "draw(n" . $root->{id} . ", (0, 0));";
97 my $tmp_dir = dirname($rep);
98 $options{save} = File::Spec->rel2abs($options{save}) if $options{save};
101 $rep = basename($rep); # Output in current dir.
103 system("asy $tmp"); # Create the .eps file.
104 system("gv --scale=-1000 --noresize --widgetless $rep") if $options{gv};
105 if ($options{save}) {
106 system("mv $rep ${options{save}}");
107 } elsif ($options{gv}) {
116 dump-asy.pl - Render the layout tree using asymptote
120 dump-asy.pl [workspace]
124 Render the entire tree, run:
128 Render the tree starting from the node with the specified name, run:
132 Render the entire tree, save in file 'file.eps' and show using gv, run:
134 ./dump-asy.pl --save file.eps
144 Enable or disable showing the result through gv. If disabled, an .eps file will
145 be saved in the current working directory. Enabled by default.
149 Save result using the specified file-name. If omitted, no file will be saved
150 when using '--gv' or a random name will be used when using '--no-gv'.