]> git.sur5r.net Git - i3/i3/blobdiff - contrib/dump-asy.pl
Merge pull request #3176 from orestisf1993/dump-asy-marks
[i3/i3] / contrib / dump-asy.pl
index 54a1e490f2ac5124097d51ec57f3d878dac339c9..2c244752695cf645bc1b16adde28c8cf3795f791 100755 (executable)
@@ -8,14 +8,19 @@ use Getopt::Long;
 use Pod::Usage;
 use AnyEvent::I3;
 use File::Temp;
+use File::Spec;
 use File::Basename;
 use v5.10;
 use IPC::Cmd qw[can_run];
 
 my %options = (
+    gv => 1,
+    save => undef,
     help => 0,
 );
 my $result = GetOptions(
+    "gv!" => \$options{gv},
+    "save=s" => \$options{save},
     "help|?" => \$options{help},
 );
 
@@ -24,7 +29,7 @@ pod2usage(-verbose => 2, -exitcode => 0) if $options{help};
 # 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';
+can_run('gv') or die 'Please install gv' unless !$options{gv};
 
 my $i3 = i3();
 
@@ -51,7 +56,8 @@ sub dump_node {
     if (!defined($n->{window})) {
         $type = $n->{layout};
     }
-    my $name = qq|``$na'' ($type)|;
+    my $marks = $n->{marks} ? ' [' . join('][', @{$n->{marks}}) . ']' : '';
+    my $name = qq|``$na'' ($type)$marks|;
 
     print $tmp "TreeNode n" . $n->{id} . " = makeNode(";
 
@@ -86,8 +92,21 @@ say $tmp "draw(n" . $root->{id} . ", (0, 0));";
 close($tmp);
 my $rep = "$tmp";
 $rep =~ s/asy$/eps/;
-my $tmp_dir = dirname($rep);
-system("cd $tmp_dir && asy $tmp && gv --scale=-1000 --noresize --widgetless $rep && rm $rep");
+if ($options{gv}) {
+    my $tmp_dir = dirname($rep);
+    $options{save} = File::Spec->rel2abs($options{save}) if $options{save};
+    chdir($tmp_dir);
+} else {
+    $rep = basename($rep);  # Output in current dir.
+}
+system("asy $tmp");  # Create the .eps file.
+system("gv --scale=-1000 --noresize --widgetless $rep") if $options{gv};
+if ($options{save}) {
+    system("mv $rep ${options{save}}");
+} elsif ($options{gv}) {
+    system("rm $rep");
+}
+system("rm $tmp");
 
 __END__
 
@@ -108,3 +127,25 @@ Render the entire tree, run:
 Render the tree starting from the node with the specified name, run:
 
   ./dump-asy.pl 'name'
+
+Render the entire tree, save in file 'file.eps' and show using gv, run:
+
+  ./dump-asy.pl --save file.eps
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<--gv>
+
+=item B<--no-gv>
+
+Enable or disable showing the result through gv. If disabled, an .eps file will
+be saved in the current working directory. Enabled by default.
+
+=item B<--save>
+
+Save result using the specified file-name. If omitted, no file will be saved
+when using '--gv' or a random name will be used when using '--no-gv'.
+
+=back