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