Pimpd/Shell.pm untested

filter: Statementstmt 0.0% 0 / 270 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 379 CC 39 SCAR 69.4
CC
39
Cov
0%
spawn_shell
1560.0
BEGIN
2.0
BEGIN
2.0
CRAP 1031.9
1 package App::Pimpd::Shell;
2 use strict;
3  
4 BEGIN {
5 use Exporter;
6 use vars qw(@ISA @EXPORT);
7 @ISA = qw(Exporter);
8 @EXPORT = qw(
9 spawn_shell
10 );
11 }
12  
13 use App::Pimpd;
14 use App::Pimpd::Doc;
15 use App::Pimpd::Info;
16 use App::Pimpd::Player;
17 use App::Pimpd::Commands;
18 use App::Pimpd::Transfer;
19 use App::Pimpd::Collection::Album;
20 use App::Pimpd::Collection::Search;
21 use App::Pimpd::Playlist;
22 use App::Pimpd::Playlist::Favorite;
23 use App::Pimpd::Playlist::Randomize;
24 use App::Pimpd::Playlist::Search;
25 use App::Pimpd::Validate;
26 use Term::ExtendedColor qw(fg);
27 use Term::ReadLine; # Term::ReadLine::Gnu prefered
28 use File::LsColor qw(ls_color);
29  
30 my $opts;
31 sub spawn_shell {
32 my $option = shift;
33 my($cmd, $arg, @cmd_args); # for later use
34  
35  
36 #print help('shell');
37  
38  
39 $opts = {
40  
41 'randomize' => sub {
42 if(!defined($_[0])) {
43 $_[0] = 100;
44 }
45 elsif(defined($_[0]) and $_[0] !~ /^\d+$/m) {
46 print STDERR "Need a valid integer\n";
47 $_[0] = 100;
48 }
49 print 'Adding ' . fg('bold', @_) . " random tracks...\n";
50 my @random = randomize(@_);
51  
52 # print $_->artist for @random;
53 # print "$_\n" for @random;
54 clear_playlist();
55 add_to_playlist(@random);
56 print "$_\n" for map { ls_color($_) } @random;
57 },
58  
59  
60 'randomalbum' => sub {
61 $_[0] = 10 if(!$_[0]);
62 print 'Adding ' . fg('bold', $_[0]) . " random albums...\n\n";
63 my @albums = randomize_albums($_[0]);
64  
65 my $old = undef;
66 for(@albums) {
67 my($album_dir) = $_ =~ m|(.+)/.+|m;
68 if($old ne $album_dir) {
69 print "> $album_dir\n";
70 $old = $album_dir;
71 }
72 }
73 print "\n";
74 clear_playlist();
75 add_to_playlist(@albums);
76 },
77  
78  
79 'playlist' => sub {
80 if(empty_playlist()) {
81 print STDERR "Playlist is empty\n";
82 return 1;
83 }
84 show_playlist();
85 print fg('bold', ' >'), '> ', current(), "\n";
86 },
87  
88  
89 'love' => sub {
90 if(empty_playlist()) {
91 print STDERR "Nothing is playing - playlist is empty\n";
92 return 1;
93 }
94 add_to_favlist(@_);
95 },
96  
97 'loved?' => sub {
98 if(already_loved($mpd->current->file)) {
99 printf("%s, %s by %s is loved.\n",
100 fg('bold', 'Yes'),
101 fg($c[10], $mpd->current->title),
102 fg($c[2], fg('bold', $mpd->current->artist)),
103 );
104 }
105 else {
106 printf("%s, %s by %s is not loved yet.\n",
107 fg('bold', 'No'),
108 fg($c[10], $mpd->current->title),
109 fg($c[2], fg('bold', $mpd->current->artist)),
110 );
111 }
112 },
113  
114 'unlove' => sub {
115 if(!@_) {
116 print help('unlove');
117 return;
118 }
119 remove_favorite(@_);
120 },
121  
122 'track' => sub {
123 $_[0] = 1 if $_[0] !~ /^\d+$/m;
124 play_pos_from_playlist(@_);
125 },
126  
127 'copy' => sub {
128 if(empty_playlist()) {
129 print STDERR "Nothing is playing - playlist is empty\n";
130 return 1;
131 }
132 cp($config{target_directory});
133 },
134  
135 'copya' => sub {
136 if(empty_playlist()) {
137 print STDERR "Nothing is playing - playlist is empty\n";
138 return 1;
139 }
140 cp_album($config{target_directory});
141 },
142  
143 # FIXME
144 'copy-list' => sub { cp_list(@_); },
145  
146 'info' => sub {
147 if(empty_playlist()) {
148 print STDERR "Nothing is playing - playlist is empty\n";
149 return 1;
150 }
151 info();
152 },
153  
154 'monitor' => sub {
155 if(empty_playlist()) {
156 print STDERR "Playlist is empty - there's nothing to monitor\n";
157 return 1;
158 }
159 #monitor();
160 #FIXME
161 },
162  
163 'sartist' => sub {
164 my $artist = join(' ', @_);
165 if(!$artist) {
166 print help('sartist');
167 return;
168 }
169 add_to_playlist(search_db_artist($artist));
170 },
171  
172 'salbum' => sub {
173 my $album = join(' ', @_);
174 if(!$album) {
175 print help('salbum');
176 return;
177 }
178 add_to_playlist(search_db_album($album));
179 },
180  
181 'stitle' => sub {
182 my $title = join(' ', @_);
183 if(!$title) {
184 print help('stitle');
185 return;
186 }
187  
188 my @result = search_db_title($title);
189 if(@result) {
190 print "$_\n" for @result;
191 add_to_playlist(@result);
192 }
193 else {
194 printf("No titles matching '%s'\n",
195 fg($c[5], $title),
196 );
197 }
198 },
199  
200 'sany' => sub {
201 my $search = join(' ', @_);
202 if(!$search) {
203 print help('sany');
204 return;
205 }
206 add_to_playlist(search_db_quick($search));
207 },
208  
209 'splaylist' => sub {
210 my $search = join(' ', @_);
211 if(!$search) {
212 print help('splaylist');
213 return;
214 }
215 print "$_\n" for values %{ search_playlist($search) };
216 my $result = search_playlist($search);
217 if(scalar(keys(%{ $result })) > 0) {
218 print "$_\n" for values %{ $result };
219 queue( keys % { search_playlist($search) } );
220 }
221 else {
222 print "No match\n";
223 }
224 },
225  
226 'slove' => sub {
227 my $search = join(' ', @_);
228 my @files = search_favlist($search);
229 print "$_\n" for @files;
230 add_to_playlist(@files);
231 },
232  
233  
234 'sap' => sub {
235 my $search = join(' ', @_);
236 if(!$search) {
237 print help('slove');
238 return;
239 }
240 my @result = search_all_playlists($search);
241 print "$_\n" for @result;
242 add_to_playlist(@result);
243 },
244  
245  
246 'albums' => sub {
247 if(empty_playlist() and !@_) {
248 print STDERR "Nothing is playing, and no argument supplied\n";
249 return 1;
250 }
251 my $artist = join(' ', @_);
252 print "$_\n" for albums_by_artist($artist);
253 },
254  
255 'songs' => sub {
256 printf "%s\n", ls_color($_->file) for songs_on_album(@_);
257 # printf "%s\n", $_->title for songs_on_album(@_);
258 },
259  
260 'add-album' => sub {
261 add_to_playlist( map{ $_->file } get_album_songs(@_));
262 },
263 'lsplaylists' => sub { print "$_\n" for list_all_playlists(); },
264 'add' => sub {
265  
266 if($_[0] eq 'songs') {
267 local $\ = "\n";
268 my @songs = map { $_->file } songs_on_album();
269 printf("Adding %d songs from %s\n",
270 scalar(@songs), fg('bold', $mpd->current->album),
271 );
272 add_to_playlist(@songs);
273 }
274  
275 elsif($_[0] eq 'slove') {
276 shift @_; # so we can grab the PATTERN
277 my @result = search_favlist(@_);
278  
279 if(scalar(@result) > 0) {
280 print "$_\n" for @result;
281  
282 add_to_playlist(@result);
283 printf("\nAdded %s loved %s matching '%s'\n",
284 fg('bold', scalar(@result)),
285 (scalar(@result) > 1) ? 'songs' : 'song',
286 fg($c[4], fg('bold', $_[0])),
287 );
288 }
289 else {
290 printf("No songs matching '%s' were found\n",
291 fg($c[4], fg('bold', $_[0])),
292 );
293 }
294 }
295  
296 else {
297 add_playlist(@_);
298 }
299 },
300  
301 'next' => sub {
302 if(empty_playlist()) {
303 print STDERR "Playlist is empty!\n";
304 return 1;
305 }
306 next_track();
307 print current() . "\n";
308 },
309  
310 'previous' => sub {
311 if(empty_playlist()) {
312 print STDERR "Playlist is empty!\n";
313 return 1;
314 }
315 previous_track();
316 print current() . "\n";
317 },
318  
319 'pause' => sub {
320 toggle_pause();
321 print $mpd->status->state . "\n";
322 },
323  
324 'shuffle' => sub {
325 $mpd->playlist->shuffle;
326 print "New playlist version is " .$mpd->status->playlist . "\n"
327 },
328  
329 'np' => sub {
330 if(empty_playlist()) {
331 print STDERR "Nothing is playing - playlist is empty\n";
332 return 1;
333 }
334 print current() . "\n";
335 },
336  
337 'nprt' => sub {
338 if(empty_playlist()) {
339 print STDERR "Nothing is playing - playlist is empty\n";
340 return 1;
341 }
342 np_realtime();
343 },
344  
345 'queue' => sub {
346 if(invalid_playlist_pos(@_)) {
347 printf("No such song%s\n", (@_ < 1) ? 's' : '');
348 return 1;
349 }
350 queue(@_);
351 },
352  
353 'random' => sub {
354 $mpd->random;
355 my $status = ($mpd->status->random)
356 ? "Random: " . fg('bold', 'On')
357 : "Random: " . fg('bold', 'Off');
358 print "$status\n";
359 },
360  
361 'repeat' => sub {
362 $mpd->repeat;
363 my $status = ($mpd->status->repeat)
364 ? "Repeat: " . fg('bold', 'On')
365 : "Repeat: " . fg('bold', 'Off');
366 print "$status\n";
367 },
368  
369 'stats' => sub { stats(); },
370 'status' => sub { print status(), "\n"; },
371  
372 'randomtrack' => sub {
373 play_pos_from_playlist(random_track_in_playlist());
374 print current(), "\n";
375 },
376  
377 'external' => sub { songs_in_playlist(@_); },
378 'clear' => sub { clear_playlist() },
379 'crop' => sub { $mpd->playlist->crop; },
380 'stop' => sub { stop(); },
381 'kill' => sub { player_destruct(); },
382 'play' => sub {
383 if(empty_playlist()) {
384 print STDERR "Nothing is playing - playlist is empty\n";
385 return 1;
386 }
387 play();
388 },
389  
390 'delete-album' => \&delete_album,
391 'stats' => sub { App::Pimpd::Info::stats() },
392  
393 'rmalbum' => sub { remove_album_from_playlist(@_); },
394 'exit' => sub { exit(0); },
395 ':q' => sub { exit(0); },
396 'help' => sub {
397 if( defined($opts->{$_[0]}) ) {
398 print help($_[0]);
399 }
400 else {
401 print help('shell');
402 }
403 },
404 };
405  
406 while(1) {
407 #print fg($c[6], 'pimpd'), fg('bold', '> ');
408  
409 #chomp(my $choice = <STDIN>);
410 my @available_cmd = keys(%{$opts});
411 push(@available_cmd, 'shell');
412  
413 my $term = Term::ReadLine->new('pimpd2');
414 my $attr = $term->Attribs;
415  
416 $attr->{completion_function} = sub {
417 my($text, $line, $start) = @_;
418 return @available_cmd;
419 };
420  
421 $attr->{autolist} = 0;
422 $attr->{maxcomplete} = 0;
423 # Sane keymap please.
424 $term->set_keymap('vi');
425  
426 my $choice;
427  
428 while(1) {
429 $choice = $term->readline(fg($c[6], 'pimpd') . fg('bold', '> '));
430 $term->addhistory($choice) if $choice =~ /\S/m;
431  
432 ($cmd) = $choice =~ m/^(\S+)/m;
433 ($arg) = $choice =~ m/\s+(.+)$/m;
434 @cmd_args = split(/\s+/m, $arg);
435  
436 if(defined($opts->{$cmd})) {
437 $mpd->play;
438 $opts->{$cmd}->(@cmd_args);
439 }
440 else {
441 $opts->{help}->();
442 print STDERR "No such option '", fg($c[5], $cmd), "'.\n";
443 }
444 }
445 }
446 exit(0);
447 }
448  
449  
450  
451 1;
452  
453 __END__