| 1 | package App::VTide::Command::Sessions; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # Created on: 2016-03-22 15:42:06 | ||||||||||||||||||||
| 4 | # Create by: Ivan Wills | ||||||||||||||||||||
| 5 | # $Id$ | ||||||||||||||||||||
| 6 | # $Revision$, $HeadURL$, $Date$ | ||||||||||||||||||||
| 7 | # $Revision$, $Source$, $Date$ | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use version; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use English qw/ -no_match_vars /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use YAML::Syck qw/ DumpFile LoadFile /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use Path::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use App::VTide::Sessions; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use Data::Dumper qw/Dumper/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | extends 'App::VTide::Command::Run'; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 1 | our $VERSION = version->new('1.0.6'); | |||||||||||||||||||
| 22 | 1 | our $NAME = 'sessions'; | |||||||||||||||||||
| 23 | 1 | our $OPTIONS = [ | |||||||||||||||||||
| 24 | 'dest|d=s', 'global|g', 'session|source|s=s', 'verbose|v+', | ||||||||||||||||||||
| 25 | 'update|u!', 'test|t!' | ||||||||||||||||||||
| 26 | ]; | ||||||||||||||||||||
| 27 | 0 | ▶ | sub details_sub { return ( $NAME, $OPTIONS ) } | ||||||||||||||||||
|
Subroutine
details_sub: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | has global => ( | ||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||
| 31 | lazy => 1, | ||||||||||||||||||||
| 32 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 33 | 0 | ▶ | $_[0]->options->opt->{global} || !$ENV{VTIDE_NAME}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||
| 35 | 1 | ); | |||||||||||||||||||
| 36 | has sessions => ( | ||||||||||||||||||||
| 37 | is => 'ro', | ||||||||||||||||||||
| 38 | lazy => 1, | ||||||||||||||||||||
| 39 | 0 | ▶ | default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 40 | 0 | my ($self) = @_; | |||||||||||||||||||
| 41 | 0 | ▶ | my $file = | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | $self->global | ||||||||||||||||||||
| 43 | ? $App::VTide::Sessions::global_file | ||||||||||||||||||||
| 44 | : $App::VTide::Sessions::local_file; | ||||||||||||||||||||
| 45 | 0 | return App::VTide::Sessions->new( { sessions_file => $file } ); | |||||||||||||||||||
| 46 | }, | ||||||||||||||||||||
| 47 | 1 | ); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 50 | 0 | my ($self) = @_; | |||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | ▶ | $self->options->opt->{update} //= 1; | ||||||||||||||||||
|
Condition0%0/2
$self->options->opt->{"update"} //= 1
MC/DC0%0/1
$self->options->opt->{"update"} //= 1
$self->options->opt->{"update"}
Truth table0%0/2
A
A= $self->options->opt->{"update"}
| |||||||||||||||||||||
| 53 | 0 | ▶ | $self->options->opt->{session} ||= 'current'; | ||||||||||||||||||
|
Condition0%0/2
$self->options->opt->{"session"} ||= "current"
MC/DC0%0/1
$self->options->opt->{"session"} ||= "current"
$self->options->opt->{"session"}
Truth table0%0/2
A
A= $self->options->opt->{"session"}
| |||||||||||||||||||||
| 54 | 0 | my $local = !$self->global; | |||||||||||||||||||
| 55 | 0 | ▶ | my $command = 'session_' . ( shift @{ $self->options->files } || 'list' ); | ||||||||||||||||||
|
Condition0%0/2
shift @{$self->options->files;} || 'list'
MC/DC0%0/1
shift @{$self->options->files;} || 'list'
shift @{$self->options->files;}
Truth table0%0/2
A
A= shift @{$self->options->files;}
| |||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | ▶ | if ( !$self->can($command) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | warn "Unknown command $command!\n"; | |||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | my $session = $self->sessions; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | $self->$command($session); | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | sub session_list { | ||||||||||||||||||
|
Subroutine
session_list: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||
| 70 | 0 | my ( $self, $session ) = @_; | |||||||||||||||||||
| 71 | 0 | my $name = $self->options->opt->{session}; | |||||||||||||||||||
| 72 | 0 | ▶ | $session ||= $self->sessions; | ||||||||||||||||||
|
Condition0%0/3
$session ||= $self->sessions
MC/DC0%0/2
$session ||= $self->sessions
$session
$self->sessions
Truth table0%0/3
A ||= B
A= $sessionB= $self->sessions
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 0 | ▶ | if ( $self->options->opt->{verbose} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | print "Sessions:\n"; | |||||||||||||||||||
| 76 | 0 | for my $name ( sort keys %{ $session->sessions } ) { | |||||||||||||||||||
| 77 | 0 | print " $name\n"; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | 0 | print "\n"; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | print "$name:\n"; | |||||||||||||||||||
| 83 | 0 | ▶ | if ( $session->sessions->{$name} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | ▶ | my $cmd = "vtide session" | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | . ( | ||||||||||||||||||||
| 86 | $name eq 'current' | ||||||||||||||||||||
| 87 | ? '' | ||||||||||||||||||||
| 88 | : " --session $name" | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | 0 | for my $i ( 0 ... @{ $session->sessions->{$name} } - 2 ) { | |||||||||||||||||||
| 91 | 0 | my $files = $session->sessions->{$name}[$i]; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | print " ", ( join " ", @$files ), "\n"; | |||||||||||||||||||
| 94 | 0 | ▶ | if ( $i == 0 ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 95 | 0 | print " ('$cmd shift' to run)\n"; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | 0 | elsif ( $i == @{ $session->sessions->{$name} } - 2 ) { | |||||||||||||||||||
| 98 | 0 | print " ('$cmd pop' to run)\n"; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||
| 103 | 0 | print " Empty\n"; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | } | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | ▶ | sub session_unshift { | ||||||||||||||||||
|
Subroutine
session_unshift: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 108 | 0 | my ($self) = @_; | |||||||||||||||||||
| 109 | return $self->modify_session( | ||||||||||||||||||||
| 110 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 111 | 0 | unshift @{ $_[0] }, $self->options->files; | |||||||||||||||||||
| 112 | 0 | return; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | 0 | ); | |||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub session_push { | ||||||||||||||||||
|
Subroutine
session_push: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 119 | 0 | my ($self) = @_; | |||||||||||||||||||
| 120 | return $self->modify_session( | ||||||||||||||||||||
| 121 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 122 | 0 | push @{ $_[0] }, $self->options->files; | |||||||||||||||||||
| 123 | 0 | return; | |||||||||||||||||||
| 124 | } | ||||||||||||||||||||
| 125 | 0 | ); | |||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | ▶ | sub session_shift { | ||||||||||||||||||
|
Subroutine
session_shift: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 130 | 0 | my ($self) = @_; | |||||||||||||||||||
| 131 | return $self->modify_session( | ||||||||||||||||||||
| 132 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 133 | 0 | my ($session) = @_; | |||||||||||||||||||
| 134 | 0 | ▶ | if ( !$session->[0] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 135 | 0 | warn "No more sessions to run!\n"; | |||||||||||||||||||
| 136 | 0 | exit 0; | |||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | 0 | warn "running $session->[0][0]\n"; | |||||||||||||||||||
| 139 | 0 | return shift @$session; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | 0 | ); | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 0 | ▶ | sub session_pop { | ||||||||||||||||||
|
Subroutine
session_pop: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 145 | 0 | my ($self) = @_; | |||||||||||||||||||
| 146 | return $self->modify_session( | ||||||||||||||||||||
| 147 | 0 | ▶ | sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 148 | 0 | my ($session) = @_; | |||||||||||||||||||
| 149 | 0 | return pop @$session; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | 0 | ); | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | sub modify_session { | ||||||||||||||||||
|
Subroutine
modify_session: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 155 | 0 | my ( $self, $modify ) = @_; | |||||||||||||||||||
| 156 | 0 | my $name = $self->options->opt->{session}; | |||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | 0 | my $session = $self->sessions->sessions; | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 0 | my $files = $modify->( $session->{$name} ); | |||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | ▶ | if ( !@{ $session->{$name} } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 163 | 0 | delete $session->{$name}; | |||||||||||||||||||
| 164 | 0 | warn "Empty session removed!\n"; | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | # TODO work out why update unset with --no-update | ||||||||||||||||||||
| 168 | # write the new session out | ||||||||||||||||||||
| 169 | 0 | ▶ | if ( $self->options->opt->update ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 0 | $self->sessions->write_session(); | |||||||||||||||||||
| 171 | } | ||||||||||||||||||||
| 172 | |||||||||||||||||||||
| 173 | 0 | ▶ | if ( !$files ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 0 | $self->session_list(); | |||||||||||||||||||
| 175 | 0 | return; | |||||||||||||||||||
| 176 | } | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | 0 | ▶ | my $action = $self->global ? "start" : "edit"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 179 | 0 | warn join ' ', 'vtide', $action, @$files; | |||||||||||||||||||
| 180 | 0 | system 'vtide', $action, @$files; | |||||||||||||||||||
| 181 | 0 | warn join ' ', 'vtide', $action, @$files; | |||||||||||||||||||
| 182 | } | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | 0 | ▶ | sub session_copy { | ||||||||||||||||||
|
Subroutine
session_copy: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 185 | 0 | my ($self) = @_; | |||||||||||||||||||
| 186 | 0 | my $src = $self->options->opt->{session}; | |||||||||||||||||||
| 187 | 0 | my $dest = $self->options->opt->{dest}; | |||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 0 | my $session = $self->sessions->sessions; | |||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | 0 | ▶ | if ( !$dest ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | 0 | warn "No destination name!"; | |||||||||||||||||||
| 193 | 0 | return; | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | 0 | $session->{$dest} = [ map { [@$_] } @{ $session->{$src} } ]; | |||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 0 | $self->sessions->write_session(); | |||||||||||||||||||
| 198 | 0 | $self->session_list(); | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 0 | ▶ | sub session_save { | ||||||||||||||||||
|
Subroutine
session_save: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 202 | 0 | my ($self) = @_; | |||||||||||||||||||
| 203 | 0 | ▶ | my $dest = $self->options->opt->{dest} || 'current'; | ||||||||||||||||||
|
Condition0%0/2
$self->options->opt->{'dest'} || 'current'
MC/DC0%0/1
$self->options->opt->{'dest'} || 'current'
$self->options->opt->{'dest'}
Truth table0%0/2
A
A= $self->options->opt->{'dest'}
| |||||||||||||||||||||
| 204 | 0 | my $session = $self->sessions->get_sessions($dest); | |||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | 0 | ▶ | if ( $self->options->opt->{global} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 207 | 0 | my @list = map { ( split /:/, $_ )[0] } `tmux ls`; | |||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 0 | push @$session, @list; | |||||||||||||||||||
| 210 | } | ||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||
| 212 | 0 | warn "Haven't yet built local session saving"; | |||||||||||||||||||
| 213 | } | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 0 | $self->sessions->write_session(); | |||||||||||||||||||
| 216 | } | ||||||||||||||||||||
| 217 | |||||||||||||||||||||
| 218 | 0 | ▶ | sub auto_complete { | ||||||||||||||||||
|
Subroutine
auto_complete: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 219 | 0 | my ( $self, $auto ) = @_; | |||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 0 | ▶ | my $partial = $ARGV[ $auto - 1 ] || ''; | ||||||||||||||||||
|
Condition0%0/2
$ARGV[$auto - 1] || ''
MC/DC0%0/1
$ARGV[$auto - 1] || ''
$ARGV[$auto - 1]
Truth table0%0/2
A
A= $ARGV[$auto - 1]
| |||||||||||||||||||||
| 222 | 0 | print join ' ', grep { /^$partial/ } qw/ | |||||||||||||||||||
| 223 | copy | ||||||||||||||||||||
| 224 | list | ||||||||||||||||||||
| 225 | pop | ||||||||||||||||||||
| 226 | push | ||||||||||||||||||||
| 227 | shift | ||||||||||||||||||||
| 228 | unshift | ||||||||||||||||||||
| 229 | /; | ||||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 1 | 1; | |||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||