]> git.sur5r.net Git - i3/i3/blobdiff - contrib/dump-asy.pl
dump-asy.pl: Add marks
[i3/i3] / contrib / dump-asy.pl
index 5af9bb42203c7ff0b4fe98943254553586c8b112..636b20ce3a3f3324b6712efa3e04de1d4362c255 100755 (executable)
@@ -1,13 +1,26 @@
 #!/usr/bin/env perl
 # vim:ts=4:sw=4:expandtab
 # renders the layout tree using asymptote
+#
+# ./dump-asy.pl
+#   will render the entire tree
+# ./dump-asy.pl 'name'
+#   will render the tree starting from the node with the specified name,
+#   e.g. ./dump-asy.pl 2 will render workspace 2 and below
 
 use strict;
 use warnings;
 use Data::Dumper;
 use AnyEvent::I3;
 use File::Temp;
+use File::Basename;
 use v5.10;
+use IPC::Cmd qw[can_run];
+
+# prerequisites check so we can be specific about failures caused
+# by not having these tools in the path
+can_run('asy') or die 'Please install asymptote';
+can_run('gv') or die 'Please install gv';
 
 my $i3 = i3();
 
@@ -24,9 +37,18 @@ sub dump_node {
 
     my $o = ($n->{orientation} eq 'none' ? "u" : ($n->{orientation} eq 'horizontal' ? "h" : "v"));
     my $w = (defined($n->{window}) ? $n->{window} : "N");
-    my $na = $n->{name};
+    my $na = ($n->{name} or "[Empty]");
     $na =~ s/#/\\#/g;
-    my $name = "($na, $o, $w)";
+    $na =~ s/\$/\\\$/g;
+    $na =~ s/&/\\&/g;
+    $na =~ s/_/\\_/g;
+    $na =~ s/~/\\textasciitilde{}/g;
+    my $type = 'leaf';
+    if (!defined($n->{window})) {
+        $type = $n->{layout};
+    }
+    my $marks = $n->{marks} ? ' [' . join('][', @{$n->{marks}}) . ']' : '';
+    my $name = qq|``$na'' ($type)$marks|;
 
     print $tmp "TreeNode n" . $n->{id} . " = makeNode(";
 
@@ -36,10 +58,30 @@ sub dump_node {
        dump_node($_, $n) for @{$n->{nodes}};
 }
 
-dump_node($tree);
-say $tmp "draw(n" . $tree->{id} . ", (0, 0));";
+sub find_node_with_name {
+    my ($node, $name) = @_;
+
+    return $node if ($node->{name} eq $name);
+    for my $child (@{$node->{nodes}}) {
+        my $res = find_node_with_name($child, $name);
+        return $res if defined($res);
+    }
+    return undef;
+}
+
+my $start = shift;
+my $root;
+if ($start) {
+    # Find the specified node in the tree
+    $root = find_node_with_name($tree, $start);
+} else {
+    $root = $tree;
+}
+dump_node($root);
+say $tmp "draw(n" . $root->{id} . ", (0, 0));";
 
 close($tmp);
 my $rep = "$tmp";
 $rep =~ s/asy$/eps/;
-system("cd /tmp && asy $tmp && gv $rep && rm $rep");
+my $tmp_dir = dirname($rep);
+system("cd $tmp_dir && asy $tmp && gv --scale=-1000 --noresize --widgetless $rep && rm $rep");