Pimpd/Playlist.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 187 CC 14 SCAR 22.4
CC
14
Cov
0%
remove_album_from_playlist
20.0
queue
20.0
get_album_songs
12.0
CRAP 9.4
1 package App::Pimpd::Playlist;
2 use strict;
3  
4 BEGIN {
5 use Exporter;
6 use vars qw(@ISA @EXPORT);
7 @ISA = qw(Exporter);
8 @EXPORT = qw(
9 show_playlist
10 play_pos_from_playlist
11 queue
12 songs_in_playlist
13 add_playlist
14 list_all_playlists
15 add_to_playlist
16 get_album_songs
17 remove_album_from_playlist
18 );
19 }
20  
21 #TODO
22 # List content in all playlist
23 # Search all playlist, without args, search for the current song
24  
25 use App::Pimpd;
26 use App::Pimpd::Validate;
27 use Term::ExtendedColor qw(fg bg bold);
28  
29 sub get_album_songs {
30 my $album = shift // $mpd->current->album;
31 if( (!defined($album)) or ($album eq '') ) {
32 return;
33 }
34  
35 my @tracks = $mpd->collection->songs_from_album($album);
36 return wantarray() ? @tracks : scalar(@tracks);
37 }
38  
39 sub remove_album_from_playlist {
40 my $search_str = shift;
41 if(!defined($search_str)) {
42 $search_str = $mpd->current->album;
43 }
44  
45 my @removed;
46 for($mpd->playlist->as_items) {
47 if($_->album =~ m/\Q$search_str\E/gim) {
48 if(not(invalid_playlist_pos($_->pos))) {
49 $mpd->playlist->delete($_->pos);
50 }
51 }
52 }
53 return;
54 }
55  
56 sub add_to_playlist {
57 my @songs = @_;
58  
59 if(ref($songs[0] eq 'ARRAY')) {
60 push(@songs, @{$songs[0]});
61 shift(@songs);
62 }
63 chomp(@songs);
64  
65 $mpd->playlist->add(@songs);
66  
67 # Start playback. Often one wants to clear the playlist and add a bunch of
68 # new, maybe randomized, content. When the playlist is cleared, playback will
69 # stop.
70 # If using the player() functionality, we have around 20s to start playback
71 # again. 20s ought to be enough for everybody.
72 $mpd->play;
73 return 0;
74 }
75  
76  
77 sub add_playlist {
78 my @lists = get_valid_lists(@_);
79 for(@lists) {
80 $mpd->playlist->load($_);
81 }
82 $mpd->play;
83 return 0;
84 }
85  
86 sub songs_in_playlist {
87 my @playlists = @_;
88  
89 @playlists = get_valid_lists(@playlists);
90 for my $playlist(@playlists) {
91 my $full_path = $config{playlist_directory} . "/$playlist.m3u";
92  
93 my $fh = undef;
94 if(remote_host()) {
95 #FIXME 80c !
96 open($fh,
97 "ssh -p $config{ssh_port} $config{ssh_user}\@$config{ssh_host} \"/bin/cat '$full_path'\"|"
98 ) or die("ssh: $config{ssh_host}:$config{ssh_port}:\n$!\n");
99 }
100 else {
101 open($fh, '<', $full_path) or die("Can not open $full_path: $!");
102 }
103 while(<$fh>) {
104 print fg($c[4], $playlist), ': ', fg($c[10], $_);
105 }
106 close($fh);
107 }
108 return 0;
109 }
110  
111 sub play_pos_from_playlist {
112 my $track_no = shift;
113  
114 if(invalid_playlist_pos($track_no)) {
115 print STDERR "Playlist index '$track_no' is invalid\n";
116 return 1;
117 }
118 $mpd->play($track_no);
119 return;
120 }
121  
122 sub queue {
123 my @to_play = @_;
124 if(scalar(@to_play < 1)) {
125 print STDERR "The queue function requires at least one song \n";
126 return 1;
127 }
128  
129 for(@to_play) {
130 if(invalid_playlist_pos($_)) {
131 print STDERR fg($c[5], $_), ": invalid position\n";
132 return 1;
133 }
134 }
135  
136 my %list = ();
137 map { $list{$_->pos} = $_->title } $mpd->playlist->as_items;
138  
139  
140 $mpd->random(0);
141 $mpd->play(shift(@to_play));
142 $mpd->playlist->move($mpd->current->pos, 0);
143  
144 return 0 if(scalar(@to_play) == 0);
145  
146  
147 my $next_pos = $mpd->current->pos + 1;
148 print fg('bold', 'Queueing'), ":\n";
149 for(@to_play) {
150 printf("%-50.50s %s\n", fg($c[3], $list{$_}), "( $_ => $next_pos )");
151  
152 $mpd->playlist->move($_, $next_pos);
153 $next_pos++;
154 }
155 return;
156 }
157  
158 sub show_playlist {
159 my @playlist = $mpd->playlist->as_items;
160  
161 my $i = 0;
162 for my $song(@playlist) {
163 my $title = $song->title // 'undef';
164 my $artist = $song->artist // 'undef';
165  
166  
167 my $crnt_title = $mpd->current->title // undef;
168 my $crnt_artist = $mpd->current->artist // undef;
169 $title =~ s/(\w+)/\u\L$1/gm;
170 $artist =~ s/(\w+)/\u\L$1/gm;
171 $crnt_title =~ s/(\w+)/\u\L$1/gm;
172 $crnt_artist =~ s/(\w+)/\u\L$1/gm;
173  
174 if($mpd->current->pos == $i) {
175 # bg('red4', $i) will add another 17 chars
176 printf("%s\n", bg('26', bold(sprintf("%4d %25.25s |x| %-47.47s", $i, $artist, $title))));
177 }
178 else {
179 printf("%4d %25.25s | | %-47.47s\n",
180 $i, $artist, $title);
181 }
182 $i++;
183 }
184 return;
185 }
186  
187 sub list_all_playlists {
188 return wantarray()
189 ? sort($mpd->collection->all_playlists)
190 : scalar($mpd->collection->all_playlists)
191 ;
192 }
193  
194  
195 1;
196  
197 __END__