]> git.sur5r.net Git - i3/i3/blobdiff - i3-save-tree
Merge pull request #1589 from Deiz/fix-center
[i3/i3] / i3-save-tree
index 53d67e993d538de05e351a445a5f29c0773ba5d2..289fd8c6980d4e8aa25cc64d9390947459846ee3 100755 (executable)
@@ -13,11 +13,13 @@ use POSIX qw(locale_h);
 use File::Find;
 use File::Basename qw(basename);
 use File::Temp qw(tempfile);
 use File::Find;
 use File::Basename qw(basename);
 use File::Temp qw(tempfile);
+use List::Util qw(first);
 use Getopt::Long;
 use Pod::Usage;
 use AnyEvent::I3;
 use JSON::XS;
 use List::Util qw(first);
 use Getopt::Long;
 use Pod::Usage;
 use AnyEvent::I3;
 use JSON::XS;
 use List::Util qw(first);
+use Encode qw(decode);
 use v5.10;
 use utf8;
 use open ':encoding(UTF-8)';
 use v5.10;
 use utf8;
 use open ':encoding(UTF-8)';
@@ -40,19 +42,27 @@ my $result = GetOptions(
 
 die "Could not parse command line options" unless $result;
 
 
 die "Could not parse command line options" unless $result;
 
-if (!defined($workspace) && !defined($output)) {
-    die "One of --workspace or --output need to be specified";
-}
-
-unless (defined($workspace) ^ defined($output)) {
+if (defined($workspace) && defined($output)) {
     die "Only one of --workspace or --output can be specified";
 }
 
     die "Only one of --workspace or --output can be specified";
 }
 
+$workspace = decode('utf-8', $workspace);
+$output = decode('utf-8', $output);
+
 my $i3 = i3();
 if (!$i3->connect->recv) {
     die "Could not connect to i3";
 }
 
 my $i3 = i3();
 if (!$i3->connect->recv) {
     die "Could not connect to i3";
 }
 
+sub get_current_workspace {
+    my $current = first { $_->{focused} } @{$i3->get_workspaces->recv};
+    return $current->{name};
+}
+
+if (!defined($workspace) && !defined($output)) {
+    $workspace = get_current_workspace();
+}
+
 sub filter_containers {
     my ($tree, $pred) = @_;
 
 sub filter_containers {
     my ($tree, $pred) = @_;
 
@@ -88,6 +98,7 @@ my %allowed_keys = map { ($_, 1) } qw(
     name
     geometry
     window_properties
     name
     geometry
     window_properties
+    mark
 );
 
 sub strip_containers {
 );
 
 sub strip_containers {
@@ -211,7 +222,7 @@ my $tree = $i3->get_tree->recv;
 my $dump;
 if (defined($workspace)) {
     $dump = filter_containers($tree, sub {
 my $dump;
 if (defined($workspace)) {
     $dump = filter_containers($tree, sub {
-        $_->{type} eq 'workspace' && $_->{name} eq $workspace
+        $_->{type} eq 'workspace' && ($_->{name} eq $workspace || ($workspace =~ /^\d+$/ && $_->{num} eq $workspace))
     });
 } else {
     $dump = filter_containers($tree, sub {
     });
 } else {
     $dump = filter_containers($tree, sub {
@@ -241,7 +252,7 @@ for my $key (qw(nodes floating_nodes)) {
 
 =head1 SYNOPSIS
 
 
 =head1 SYNOPSIS
 
-    i3-save-tree [--workspace=name] [--output=name]
+    i3-save-tree [--workspace=name|number] [--output=name]
 
 =head1 DESCRIPTION
 
 
 =head1 DESCRIPTION
 
@@ -254,19 +265,20 @@ specification. When a window is mapped (made visible on the screen) that
 matches the specification, i3 will put it into that place and kill the
 placeholder.
 
 matches the specification, i3 will put it into that place and kill the
 placeholder.
 
+If neither argument is specified, the currently focused workspace will be used.
+
 =head1 OPTIONS
 
 =over
 
 =head1 OPTIONS
 
 =over
 
-=item B<--workspace=name>
+=item B<--workspace=name|number>
 
 
-Specifies the workspace that should be dumped, e.g. 1. Either this or --output
-need to be specified.
+Specifies the workspace that should be dumped, e.g. 1. This can either be a
+name or the number of a workspace.
 
 =item B<--output=name>
 
 
 =item B<--output=name>
 
-Specifies the output that should be dumped, e.g. LVDS-1. Either this or
---workspace need to be specified.
+Specifies the output that should be dumped, e.g. LVDS-1.
 
 =back
 
 
 =back