blib/lib/Desktop/Workspace/Util.pm

filter: Statementstmt 13.7% 42 / 306 Branchbran 0.0% 0 / 166 Conditioncond 0.0% 0 / 81 MC/DCmcdc 0.0% 0 / 46 Subroutinesub 56.2% 9 / 16 Podpod 100% 3 / 3 totaltotal 8.7% 54 / 618 CC 126 SCAR 75.5
CC
126
Cov
8%
list_desktop_workspace_items
3306.0
__ANON__
1722.0
_items2windows
462.0
CRAP 1909.2
↑ Summary
1 package Desktop::Workspace::Util;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Perinci::Sub::Util qw(gen_modified_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 1 our $DATE = '2026-05-04'; # DATE
15 1 our $DIST = 'Desktop-Workspace-Util'; # DIST
16 1 our $VERSION = '0.004'; # VERSION
17  
18 1 our @EXPORT_OK = qw(
19 get_desktop_workspace_module
20 instantiate_desktop_workspace_module
21 list_desktop_workspace_items
22 open_desktop_workspace_items
23 );
24  
25 1 our %SPEC;
26  
27 1 our %argspec0_module = (
28 module => {
29 schema => 'perl::modname*',
30 req => 1,
31 pos => 0,
32 },
33 );
34  
35 1 our %argspecopt_ns_prefixes = (
36 ns_prefixes => {
37 'x.name.is_plural' => 1,
38 'x.name.singular' => 'ns_prefix',
39 summary => 'List of namespaces to search for a Desktop Workspace specification modules',
40 schema => ['array*', of=>['any*', of=>['perl::modname', ['str', in=>[""]]]]],
41 default => ['DesktopWorkspace', ''],
42 },
43 );
44  
45 1 our %argspecs_module = (
46 %argspec0_module,
47 %argspecopt_ns_prefixes,
48 );
49  
50 1 our %argspecopt_module_args = (
51 module_args => {
52 schema => 'hash*',
53 },
54 );
55  
56 1 $SPEC{':package'} = {
57 v => 1.1,
58 summary => 'Utilities related to DesktopWorkspace',
59 };
60  
61 $SPEC{get_desktop_workspace_module} = {
62 1 v => 1.1,
63 summary => 'Get the first Perl desktop workspace specification module',
64 args => {
65 %argspecs_module,
66 },
67 result_naked => 1,
68 };
69 0 ▶sub get_desktop_workspace_module {
Subroutine get_desktop_workspace_module: not called CC=7 SCAR=40.3
Pod documented
70 0 my %args = @_;
71  
72 0 ▶ my $module = $args{module} or die "Please specify 'module'";
Branch: 1 branch
BranchTrueFalseTotal
my $module = $args{"module"} or die "Please specify 'module'" 0 0 0
73 0 ▶ my $ns_prefixes = $args{ns_prefixes} // ["DesktopWorkspace"];
Condition0%0/2
$args{"ns_prefixes"} // ["DesktopWorkspace"]
l!l
00
MC/DC0%0/1
$args{"ns_prefixes"} // ["DesktopWorkspace"]
$args{"ns_prefixes"}
Truth table0%0/2
A
A= $args{"ns_prefixes"}
Aresult
11
00
74  
75 0 ▶ push @$ns_prefixes, "" unless @$ns_prefixes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ns_prefixes 0 0 0
76  
77 0 for my $ns_prefix (@$ns_prefixes) {
78 0 ▶ my $mod = (length($ns_prefix) ? "$ns_prefix\::" : "") . $module;
Branch: 1 branch
BranchTrueFalseTotal
length $ns_prefix ? : 0 0 0
79 0 (my $mod_pm = "$mod.pm") =~ s!::!/!g;
80 0 ▶ if (eval { require $mod_pm; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require $mod_pm; 1 } }) 0 0 0
81 0 return $mod;
82 }
83 }
84 0 die "Can't find desktop workspace specification module '$module' (searched in ". join(", ", @$ns_prefixes).")";
85 }
86  
87 $SPEC{instantiate_desktop_workspace_module} = {
88 1 v => 1.1,
89 summary => 'Instantiate the desktop workspace specification module (class)',
90 args => {
91 %argspecs_module,
92 %argspecopt_module_args,
93 },
94 result_naked => 1,
95 };
96 0 ▶sub instantiate_desktop_workspace_module {
Subroutine instantiate_desktop_workspace_module: not called CC=2 SCAR=17.9
Pod documented
97 0 my %args = @_;
98  
99 my $mod = get_desktop_workspace_module(
100 0 module => $args{module}, ns_prefixes => $args{ns_prefixes});
101 0 ▶ $mod->new(%{ $args{module_args} // {} });
Condition0%0/2
$args{"module_args"} // {}
l!l
00
MC/DC0%0/1
$args{"module_args"} // {}
$args{"module_args"}
Truth table0%0/2
A
A= $args{"module_args"}
Aresult
11
00
102 }
103  
104 0 ▶sub _find_window {
Subroutine _find_window: not called CC=3 SCAR=24.8
105 0 my ($re, $all_windows) = @_;
106 0 log_trace "Checking existing window (%s) ...", $re;
107 0 for my $row (@$all_windows) {
108 0 ▶ if ($row->{title} =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{"title"} =~ /$re/) 0 0 0
109 0 log_trace "Found matching window %s", $row;
110 0 return $row;
111 }
112 }
113 0 return;
114 }
115  
116 0 ▶sub _items2windows {
Subroutine _items2windows: not called CC=21 SCAR=61.4
117 0 require Desktop::XWindowManager::Util;
118  
119 0 my $items = shift;
120  
121 0 my $windowkey;
122 0 my $auto_file_num = 0;
123 0 my $auto_prog_num = 0;
124 0 my $auto_firefox_num = 0;
125 0 my $auto_dolphin_num = 0;
126 0 my %windows; # key = prefix+arbitrary number
127  
128 my $all_windows;
129 {
130 0 my $res = Desktop::XWindowManager::Util::list_xwm_windows(
131 detail=>1,
132 with_kde_activity_name => 1,
133 );
134 0 ▶ return [500, "Can't list windows: $res->[0] - $res->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
135 unless $res->[0] == 200;
136 0 $all_windows = $res->[2];
137 }
138  
139 ITEM:
140 0 for my $item (@$items) {
141 0 ▶ if (defined($item->{url}) || defined($item->{dir})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $item->{'url'} or defined $item->{'dir'}) { } 0 0 0
elsif (defined $item->{'file'}) { } 0 0 0
elsif (defined $item->{'prog_name'} or defined $item->{'prog_path'}) { } 0 0 0
Condition0%0/3
defined $item->{"url"} or defined $item->{"dir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $item->{"url"} or defined $item->{"dir"}
defined $item->{"url"} defined $item->{"dir"}
Truth table0%0/3
A or B
A= defined $item->{"url"}B= defined $item->{"dir"}
ABresult
1X1
011
000
142 0 ▶ my $url = $item->{url} // $item->{dir};
Condition0%0/3
$item->{"url"} // $item->{"dir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$item->{"url"} // $item->{"dir"}
$item->{"url"} $item->{"dir"}
Truth table0%0/3
A // B
A= $item->{"url"}B= $item->{"dir"}
ABresult
1X1
011
000
143 0 ▶ if (defined $item->{dolphin_window_num}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $item->{'dolphin_window_num'}) { } 0 0 0
elsif (defined $item->{'firefox_window_num'}) { } 0 0 0
144 0 $windowkey = "dolphin_".sprintf("%04d", $item->{dolphin_window_num});
145 } elsif (defined $item->{firefox_window_num}) {
146 0 $windowkey = "firefox_".sprintf("%04d", $item->{firefox_window_num});
147 } else {
148 0 my ($app, $num);
149 0 ▶ if ($item->{new_browser_window}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($item->{'new_browser_window'}) { } 0 0 0
elsif ($item->{'new_file_manager_window'}) { } 0 0 0
150 0 $app = "firefox";
151 0 $num = ++$auto_firefox_num;
152 } elsif ($item->{new_file_manager_window}) {
153 0 $app = "dolphin";
154 0 $num = ++$auto_dolphin_num;
155 } else {
156 0 ▶ if (defined($item->{url})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{'url'}) { } 0 0 0
157 0 $app = "firefox";
158 0 $num = $auto_firefox_num;
159 } else {
160 0 $app = "dolphin";
161 0 $num = $auto_dolphin_num;
162 }
163 }
164 0 $windowkey = "${app}_auto_".sprintf("%04d", $num);
165 }
166 } elsif (defined $item->{file}) {
167 0 $windowkey = "file_" . (++$auto_file_num);
168 ▶ } elsif (defined $item->{prog_name} or defined $item->{prog_path}) {
Condition0%0/3
defined $item->{"prog_name"} or defined $item->{"prog_path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $item->{"prog_name"} or defined $item->{"prog_path"}
defined $item->{"prog_name"} defined $item->{"prog_path"}
Truth table0%0/3
A or B
A= defined $item->{"prog_name"}B= defined $item->{"prog_path"}
ABresult
1X1
011
000
169 0 $windowkey = "prog_" . (++$auto_prog_num);
170 }
171  
172 0 ▶ $windows{$windowkey} //= { items=>[] };
Condition0%0/2
$windows{$windowkey} //= {"items", []}
l!l
00
MC/DC0%0/1
$windows{$windowkey} //= {"items", []}
$windows{$windowkey}
Truth table0%0/2
A
A= $windows{$windowkey}
Aresult
11
00
173 0 push @{ $windows{$windowkey}{items} }, $item;
174 } # for item
175  
176 CHECK_EXISTING_WINDOWS: {
177 0 for my $windowkey (sort keys %windows) {
178 0 my $window = $windows{$windowkey};
179 0 my $re = $window->{items}[-1]{window_title_re};
180 0 ▶ if (!$re && $windowkey =~ /^dolphin/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $re and $windowkey =~ /^dolphin/) 0 0 0
Condition0%0/3
not $re and $windowkey =~ /^dolphin/
!ll&&!rl&&r
000
MC/DC0%0/2
not $re and $windowkey =~ /^dolphin/
not $re $windowkey =~ /^dolphin/
Truth table0%0/3
not(A) and B
A= not $reB= $windowkey =~ /^dolphin/
ABresult
0X0
100
111
181 0 ▶ my $url = $window->{items}[-1]{url} // $window->{items}[-1]{dir};
Condition0%0/3
$window->{"items"}[-1]{"url"} // $window->{"items"}[-1]{"dir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$window->{"items"}[-1]{"url"} // $window->{"items"}[-1]{"dir"}
$window->{"items"}[-1]{"url"} $window->{"items"}[-1]{"dir"}
Truth table0%0/3
A // B
A= $window->{"items"}[-1]{"url"}B= $window->{"items"}[-1]{"dir"}
ABresult
1X1
011
000
182 0 $re = qr/\Q$url\E — Dolphin$/;
183 }
184 0 ▶ if ($re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re) 0 0 0
185 0 my $w = _find_window($re, $all_windows);
186 0 ▶ if ($w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 0 0 0
187 0 $window->{existing_window_id} = $w->{id};
188 0 $window->{existing_window_kde_activity_name} = $w->{kde_activity_name};
189 }
190 }
191 }
192 }
193  
194 0 \%windows;
195 }
196  
197 $SPEC{list_desktop_workspace_items} = {
198 1 v => 1.1,
199 summary => 'List the items from desktop workspace specification module, with filtering options',
200 args => {
201 %argspecs_module,
202 %argspecopt_module_args,
203  
204 all => {
205 summary => 'Whether to include items that are not included by default (has property `include_by_default`=0)',
206 schema => 'bool*',
207 tags => ['category:filtering'],
208 },
209 include_any_tags => {
210 summary => 'Include all items that have any tag specified',
211 schema => ['array*', of=>'str*'],
212 tags => ['category:filtering'],
213 },
214 include_all_tags => {
215 summary => 'Include all items that have ALL tags specified',
216 schema => ['array*', of=>'str*'],
217 tags => ['category:filtering'],
218 },
219 exclude_any_tags => {
220 summary => 'Exclude all items that have any tags specified',
221 schema => ['array*', of=>'str*'],
222 tags => ['category:filtering'],
223 },
224 exclude_all_tags => {
225 summary => 'Exclude all items that have ALL tags specified',
226 schema => ['array*', of=>'str*'],
227 tags => ['category:filtering'],
228 },
229 include_url => {
230 summary => 'Whether to include URL items',
231 schema => ['bool*'],
232 tags => ['category:filtering'],
233 },
234 include_file => {
235 summary => 'Whether to include file items',
236 schema => ['bool*'],
237 tags => ['category:filtering'],
238 },
239 include_dir => {
240 summary => 'Whether to include dir items',
241 schema => ['bool*'],
242 tags => ['category:filtering'],
243 },
244 include_prog => {
245 summary => 'Whether to include program items',
246 schema => ['bool*'],
247 tags => ['category:filtering'],
248 },
249 query => {
250 schema => ['array*', of=>'str*'],
251 pos => 1,
252 slurpy => 1,
253 tags => ['category:filtering'],
254 },
255  
256 detail => {
257 schema => 'bool*',
258 cmdline_aliases => {l=>{}},
259 tags => ['category:result'],
260 },
261 shuffle => {
262 schema => 'bool*',
263 tags => ['category:result'],
264 },
265 },
266 };
267 0 ▶sub list_desktop_workspace_items {
Subroutine list_desktop_workspace_items: not called CC=57 SCAR=81.0
Pod documented
268 0 my %args = @_;
269  
270 my $obj = instantiate_desktop_workspace_module(
271 module => $args{module},
272 ns_prefixes => $args{ns_prefixes},
273 module_args => $args{module_args},
274 0 );
275  
276 0 my $items = $obj->items;
277  
278 0 ▶ if ($args{shuffle}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"shuffle"}) 0 0 0
279 0 require List::Util;
280 0 $items = [List::Util::shuffle(@$items)];
281 }
282  
283 0 require List::Util::Find;
284 0 my @filtered_items;
285 ITEM:
286 0 for my $i (0 .. $#{$items}) {
287 0 my $item = $items->[$i];
288  
289 # if not included by default, will be included only if specifically matching a filter
290 my $include_by_default = $args{all} ? 1 :
291 0 ▶ ($item->{include_by_default} // 1);
Branch: 1 branch
BranchTrueFalseTotal
$args{'all'} ? : 0 0 0
Condition0%0/2
$item->{"include_by_default"} // 1
l!l
00
MC/DC0%0/1
$item->{"include_by_default"} // 1
$item->{"include_by_default"}
Truth table0%0/2
A
A= $item->{"include_by_default"}
Aresult
11
00
292  
293 0 my $match_a_filter = 0;
294  
295 FILTER: {
296 0 INCLUDE_ANY_TAGS: {
297 0 ▶ last unless $args{include_any_tags} && @{ $args{include_any_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"include_any_tags"} and @{$args{"include_any_tags"};} 0 0 0
Condition0%0/3
$args{"include_any_tags"} and @{$args{"include_any_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"include_any_tags"} and @{$args{"include_any_tags"};}
$args{"include_any_tags"} @{$args{"include_any_tags"};}
Truth table0%0/3
A and B
A= $args{"include_any_tags"}B= @{$args{"include_any_tags"};}
ABresult
0X0
100
111
298 0 do { log_debug "Skipping item %s: does not pass include_any_tags %s", $item, $args{include_any_tags}; next ITEM }
299 0 ▶ unless List::Util::Find::hasanystrs($args{include_any_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
unless (List::Util::Find::hasanystrs($args{"include_any_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
300 0 $match_a_filter++;
301 }
302 INCLUDE_ALL_TAGS: {
303 0 ▶ last unless $args{include_all_tags} && @{ $args{include_all_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"include_all_tags"} and @{$args{"include_all_tags"};} 0 0 0
Condition0%0/3
$args{"include_all_tags"} and @{$args{"include_all_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"include_all_tags"} and @{$args{"include_all_tags"};}
$args{"include_all_tags"} @{$args{"include_all_tags"};}
Truth table0%0/3
A and B
A= $args{"include_all_tags"}B= @{$args{"include_all_tags"};}
ABresult
0X0
100
111
304 0 do { log_debug "Skipping item %s: does not pass include_all_tags %s", $item, $args{include_all_tags}; next ITEM }
305 0 ▶ unless List::Util::Find::hasallstrs($args{include_all_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
unless (List::Util::Find::hasallstrs($args{"include_all_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
306 0 $match_a_filter++;
307 }
308 EXCLUDE_ANY_TAGS: {
309 0 ▶ last unless $args{exclude_any_tags} && @{ $args{exclude_any_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"exclude_any_tags"} and @{$args{"exclude_any_tags"};} 0 0 0
Condition0%0/3
$args{"exclude_any_tags"} and @{$args{"exclude_any_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"exclude_any_tags"} and @{$args{"exclude_any_tags"};}
$args{"exclude_any_tags"} @{$args{"exclude_any_tags"};}
Truth table0%0/3
A and B
A= $args{"exclude_any_tags"}B= @{$args{"exclude_any_tags"};}
ABresult
0X0
100
111
310 0 do { log_debug "Skipping item %s: does not pass exclude_any_tags %s", $item, $args{exclude_any_tags}; next ITEM }
311 0 ▶ if List::Util::Find::hasanystrs($args{exclude_any_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
if (List::Util::Find::hasanystrs($args{"exclude_any_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
312 0 $match_a_filter++;
313 }
314 EXCLUDE_ALL_TAGS: {
315 0 ▶ last unless $args{exclude_all_tags} && @{ $args{exclude_all_tags} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"exclude_all_tags"} and @{$args{"exclude_all_tags"};} 0 0 0
Condition0%0/3
$args{"exclude_all_tags"} and @{$args{"exclude_all_tags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"exclude_all_tags"} and @{$args{"exclude_all_tags"};}
$args{"exclude_all_tags"} @{$args{"exclude_all_tags"};}
Truth table0%0/3
A and B
A= $args{"exclude_all_tags"}B= @{$args{"exclude_all_tags"};}
ABresult
0X0
100
111
316 0 do { log_debug "Skipping item %s: does not pass exclude_all_tags %s", $item, $args{exclude_all_tags}; next ITEM }
317 0 ▶ if List::Util::Find::hasallstrs($args{exclude_all_tags}, @{ $item->{tags} // []});
Branch: 1 branch
BranchTrueFalseTotal
if (List::Util::Find::hasallstrs($args{"exclude_all_tags"}, @{$item->{"tags"} // [];})) 0 0 0
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
318 0 $match_a_filter++;
319 }
320 QUERY: {
321 0 ▶ last unless $args{query} && @{ $args{query} };
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"query"} and @{$args{"query"};} 0 0 0
Condition0%0/3
$args{"query"} and @{$args{"query"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"query"} and @{$args{"query"};}
$args{"query"} @{$args{"query"};}
Truth table0%0/3
A and B
A= $args{"query"}B= @{$args{"query"};}
ABresult
0X0
100
111
322 0 my $num_positive_queries = 0;
323 0 my $num_negative_queries = 0;
324 0 my $match = 0;
325 Q:
326 0 for my $query0 (@{ $args{query} }) {
327 0 my ($is_negative, $query) = $query0 =~ /\A(-?)(.*)/;
328 0 ▶ $num_positive_queries++ if !$is_negative;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_negative 0 0 0
329 0 ▶ $num_negative_queries++ if $is_negative;
Branch: 1 branch
BranchTrueFalseTotal
if $is_negative 0 0 0
330  
331 0 ▶ if (defined $item->{url}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"url"}) 0 0 0
332 0 ▶ if ($item->{url} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"url"} =~ /$query/i) 0 0 0
333 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
334 }
335 }
336 0 ▶ if (defined $item->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"file"}) 0 0 0
337 0 ▶ if ($item->{file} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"file"} =~ /$query/i) 0 0 0
338 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
339 }
340 }
341 0 ▶ if (defined $item->{dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"dir"}) 0 0 0
342 0 ▶ if ($item->{dir} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"dir"} =~ /$query/i) 0 0 0
343 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
344 }
345 }
346 0 ▶ if (defined $item->{prog_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"prog_name"}) 0 0 0
347 0 ▶ if ($item->{prog_name} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"prog_name"} =~ /$query/i) 0 0 0
348 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
349 }
350 }
351 0 ▶ if (defined $item->{prog_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"prog_path"}) 0 0 0
352 0 ▶ if ($item->{prog_path} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"prog_path"} =~ /$query/i) 0 0 0
353 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
354 }
355 }
356 0 ▶ for my $tag (@{ $item->{tags} // [] }) {
Condition0%0/2
$item->{"tags"} // []
l!l
00
MC/DC0%0/1
$item->{"tags"} // []
$item->{"tags"}
Truth table0%0/2
A
A= $item->{"tags"}
Aresult
11
00
357 0 ▶ if ($tag =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /$query/i) 0 0 0
358 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
359 }
360 }
361 0 ▶ if (defined $item->{firefox_container}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"firefox_container"}) 0 0 0
362 0 ▶ if ($item->{firefox_container} =~ /$query/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"firefox_container"} =~ /$query/i) 0 0 0
363 0 ▶ if ($is_negative) { goto L1 } else { $match = 1; last Q }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_negative) { } 0 0 0
364 }
365 }
366 } # for query
367 0 ▶ $match++ if $num_positive_queries == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_positive_queries == 0 0 0 0
368 L1:
369 0 ▶ do { log_debug "Skipping item %s: does not pass query %s", $item, $args{query}; next ITEM }
Branch: 1 branch
BranchTrueFalseTotal
unless ($match) 0 0 0
370 unless $match;
371 0 $match_a_filter++;
372 } # QUERY
373 } # FILTER
374  
375 0 ▶ if (!$include_by_default && !$match_a_filter) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($include_by_default or $match_a_filter) 0 0 0
Condition0%0/3
$include_by_default or $match_a_filter
l!l&&r!l&&!r
000
MC/DC0%0/2
$include_by_default or $match_a_filter
$include_by_default $match_a_filter
Truth table0%0/3
A or B
A= $include_by_defaultB= $match_a_filter
ABresult
1X1
011
000
376 0 log_debug "Skipping item %s: not included by default and does not match filter(s)", $item;
377 0 next ITEM;
378 }
379  
380 0 push @filtered_items, $item;
381 } # for item
382  
383 0 ▶ unless ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"detail"}) 0 0 0
384 @filtered_items = map {
385 0 $_->{url} // $_->{file} // $_->{dir} // $_->{prog_name} // $_->{prog_path}
386 0 ▶ } @filtered_items;
Condition0%0/12
((($_->{"url"} // $_->{"file"}) // $_->{"dir"}) // $_->{"prog_name"}) // $_->{"prog_path"}
l!l&&r!l&&!r
000
(($_->{"url"} // $_->{"file"}) // $_->{"dir"}) // $_->{"prog_name"}
l!l&&r!l&&!r
000
($_->{"url"} // $_->{"file"}) // $_->{"dir"}
l!l&&r!l&&!r
000
$_->{"url"} // $_->{"file"}
l!l&&r!l&&!r
000
MC/DC0%0/5
((($_->{"url"} // $_->{"file"}) // $_->{"dir"}) // $_->{"prog_name"}) // $_->{"prog_path"}
$_->{"url"} $_->{"file"} $_->{"dir"} $_->{"prog_name"} $_->{"prog_path"}
Truth table0%0/6
A // B // C // D // E
A= $_->{"url"}B= $_->{"file"}C= $_->{"dir"}D= $_->{"prog_name"}E= $_->{"prog_path"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
387 }
388  
389 0 [200, "OK", \@filtered_items, {
390 'func.obj' => $obj,
391 }];
392 }
393  
394 gen_modified_sub(
395 die => 1,
396 output_name => 'open_desktop_workspace_items',
397 base_name => 'list_desktop_workspace_items',
398 summary => 'Open desktop workspace items',
399 description => <<'MARKDOWN',
400  
401  
402 MARKDOWN
403 add_args => {
404 kde_activity => {
405 summary => 'Switch to the specified KDE activity name',
406 description => <<'MARKDOWN',
407  
408 Override's desktop workspace specification's `kde_activity` property.
409  
410 MARKDOWN
411 schema => 'str*',
412 },
413 reuse_window => {
414 summary => 'Do not open if existing window is detected',
415 schema => 'bool*',
416 },
417 },
418 1 ▶ modify_meta => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
419 1 my $meta = shift;
420  
421 $meta->{features} = {
422 1 dry_run => 1,
423 };
424 $meta->{deps} = {
425 1 all => [
426 {prog => 'dolphin'},
427 {prog => 'firefox-container'},
428 ],
429 };
430  
431 },
432 0 ▶ wrap_code => sub {
Subroutine __ANON__: not called CC=41 SCAR=74.5
433 0 require IPC::System::Options;
434  
435 0 my $orig = shift;
436 0 my %args = @_;
437  
438 0 my $dry_run = $args{-dry_run};
439  
440 0 my $obj;
441 my $items;
442 LIST_ITEMS: {
443 0 my $res = $orig->(%args, detail=>1);
444 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
445 0 return [500, "Can't list desktop workspace items: $res->[0] - $res->[1]"];
446 }
447 0 $items = $res->[2];
448 0 $obj = $res->[3]{'func.obj'};
449 } # LIST_ITEMS
450  
451 0 my $windows = _items2windows($items);
452  
453 WINDOW:
454 0 for my $windowkey (sort keys %$windows) {
455 0 my $window = $windows->{$windowkey};
456 0 my $items = $window->{items};
457  
458 0 my $target_kde_activity;
459 GET_TARGET_KDE_ACTIVITY: {
460 0 ▶ my $item_kde_activity = first {defined $_->{kde_activity}} @$items; $item_kde_activity = $item_kde_activity->{kde_activity} if defined $item_kde_activity;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if defined $item_kde_activity 0 0 0
461 0 ▶ $target_kde_activity = $args{kde_activity} // $item_kde_activity // $obj->{kde_activity};
Condition0%0/6
($args{"kde_activity"} // $item_kde_activity) // $obj->{"kde_activity"}
l!l&&r!l&&!r
000
$args{"kde_activity"} // $item_kde_activity
l!l&&r!l&&!r
000
MC/DC0%0/3
($args{"kde_activity"} // $item_kde_activity) // $obj->{"kde_activity"}
$args{"kde_activity"} $item_kde_activity $obj->{"kde_activity"}
Truth table0%0/4
A // B // C
A= $args{"kde_activity"}B= $item_kde_activityC= $obj->{"kde_activity"}
ABCresult
1XX1
01X1
0011
0000
462 }
463  
464 REUSE_EXISTING_WINDOW: {
465 0 ▶ last unless $window->{existing_window_id};
Branch: 1 branch
BranchTrueFalseTotal
unless $window->{"existing_window_id"} 0 0 0
466 log_trace "Window for items %s already exists (ID %s)%s",
467 $items,
468 $window->{existing_window_id},
469 0 ▶ $args{reuse_window} ? ", reusing it" : "";
Branch: 1 branch
BranchTrueFalseTotal
$args{'reuse_window'} ? : 0 0 0
470 0 ▶ last unless $args{reuse_window};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"reuse_window"} 0 0 0
471 #log_error "D: target kde activity: %s, existing window's kde activity: %s", $target_kde_activity, $window->{existing_window_kde_activity_name};
472 0 ▶ if (defined($window->{existing_window_kde_activity_name}) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $window->{"existing_window_kde_activity_name"} and defined $target_kde_activity and $window->{"existing_window_kde_activity_name"} ne $target_kde_activity) 0 0 0
Condition0%0/6
defined $window->{"existing_window_kde_activity_name"} and defined $target_kde_activity and $window->{"existing_window_kde_activity_name"} ne $target_kde_activity
!ll&&!rl&&r
000
defined $window->{"existing_window_kde_activity_name"} and defined $target_kde_activity
!ll&&!rl&&r
000
MC/DC0%0/3
defined $window->{"existing_window_kde_activity_name"} and defined $target_kde_activity and $window->{"existing_window_kde_activity_name"} ne $target_kde_activity
defined $window->{"existing_window_kde_activity_name"} defined $target_kde_activity $window->{"existing_window_kde_activity_name"} ne $target_kde_activity
Truth table0%0/4
A and B and C
A= defined $window->{"existing_window_kde_activity_name"}B= defined $target_kde_activityC= $window->{"existing_window_kde_activity_name"} ne $target_kde_activity
ABCresult
0XX0
10X0
1100
1111
473 defined($target_kde_activity) &&
474 $window->{existing_window_kde_activity_name} ne $target_kde_activity) {
475 log_trace "%sMoving window ID %s from KDE activity %s to KDE activity %s ...",
476 $args{-dry_run} ? "[DRY-RUN]" : "",
477 $window->{existing_window_id},
478 $window->{existing_window_kde_activity_name},
479 0 ▶ $target_kde_activity;
Branch: 1 branch
BranchTrueFalseTotal
$args{'-dry_run'} ? : 0 0 0
480 0 ▶ last if $args{-dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-dry_run"} 0 0 0
481 0 require Desktop::XWindowManager::Util;
482 my $res = Desktop::XWindowManager::Util::move_windows_to_kde_activity(
483 id => $window->{existing_window_id},
484 0 activity_name => $target_kde_activity,
485 );
486 log_warn "Can't move window ID %s to KDE activity %s: %d - %s",
487 $window->{existing_window_id},
488 0 ▶ $target_kde_activity,
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
489 $res->[0], $res->[1]
490 unless $res->[0] == 200;
491 }
492 0 next WINDOW;
493 } # REUSE_EXISTING_WINDOW
494  
495 SWITCH_KDE_ACTIVITY: {
496 0 ▶ last unless defined $target_kde_activity;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target_kde_activity 0 0 0
497 log_trace "%sSetting KDE activity to %s ...",
498 0 ▶ $args{-dry_run} ? "[DRY-RUN]" : "",
Branch: 1 branch
BranchTrueFalseTotal
$args{'-dry_run'} ? : 0 0 0
499 $target_kde_activity;
500 0 ▶ last if $args{-dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"-dry_run"} 0 0 0
501 0 require Desktop::KDEActivity::Util;
502 0 my $res = Desktop::KDEActivity::Util::set_current_kde_activity(
503 name => $target_kde_activity);
504 0 ▶ return [500, "Can't set current KDE activity: $res->[0] - $res->[1]"]
Branch: 1 branch
BranchTrueFalseTotal
unless $res->[0] == 200 0 0 0
505 unless $res->[0] == 200;
506 }
507  
508 0 ▶ if ($windowkey =~ /^firefox/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($windowkey =~ /^firefox/) { } 0 0 0
elsif ($windowkey =~ /^file_/) { } 0 0 0
elsif ($windowkey =~ /^dolphin_/) { } 0 0 0
elsif ($windowkey =~ /^prog_/) { } 0 0 0
509  
510 0 my $i = 0;
511 0 for my $item (@$items) {
512 0 $i++;
513 0 my $url = $item->{url};
514  
515 0 my @ff_args;
516 my $env;
517 0 ▶ if ($i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) { } 0 0 0
518 0 push @ff_args, "--new-window", $url;
519 } else {
520 0 push @ff_args, $url;
521 }
522  
523 0 ▶ if (defined $item->{firefox_container}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"firefox_container"}) 0 0 0
524 0 $env->{FIREFOX_CONTAINER} = $item->{firefox_container};
525 }
526  
527 log_trace "%sOpening URL %s in firefox (window %s) tab [#%d/%d]%s ...",
528 $dry_run ? "[DRY-RUN]" : "",
529 $url,
530 $windowkey,
531 $i,
532 scalar(@$items),
533 0 ▶ (defined $item->{firefox_container} ? " container $item->{firefox_container}" : "");
Branch: 2 branches
BranchTrueFalseTotal
$dry_run ? : 0 0 0
defined $item->{'firefox_container'} ? : 0 0 0
534 0 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 0 0 0
535 0 IPC::System::Options::system(
536 {env=>$env, log=>1},
537 "firefox-container", @ff_args);
538 }
539 } # for item
540  
541 } elsif ($windowkey =~ /^file_/) {
542  
543 0 my $i = 0;
544 0 for my $item (@$items) {
545 0 $i++;
546 0 my $file = $item->{file};
547 0 ▶ log_trace "%sOpening file %s (window %s) ...",
Branch: 1 branch
BranchTrueFalseTotal
$dry_run ? : 0 0 0
548 $dry_run ? "[DRY-RUN]" : "",
549 $file,
550 $windowkey;
551 0 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 0 0 0
552 0 require Desktop::Open;
553 0 Desktop::Open::open_desktop($file);
554 }
555 } # for item
556  
557 } elsif ($windowkey =~ /^dolphin_/) {
558  
559 0 my $i = 0;
560 0 my @urls;
561 0 for my $item (@$items) {
562 0 $i++;
563 0 ▶ my $url = $item->{dir} // $item->{url};
Condition0%0/3
$item->{"dir"} // $item->{"url"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$item->{"dir"} // $item->{"url"}
$item->{"dir"} $item->{"url"}
Truth table0%0/3
A // B
A= $item->{"dir"}B= $item->{"url"}
ABresult
1X1
011
000
564 0 push @urls, $url;
565 }
566 0 ▶ log_trace "%sOpening dirs %s (window %s) ...",
Branch: 1 branch
BranchTrueFalseTotal
$dry_run ? : 0 0 0
567 $dry_run ? "[DRY-RUN]" : "",
568 \@urls,
569 $windowkey;
570 0 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 0 0 0
571 0 IPC::System::Options::system(
572 {log=>1, shell=>1},
573 "dolphin", "--new-window", @urls, \"&");
574 }
575  
576 } elsif ($windowkey =~ /^prog_/) {
577  
578 0 my $i = 0;
579 0 for my $item (@$items) {
580 0 $i++;
581 0 ▶ my $prog = $item->{prog_name} // $item->{prog_path};
Condition0%0/3
$item->{"prog_name"} // $item->{"prog_path"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$item->{"prog_name"} // $item->{"prog_path"}
$item->{"prog_name"} $item->{"prog_path"}
Truth table0%0/3
A // B
A= $item->{"prog_name"}B= $item->{"prog_path"}
ABresult
1X1
011
000
582 0 ▶ log_trace "%sOpening program %s (window %s) ...",
Branch: 1 branch
BranchTrueFalseTotal
$dry_run ? : 0 0 0
583 $dry_run ? "[DRY-RUN]" : "",
584 $prog,
585 $windowkey;
586 0 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dry_run) 0 0 0
587 IPC::System::Options::system(
588 {log=>1, shell=>1},
589 0 ▶ $prog, ($item->{prog_args} ? @{ $item->{prog_args} } : ()),
Branch: 1 branch
BranchTrueFalseTotal
$item->{'prog_args'} ? : 0 0 0
590 \"&");
591 }
592 }
593  
594 } else {
595  
596 0 die "BUG: Unknown window type $windowkey";
597  
598 }
599  
600 } # for window
601  
602 0 [200];
603 },
604 1 );
605  
606 1 1;
607 # ABSTRACT: Utilities related to DesktopWorkspace
608  
609 __END__
↑ Summary