File: vipl

package info (click to toggle)
mpdtoys 0.8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 80 kB
  • ctags: 6
  • sloc: perl: 495; sh: 58; makefile: 18
file content (152 lines) | stat: -rwxr-xr-x 3,235 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/perl

use warnings;
use strict;
use File::Spec;
use File::Temp;
use Audio::MPD q{0.19.0};

=head1 NAME

vipl - edit mpd playlist

=head1 SYNOPSIS

B<vipl> [host]

=head1 DESCRIPTION

vipl allows editing the mpd playlist using your text editor. The current
playlist will be brought up in the editor. Delete or rearrange songs as
desired using the editor. You can also enter parts of the names of songs,
albums, or artists, and they will be expanded to matching items from the
music library.

The currently playing song is marked with a ">" at the front. To change
which song is playing, just move the ">" to a different song.

If the hostname is omitted, the MPD_HOST environment variable will be used.

=back

=head1 AUTHOR

Copyright 2008 by Joey Hess <joey@kitenet.net>

Licensed under the GNU GPL.

=cut

if (@ARGV) {
	$ENV{MPD_HOST}=shift;
}
my $mpd=Audio::MPD->new(conntype => $REUSE);
my $current=$mpd->current;
my $pl=$mpd->playlist;
my %names=map { $_->as_string => $_ } $pl->as_items;
my @origlist=$pl->as_items;

my $tmp=File::Temp->new(TEMPLATE => "dirXXXXX", DIR => File::Spec->tmpdir);
open (OUT, ">".$tmp->filename) || die "$0: cannot create ".$tmp->filename.": $!\n";
foreach my $song (@origlist) {
	print OUT ">" if defined $current && $song->id eq $current->id;
	print OUT $song->as_string;
	print OUT "\n";
}
close OUT || die "$0: cannot write ".$tmp->filename.": $!\n";

my @editor="vi";
if (-x "/usr/bin/editor") {
	@editor="/usr/bin/editor";
}
if (exists $ENV{EDITOR}) {
	@editor=split(' ', $ENV{EDITOR});
}
if (exists $ENV{VISUAL}) {
	@editor=split(' ', $ENV{VISUAL});
}
my $ret=system(@editor, $tmp);
if ($ret != 0) {
	die "@editor exited nonzero, aborting\n";
}

my $changed=0;
my @list;
open (IN, "<".$tmp->filename) || die "$0: cannot read ".$tmp->filename.": $!\n";
my $playing;
my $num=0;
while (<IN>) {
	chomp;
	
	if (s/^>\s*//) {
		$playing=$num;
	}

	if (exists $names{$_}) {
		push @list, $names{$_};
		if (! $changed && !($origlist[$#list] &&
		    $names{$_}->file eq $origlist[$#list]->file)) {
			$changed=1;
		}
		$num++;
	}
	else {
		next if /^\s*$/;

		my $coll=$mpd->collection;
		my @matches = ($coll->songs_with_title_partial($_),
		               $coll->songs_from_album_partial($_),
		               $coll->songs_by_artist_partial($_));
		if (! @matches) {
			print STDERR "$0: no matches for \"$_\"\n";
		}
		else {
			push @list, @matches;
			$changed=1;
		}
		$num+=@list;
	}
}
close IN;
if ($#list != $#origlist) {
	$changed=1;
}

if (! $changed) {
	# yay for optimisation!
}
elsif ($current && grep { $_->file eq $current->file } @list) {
	# Avoid touching the currently playing song, while changing
	# the playlist around it.
	$pl->crop;
	my $pos=0;
	my $past=0;
	foreach my $song (@list) {
		if (! $past && $song->file eq $current->file) {
			$past=1;
			# move current song into right location
			$pl->move(0, $pos);
		}
		else {
			$pl->add($song->file);
		}
		$pos++;
	}
	$mpd->play if @list;
}
else {
	$pl->clear;
	foreach my $song (@list) {
		$pl->add($song->file);
	}
}

if (defined $playing) {
	if (! defined $mpd->status->song || $playing ne $mpd->status->song ||
	    $mpd->status->state ne 'play') {
		$mpd->play($playing);
	}
}
else {
	$mpd->play(0) if @list;
}