File: mprompt

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 (370 lines) | stat: -rwxr-xr-x 7,101 bytes parent folder | download | duplicates (3)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/perl
use strict;
use warnings;
use Audio::MPD q{0.19.0};
use MpdToys;
use Getopt::Long;
use Term::ReadKey;
use Encode;

=head1 NAME

mprompt - simple prompt-based control for mpd

=head1 SYNOPSIS

mpompt [-s] [-m key=key] [-t n] [-f] [tty] [-T] [host]

=cut

sub usage {
	die "Usage: mprompt [-s] [-m key=key] [-t n] [-f] [-t] [tty] [host]\n";
}

=head1 DESCRIPTION

B<mprompt> is a mpd client with a prompt-based interface. It is 
designed to be usable on a headless machine.

At the prompt, enter the name of a playlist, or part of the name of an
album, artist, or song. Matching items will start playing. You can also
paste in urls to stream.

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

Use the left and right arrow keys to adjust volume, and the up and down
arrow keys to move through the playlist. 

The Tab and Enter keys can both be used to pause and unpause playback.
(Enter toggles pause only if nothing has been entered at the prompt.)

Example of how to run mprompt in /etc/inittab:

	1:2345:respawn:/usr/bin/mprompt /dev/tty1

=head1 OPTIONS

=over 4

=item -s

This option allows shell commands to be typed in to mprompt, to be
run by whatever user it is running as. (Typically root if it is run from
/etc/inittab).

To enter a shell command, type a "!", followed by the command to run,
followed by Enter.

=item -m key=key

This option allows remapping keys. Any key can be remapped to any other
key, which is useful to support keyboard with unusual key layouts, or
missing keys.

For alphanumeric and punctuation keys, individual symbols can be remapped.
For example, "-m a=b" will turn each entered "a" into "b".

For other keys, use the following names:

=over 4

=item <return>

=item <tab>

=item <space>

=item <up>

=item <down>

=item <left>

=item <right>

=item <backspace>

=back

For example, -m "n=<down>" will map the "n" key to the down arrow, causing
that key to change to the next track; -m "<space>=<return>" will make the space
bar act as a pause.

It's possible to swap keys too. For example, -m "<down>=<up>" -m "<up>=<down>"

A single key can also be bound to a series of keystrokes. For example,
-m "1=Mule Variations<return>" will cause the "1" key to play the "Mule
Variations" album, a nice choice.

=item -t n

Adds a timeout, a specified number of seconds after which the entry
on the command line will be cleared. Useful for headless systems, to avoid
cat-on-keyboard confusing your later commands.

=item -T

Enables terse output mode. This mode tries to avoid displaying excessive
or complex things, with the intent that mprompt's output can be piped into
a speech synthesiser, such as espeak.

=back

=head1 SEE ALSO

vipl(1) mptoggle(1) mpd(1)

=head1 AUTHOR

Copyright 2009 Joey Hess <joey@kitenet.net>

Licensed under the GNU GPL version 2 or higher.

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

=cut

my $tty;
my $shell=0;
my $timeout=0;
my $terse=0;
my %controlchars = GetControlChars;
my %keysyms = (
	"\n" => '<return>',
	"\t" => '<tab>',
	" " =>  '<space>',
	"\e[A" => '<up>',
	"\e[3~" => '<up>', # delete on some terminals, raw on others
	"\e[B" => '<down>',
	"\e[D" => '<left>',
	"\e[C" => '<right>',
	$controlchars{ERASE} => "<backspace>",
);
my %keymap;

Getopt::Long::Configure("no_ignore_case");
GetOptions(
	"s" => \$shell,
	"m=s" => sub {
		my ($old, $new)=split(/=/, $_[1], 2);
		$keymap{$old}=$new;
	},
	"t=i" => \$timeout,
	"f" => sub { print STDERR "the -f option is now enabled by default\n" },
	"T" => \$terse,
) || usage();

if (@ARGV) {
	$tty=shift;
	close STDIN;
	close STDOUT;
	open(STDIN, "<", $tty) || die "open $tty: $!";
	open(STDOUT, ">", $tty) || die "open $tty: $!";
}
if (@ARGV) {
	$ENV{MPD_HOST}=shift;
}
my $mpd=Audio::MPD->new(conntype => "reuse");

