File: vipl

package info (click to toggle)
mpdtoys 0.24
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 104 kB
  • ctags: 22
  • sloc: perl: 842; sh: 58; makefile: 21
file content (161 lines) | stat: -rwxr-xr-x 3,439 bytes parent folder | download | duplicates (5)
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
153
154
155
156
157
158
159
160
161
#!/usr/bin/perl

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

=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 the name of a playlist, or part of the name of an
album, artist, or song. Matching items will be added to the playlist.
Streaming urls can also be entered.

(If the perl String::Approx module is available, it will be used to handle
typos, etc in the names you enter.)

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.

=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 => "plXXXXX", 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 encode_utf8($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;
	$_=decode_utf8($_);
	
	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 @matches=MpdToys::findmatchingsongs($_, $mpd);
		if (! @matches && MpdToys::canmatch_fuzzy()) {
			@matches=MpdToys::findmatchingsongs_fuzzy($_, $mpd);
		}

		if (! @matches) {
			print STDERR "$0: no matches for \"$_\"\n";
		}
		else {
			foreach (@matches) {
				push @list, $_;
			}
			$changed=1;
			$num+=@matches;
		}
	}
}
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;
}