blib/lib/App/Devmode2.pm

filter: Statementstmt 28.5% 32 / 112 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 53.3% 8 / 15 Podpod 100% 4 / 4 totaltotal 22.2% 44 / 198 CC 28 SCAR 42.5
CC
28
Cov
19%
run
156.0
process_config
90.0
_auto
31.6
CRAP 69.8
↑ Summary
1 package App::Devmode2;
2  
3 # Created on: 2014-10-04 20:31:39
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use FindBin qw/$Bin/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 our $VERSION = 0.9;
18 1 our ($name) = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
19 1 our $tmux_conf = path("$ENV{HOME}", '.tmux.conf');
20 1 our $tmux_layout = path("$ENV{HOME}", '.tmux', 'layout');
21 1 our $tmux_devmode = path("$ENV{HOME}", '.tmux', 'devmode2');
22 1 our %option;
23 1 our %p2u_extra;
24  
25 0 ▶sub run {
Subroutine run: not called CC=12 SCAR=50.5
Pod documented
26 0 my ($self) = @_;
27 0 Getopt::Long::Configure('bundling');
28 0 my $success = GetOptions(
29 \%option,
30 'layout|l=s',
31 'chdir|cd|c=s',
32 'curdir|C',
33 'save|s',
34 'auto|auto-complete',
35 'current=s',
36 'test|t!',
37 'verbose|v+',
38 'man',
39 'help',
40 'version!',
41 );
42  
43 0 ▶ if ( !$success && !$option{auto} ) {
Branch: 5 branches
BranchTrueFalseTotal
if (not $success || $App::Devmode2::option{'auto'}) { } 0 0 0
elsif ($App::Devmode2::option{'VERSION'}) { } 0 0 0
elsif ($App::Devmode2::option{'man'}) { } 0 0 0
elsif ($App::Devmode2::option{'help'}) { } 0 0 0
elsif ($App::Devmode2::option{'auto'}) { } 0 0 0
Condition0%0/3
$success || $App::Devmode2::option{'auto'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$success || $App::Devmode2::option{'auto'}
$success $App::Devmode2::option{'auto'}
Truth table0%0/3
A || B
A= $successB= $App::Devmode2::option{'auto'}
ABresult
1X1
011
000
44 0 require Pod::Usage;
45 0 Pod::Usage::pod2usage(
46 -verbose => 1,
47 -input => __FILE__,
48 %p2u_extra,
49 );
50 0 return 1;
51 }
52 elsif ( $option{'VERSION'} ) {
53 0 print "$name Version = $VERSION\n";
54 0 return 0;
55 }
56 elsif ( $option{'man'} ) {
57 0 require Pod::Usage;
58 0 Pod::Usage::pod2usage(
59 -verbose => 2,
60 -input => __FILE__,
61 %p2u_extra,
62 );
63 0 return 2;
64 }
65 elsif ( $option{'help'} ) {
66 0 require Pod::Usage;
67 0 Pod::Usage::pod2usage(
68 -verbose => 1,
69 -input => __FILE__,
70 %p2u_extra,
71 );
72 0 return 1;
73 }
74 elsif ( $option{auto} ) {
75 0 $self->_auto();
76 0 return 0;
77 }
78  
79 # get the session name
80 0 ▶ my $session = @ARGV ? shift @ARGV : die "No session name passed!";
Branch: 1 branch
BranchTrueFalseTotal
@ARGV ? : 0 0 0
81 0 my @sessions = $self->sessions();
82  
83 # set the terminal title to the session name
84 0 $self->set_title($session);
85  
86 0 ▶ if ( grep { $_ eq $session } @sessions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $session;} @sessions) 0 0 0
87 # connect to session
88 0 $self->_exec('tmux', '-u2', 'attach', '-t', $session);
89 0 return 1;
90 }
91  
92 # creating a new session should do some extra work
93 0 $self->process_config($session, \%option);
94  
95 0 ▶ if ($option{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"chdir"}) 0 0 0
96 0 ▶ die "No directory '$option{chdir}'!\n" if !-d $option{chdir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $option{"chdir"} 0 0 0
97 0 chdir $option{chdir};
98 }
99  
100 0 my @actions = ('-u2', 'new-session', '-s', $session, ';', 'source-file', $tmux_conf);
101 0 ▶ if ($option{layout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"layout"}) 0 0 0
102 0 push @actions, ';', "source-file", $tmux_layout->child($option{layout});
103 }
104  
105 0 $self->_exec('tmux', @actions);
106 0 warn "Not found\n";
107  
108 0 return 1;
109 }
110  
111 0 ▶sub set_title {
Subroutine set_title: not called CC=2 SCAR=17.9
Pod documented
112 0 my ($self, $session) = @_;
113 0 ▶ eval { require Term::Title; } or return;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::Title } } or return 0 0 0
114 0 Term::Title::set_titlebar($session);
115 0 return;
116 }
117  
118 0 ▶sub sessions {
Subroutine sessions: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift;
120 return map {
121 0 /^(.+) : \s+ \d+ \s+ window/xms;
122 0 $1;
123 }
124 $self->_qx('tmux ls');
125 }
126  
127 0 ▶sub process_config {
Subroutine process_config: not called CC=9 SCAR=45.0
Pod documented
128 0 my ($self, $session, $option) = @_;
129 0 my $config_file = $tmux_devmode->child($session);
130  
131 # return if no config and not saving
132 0 ▶ return if !-f $config_file && !$option->{save};
Branch: 1 branch
BranchTrueFalseTotal
unless -f $config_file or $option->{"save"} 0 0 0
Condition0%0/3
-f $config_file or $option->{"save"}
l!l&&r!l&&!r
000
MC/DC0%0/2
-f $config_file or $option->{"save"}
-f $config_file $option->{"save"}
Truth table0%0/3
A or B
A= -f $config_fileB= $option->{"save"}
ABresult
1X1
011
000
133  
134 0 ▶ if ( -f $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $config_file) 0 0 0
135 0 require YAML;
136 0 my ($config) = YAML::LoadFile("$config_file");
137 0 for my $key (keys %{ $config }) {
138 0 ▶ $option->{$key} = $config->{$key} if !exists $option->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $option->{$key} 0 0 0
139 }
140 }
141  
142 # save the config if requested to
143 0 ▶ if ($option->{save} || $option{curdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"save"} or $option{"curdir"}) 0 0 0
Condition0%0/3
$option->{"save"} or $option{"curdir"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$option->{"save"} or $option{"curdir"}
$option->{"save"} $option{"curdir"}
Truth table0%0/3
A or B
A= $option->{"save"}B= $option{"curdir"}
ABresult
1X1
011
000
144 # create the path if missing
145 0 $config_file->parent->mkpath();
146  
147 # don't save saving
148 0 delete $option->{save};
149  
150 0 ▶ if ($option{curdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"curdir"}) 0 0 0
151 0 delete $option->{curdir};
152 0 $option->{chdir} = path('.')->realpath . '';
153 }
154  
155 # save the config to YAML
156 0 require YAML;
157 0 YAML::DumpFile("$config_file", $option);
158 }
159  
160 0 return;
161 }
162  
163 0 ▶sub _qx {
Subroutine _qx: not called CC=1 SCAR=6.9
164 0 my $self = shift;
165 0 return qx/@_/;
166 }
167  
168 0 ▶sub _exec {
Subroutine _exec: not called CC=3 SCAR=24.8
169 0 my $self = shift;
170 0 ▶ print join ' ', @_, "\n" if $option{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"verbose"} 0 0 0
171 0 ▶ exec @_ if !$option{test};
Branch: 1 branch
BranchTrueFalseTotal
unless $option{"test"} 0 0 0
172 0 return;
173 }
174  
175 0 ▶sub _auto {
Subroutine _auto: not called CC=6 SCAR=34.5
176 0 my ($self) = @_;
177 0 my $current = $ARGV[$option{current}];
178 0 my $previous = $ARGV[$option{current} - 1];
179  
180 0 ▶ if ( defined $current && $current =~ /^-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $current and $current =~ /^-/) { } 0 0 0
elsif ($previous =~ /^-c$|^--(?:chdir|cd)$/) { } 0 0 0
Condition0%0/3
defined $current and $current =~ /^-/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current and $current =~ /^-/
defined $current $current =~ /^-/
Truth table0%0/3
A and B
A= defined $currentB= $current =~ /^-/
ABresult
0X0
100
111
181 0 print join "\n", qw/-l --layout -s --save -c --cd -C --curdir/, '';
182 }
183 elsif ( $previous =~ /^-c$|^--(?:chdir|cd)$/ ) {
184 0 ▶ print join "\n", glob "$current*";
Subroutine BEGIN: called CC=1 SCAR=0.2
185 }
186 else {
187 0 ▶ my $dir = $previous =~ /^-\w*l$|^--layout$/ ? $tmux_layout : $tmux_devmode;
Branch: 1 branch
BranchTrueFalseTotal
$previous =~ /^-\w*l$|^--layout$/ ? : 0 0 0
188 my @found = sort {
189 0 lc $a cmp lc $b
190 }
191 grep {
192 0 ▶ !$current || /^$current/i
Condition0%0/3
!$current || /^$current/i
l!l&&r!l&&!r
000
MC/DC0%0/2
!$current || /^$current/i
!$current /^$current/i
Truth table0%0/3
not(A) || B
A= !$currentB= /^$current/i
ABresult
1X1
011
000
193 }
194 map {
195 0 m{/([^/]+)$}; $1
196 }
197 $dir->children;
198 0 print join "\n", @found, '';
199 }
200 }
201  
202 1 1;
203  
204 __END__
↑ Summary