summaryrefslogtreecommitdiff
path: root/mpinsert
blob: 480d7cf0e7f9403b47ce39e0421fdff88170d90f (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use Audio::MPD q{0.19.0};
use Data::Dumper;

=head1 NAME

mpinsert - insert song after currently playing song

=head1 SYNOPSIS

mpinsert [song ..]

=head1 DESCRIPTION

B<mpinsert> inserts a song into the playlist directly after the currently
playing song.

If no songs are specified on the command line, it will read a list from
stdin.

=head1 AUTHOR

Copyright 2007 Joey Hess <joey@kitenet.net>

Licensed under the GNU GPL version 2 or higher.

http://kitenet.net/~joey/code/mpdtoys

=cut

my @list=@ARGV;
if (! @list) {
	while (<>) {
		chomp;
		push @list, $_;
	}
}

my $mpd=Audio::MPD->new(conntype => $REUSE);
my $pl=$mpd->playlist;
my $current=$mpd->current;

foreach my $item (reverse @list) {
	if (! length $item) {
		die "no item specified to insert\n";
	}
	$pl->add($item);
	
	# move from end to just after current
	if ($current) {
		my @items=$pl->as_items;
		if (! @items) {
			die "failed!";
		}
		$pl->move($#items, $current->pos+1);
	}
}