]> git.sur5r.net Git - i3/i3/blob - contrib/dump-asy.pl
dump-asy.pl: Add POD usage
[i3/i3] / contrib / dump-asy.pl
1 #!/usr/bin/env perl
2 # vim:ts=4:sw=4:expandtab
3
4 use strict;
5 use warnings;
6 use Data::Dumper;
7 use Getopt::Long;
8 use Pod::Usage;
9 use AnyEvent::I3;
10 use File::Temp;
11 use File::Basename;
12 use v5.10;
13 use IPC::Cmd qw[can_run];
14
15 my %options = (
16     help => 0,
17 );
18 my $result = GetOptions(
19     "help|?" => \$options{help},
20 );
21
22 pod2usage(-verbose => 2, -exitcode => 0) if $options{help};
23
24 # prerequisites check so we can be specific about failures caused
25 # by not having these tools in the path
26 can_run('asy') or die 'Please install asymptote';
27 can_run('gv') or die 'Please install gv';
28
29 my $i3 = i3();
30
31 my $tree = $i3->get_tree->recv;
32
33 my $tmp = File::Temp->new(UNLINK => 0, SUFFIX => '.asy');
34
35 say $tmp "import drawtree;";
36
37 say $tmp "treeLevelStep = 2cm;";
38
39 sub dump_node {
40         my ($n, $parent) = @_;
41
42     my $o = ($n->{orientation} eq 'none' ? "u" : ($n->{orientation} eq 'horizontal' ? "h" : "v"));
43     my $w = (defined($n->{window}) ? $n->{window} : "N");
44     my $na = ($n->{name} or "[Empty]");
45     $na =~ s/#/\\#/g;
46     $na =~ s/\$/\\\$/g;
47     $na =~ s/&/\\&/g;
48     $na =~ s/_/\\_/g;
49     $na =~ s/~/\\textasciitilde{}/g;
50     my $type = 'leaf';
51     if (!defined($n->{window})) {
52         $type = $n->{layout};
53     }
54     my $name = qq|``$na'' ($type)|;
55
56     print $tmp "TreeNode n" . $n->{id} . " = makeNode(";
57
58     print $tmp "n" . $parent->{id} . ", " if defined($parent);
59     print $tmp "\"" . $name . "\");\n";
60
61         dump_node($_, $n) for @{$n->{nodes}};
62 }
63
64 sub find_node_with_name {
65     my ($node, $name) = @_;
66
67     return $node if ($node->{name} eq $name);
68     for my $child (@{$node->{nodes}}) {
69         my $res = find_node_with_name($child, $name);
70         return $res if defined($res);
71     }
72     return undef;
73 }
74
75 my $start = shift;
76 my $root;
77 if ($start) {
78     # Find the specified node in the tree
79     $root = find_node_with_name($tree, $start);
80 } else {
81     $root = $tree;
82 }
83 dump_node($root);
84 say $tmp "draw(n" . $root->{id} . ", (0, 0));";
85
86 close($tmp);
87 my $rep = "$tmp";
88 $rep =~ s/asy$/eps/;
89 my $tmp_dir = dirname($rep);
90 system("cd $tmp_dir && asy $tmp && gv --scale=-1000 --noresize --widgetless $rep && rm $rep");
91
92 __END__
93
94 =head1 NAME
95
96 dump-asy.pl - Render the layout tree using asymptote
97
98 =head1 SYNOPSIS
99
100 dump-asy.pl [workspace]
101
102 =head1 EXAMPLE
103
104 Render the entire tree, run:
105
106   ./dump-asy.pl
107
108 Render the tree starting from the node with the specified name, run:
109
110   ./dump-asy.pl 'name'