sub quit {
	ReadMode("restore");
	exit(0);
};
$SIG{INT}=$SIG{TERM}=\&quit;
ReadMode("raw");

$|=1;

my $line="";
my $sequence;
my $laststroke=time;

showprompt();

KEY: while (my $key = ReadKey(0)) {
	if ($timeout) {
	       if (length $line && time - $laststroke > $timeout) {
			$line="";
			print "  <timeout>\n" unless $terse;
			showprompt();
		}
		$laststroke=time;
	}

	if ($key eq $controlchars{INTERRUPT} ||
	    $key eq $controlchars{EOF}) {
		quit();
	}

	# Sequences are started with escape, and accumulated
	# until a recognised sequence is seen, or until it becomes clear
	# that it is not part of a recognised sequence.
	if (defined $sequence) {
		$sequence.=$key;
		if (exists $keysyms{$sequence}) {
			$key=$sequence;
			$sequence=undef;
		}
		else {
			foreach my $sym (keys %keysyms) {
				if ($sym=~/^\Q$sequence\E/) {
					next KEY; # unfinished sequence
				}
			}
			$key=$sequence;
			$sequence=undef;
		}
	}

	$key = $keysyms{$key} if exists $keysyms{$key};
	$key = $keymap{$key} if exists $keymap{$key};
	
	# The key may be mapped to a multiple letter sequence.
	while (length $key) {
		if ($key=~s/^(<[^>]+>)//) {
			handle($1);
		}
		elsif ($key=~s/(.)//) {
			handle($1);
		}
	}
}

sub handle {
	my $key=shift;

	if ($key eq "\e") {
		$sequence=$key;
	}
	elsif ($key eq '<return>') {
		if ($shell && $line =~ /^\!(.*)/) {
			print "\nrunning $1\n";
			system($1);
		}
		elsif (length $line && $line !~ /^\s*$/) {
			queue($line);
		}
		else {
			toggle();
		}
		$line="";
		showprompt();
	}
	elsif ($key eq '<backspace>') {
		if (length $line) {
			chop $line;
			print "\b \b";
		}
	}
	elsif ($key eq '<space>') {
		print " ";
		$line.=" ";
	}
	elsif ($key eq '<tab>') {
		toggle();
		showprompt();
	}
	elsif ($key eq '<left>') {
		adjustvolume(-5);
	}
	elsif ($key eq '<right>') {
		adjustvolume(+5);
	}
	elsif ($key eq '<up>') {
		$mpd->prev;
		$mpd->play;
		showplaying();
		showprompt();
	}
	elsif ($key eq '<down>') {
		$mpd->next;
		$mpd->play;
		showplaying();
		showprompt();
	}
	else {
		print "$key";
		$line.=$key;
	}
}
		
sub adjustvolume {
	my $amount=shift;
	my $vol=$mpd->status->volume;

	$vol+=$amount;
	if ($vol > 100) {
		$vol=100;
	}
	elsif ($vol < 0) {
		$vol=0;
	}

	if (! $terse) {
		print "\nvolume: $vol%\n";
	}
	$mpd->volume($vol);
	showprompt();
}

sub showprompt {
	print "> $line";
}

sub showplaying {
	print "\n";
	my $song=$mpd->current;
	if (! defined $song) {
		print "nothing queued\n";
	}
	else {
		if (! $terse) {
			print encode_utf8($song->as_string)."\n";
		}
	}
}

sub toggle {
	$mpd->pause;
	my $state=$mpd->status->state;
	print "\n";
	print "$state\n" if ! $terse || $state ne "play";
}

sub queue {
	my $line=shift;

	print "\n";

	my $pl=$mpd->playlist;

	eval q{$pl->load($line)};
	if (! $@) {
		$pl->clear;
		$pl->load($line);
		$mpd->play;

		print "added $line playlist";
		showplaying();
		return;
	}

	my @matches=MpdToys::findmatchingsongs($line, $mpd);
	if (! @matches && MpdToys::canmatch_fuzzy()) {
		print "trying fuzzy match..\n";
		@matches=MpdToys::findmatchingsongs_fuzzy($line, $mpd);
	}
	if (@matches) {
		$pl->clear;
		foreach (@matches) {
			$pl->add($_->file);
		}
		$mpd->play;
		print "added ".int(@matches)." songs";
		showplaying();
	}
	else {
		print "no matches found for \"$line\"\n";
	}
}