From 03c8da0a74463fb9506905685c340572a0b16304 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 2 Jun 2010 17:50:06 +0200 Subject: [PATCH] =?utf8?q?t/lib/i3test.pm:=20refactor=20get=5Fws=20and=20g?= =?utf8?q?et=5Fws=5Fcontent=20using=20List::Util=E2=80=99s=20first?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- testcases/t/lib/i3test.pm | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/testcases/t/lib/i3test.pm b/testcases/t/lib/i3test.pm index da3c1b28..f3a9a2e5 100644 --- a/testcases/t/lib/i3test.pm +++ b/testcases/t/lib/i3test.pm @@ -15,6 +15,7 @@ use X11::XCB::Rect; use X11::XCB::Window; use X11::XCB qw(:all); use AnyEvent::I3; +use List::Util qw(first); # Test::Kit already uses Exporter #use Exporter qw(import); use base 'Exporter'; @@ -60,6 +61,17 @@ sub get_unused_workspace { $tmp } +sub get_ws { + my ($name) = @_; + my $i3 = i3("/tmp/nestedcons"); + my $tree = $i3->get_workspaces->recv; + my @ws = map { @{$_->{nodes}} } @{$tree->{nodes}}; + + # as there can only be one workspace with this name, we can safely + # return the first entry + return first { $_->{name} eq $name } @ws; +} + # # returns the content (== tree, starting from the node of a workspace) # of a workspace. If called in array context, also includes the focus @@ -67,13 +79,8 @@ sub get_unused_workspace { # sub get_ws_content { my ($name) = @_; - my $i3 = i3("/tmp/nestedcons"); - my $tree = $i3->get_workspaces->recv; - my @ws = map { @{$_->{nodes}} } @{$tree->{nodes}}; - my @cons = grep { $_->{name} eq $name } @ws; - # as there can only be one workspace with this name, we can safely - # return the first entry - return wantarray ? ($cons[0]->{nodes}, $cons[0]->{focus}) : $cons[0]->{nodes}; + my $con = get_ws($name); + return wantarray ? ($con->{nodes}, $con->{focus}) : $con->{nodes}; } sub get_focused { @@ -98,13 +105,4 @@ sub get_focused { return $lf; } -sub get_ws { - my ($name) = @_; - my $i3 = i3("/tmp/nestedcons"); - my $tree = $i3->get_workspaces->recv; - my @ws = map { @{$_->{nodes}} } @{$tree->{nodes}}; - my @cons = grep { $_->{name} eq $name } @ws; - return $cons[0]; -} - 1 -- 2.39.5