1 508e5f5f 2022-07-10 op #!/usr/bin/env perl
3 508e5f5f 2022-07-10 op # Copyright (c) 2022 Omar Polo <op@omarpolo.com>
5 508e5f5f 2022-07-10 op # Permission to use, copy, modify, and distribute this software for any
6 508e5f5f 2022-07-10 op # purpose with or without fee is hereby granted, provided that the above
7 508e5f5f 2022-07-10 op # copyright notice and this permission notice appear in all copies.
9 508e5f5f 2022-07-10 op # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 508e5f5f 2022-07-10 op # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 508e5f5f 2022-07-10 op # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 508e5f5f 2022-07-10 op # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 508e5f5f 2022-07-10 op # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 508e5f5f 2022-07-10 op # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 508e5f5f 2022-07-10 op # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 508e5f5f 2022-07-10 op use POSIX ":sys_wait_h";
23 508e5f5f 2022-07-10 op use Text::CharWidth qw(mbswidth);
24 508e5f5f 2022-07-10 op use IO::Poll qw(POLLIN);
25 508e5f5f 2022-07-10 op use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
26 508e5f5f 2022-07-10 op use Getopt::Long qw(:config bundling);
27 508e5f5f 2022-07-10 op use Pod::Usage;
32 508e5f5f 2022-07-10 op my $trim = "";
34 508e5f5f 2022-07-10 op my $pair_n = 1;
37 508e5f5f 2022-07-10 op my $playlist_cur;
38 508e5f5f 2022-07-10 op my $playlist_max;
44 508e5f5f 2022-07-10 op my $last_lines;
47 508e5f5f 2022-07-10 op my ($a, $b) = @_;
48 508e5f5f 2022-07-10 op return $a < $b ? $a : $b;
52 508e5f5f 2022-07-10 op my ($a, $b) = @_;
53 508e5f5f 2022-07-10 op return $a > $b ? $a : $b;
57 508e5f5f 2022-07-10 op my $lines = int(shift);
58 508e5f5f 2022-07-10 op my $ctx = $lines * 2 + 1;
61 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused show -p | grep -C$ctx '^>'");
64 508e5f5f 2022-07-10 op while (<$fh>) {
67 508e5f5f 2022-07-10 op push @tmp, $_;
68 508e5f5f 2022-07-10 op $cur = $x if m/^>/;
74 508e5f5f 2022-07-10 op if ($cur - $lines < 0) {
75 508e5f5f 2022-07-10 op $from = max($cur - $lines, 0);
76 508e5f5f 2022-07-10 op } elsif ($cur + $lines >= $#tmp) {
77 508e5f5f 2022-07-10 op $from = max($#tmp - $lines * 2 - 1, 0);
79 508e5f5f 2022-07-10 op $from = $cur - $lines;
82 508e5f5f 2022-07-10 op my $len = min($lines * 2 + 1, $#tmp);
83 508e5f5f 2022-07-10 op my @x = splice @tmp, $from, $len + 1;
87 508e5f5f 2022-07-10 op sub playlist_numbers {
88 508e5f5f 2022-07-10 op my ($cur, $tot, $found) = (0, 0, 0);
89 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused", "show", "-p");
90 508e5f5f 2022-07-10 op while (<$fh>) {
92 508e5f5f 2022-07-10 op $cur++ unless $found;
93 508e5f5f 2022-07-10 op $found = 1 if m/^>/;
96 508e5f5f 2022-07-10 op return ($cur, $tot);
100 508e5f5f 2022-07-10 op my ($pos, $dur, $repeat);
102 508e5f5f 2022-07-10 op open (my $fh, "-|", "amused", "status", "-f",
103 508e5f5f 2022-07-10 op "status,time:raw,repeat:oneline");
104 508e5f5f 2022-07-10 op my $status = <$fh>;
105 508e5f5f 2022-07-10 op chomp $status;
106 508e5f5f 2022-07-10 op while (<$fh>) {
108 508e5f5f 2022-07-10 op $pos = s/position //r if m/^position /;
109 508e5f5f 2022-07-10 op $dur = s/duration //r if m/^duration /;
110 508e5f5f 2022-07-10 op $repeat = $_ if m/^repeat/;
113 508e5f5f 2022-07-10 op return ($status, $pos, $dur, $repeat);
116 508e5f5f 2022-07-10 op sub showtime {
117 508e5f5f 2022-07-10 op my $seconds = shift;
118 508e5f5f 2022-07-10 op my $str = "";
120 508e5f5f 2022-07-10 op if ($seconds > 3600) {
121 508e5f5f 2022-07-10 op my $hours = int($seconds / 3600);
122 508e5f5f 2022-07-10 op $seconds -= $hours * 3600;
123 508e5f5f 2022-07-10 op $str = sprintf("%02:", $hours);
126 508e5f5f 2022-07-10 op my $minutes = int($seconds / 60);
127 508e5f5f 2022-07-10 op $seconds -= $minutes * 60;
128 508e5f5f 2022-07-10 op $str .= sprintf "%02d:%02d", $minutes, $seconds;
133 508e5f5f 2022-07-10 op my ($str, $pstr) = @_;
134 508e5f5f 2022-07-10 op my $width = mbswidth($str);
135 508e5f5f 2022-07-10 op return $str if $width > $COLS;
136 508e5f5f 2022-07-10 op my $pre = ($COLS - $width) / 2;
137 508e5f5f 2022-07-10 op my $lpad = $pstr x $pre;
138 508e5f5f 2022-07-10 op my $rpad = $pstr x ($COLS - $width - $pre + 1);
139 508e5f5f 2022-07-10 op return ($lpad, $str, $rpad);
142 508e5f5f 2022-07-10 op sub offsets {
143 508e5f5f 2022-07-10 op my ($y, $x, $cur, $max) = @_;
144 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center(" $cur / $max ", '-');
145 508e5f5f 2022-07-10 op addstring $y, $x, "";
147 508e5f5f 2022-07-10 op my $p = COLOR_PAIR($pair_n);
150 508e5f5f 2022-07-10 op addstring $pre;
153 508e5f5f 2022-07-10 op addstring $c;
156 508e5f5f 2022-07-10 op addstring $post;
160 508e5f5f 2022-07-10 op sub progress {
161 508e5f5f 2022-07-10 op my ($y, $x, $pos, $dur) = @_;
163 508e5f5f 2022-07-10 op my $pstr = showtime $pos;
164 508e5f5f 2022-07-10 op my $dstr = showtime $dur;
166 508e5f5f 2022-07-10 op my $len = $COLS - length($pstr) - length($dstr) - 4;
167 127f376a 2022-07-11 op return if $len <= 0 or $dur <= 0;
168 508e5f5f 2022-07-10 op my $filled = int($pos * $len / $dur);
170 508e5f5f 2022-07-10 op addstring $y, $x, "$pstr [";
171 508e5f5f 2022-07-10 op addstring "#" x $filled;
172 508e5f5f 2022-07-10 op addstring " " x max($len - $filled, 0);
173 508e5f5f 2022-07-10 op addstring "] $dstr";
176 508e5f5f 2022-07-10 op sub show_status {
177 508e5f5f 2022-07-10 op my ($y, $x, $status) = @_;
178 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center($status, ' ');
179 508e5f5f 2022-07-10 op addstring $y, $x, $pre;
180 508e5f5f 2022-07-10 op addstring $c;
181 508e5f5f 2022-07-10 op addstring $post;
184 508e5f5f 2022-07-10 op sub show_repeat {
185 508e5f5f 2022-07-10 op my ($y, $x, $repeat) = @_;
186 508e5f5f 2022-07-10 op my ($pre, $c, $post) = center($repeat, ' ');
187 508e5f5f 2022-07-10 op addstring $y, $x, $pre;
188 508e5f5f 2022-07-10 op addstring $c;
189 508e5f5f 2022-07-10 op addstring $post;
194 508e5f5f 2022-07-10 op if ($LINES < 4 || $COLS < 20) {
195 508e5f5f 2022-07-10 op addstring "window too small";
200 508e5f5f 2022-07-10 op my $song_pad = "";
201 508e5f5f 2022-07-10 op my $longest = 0;
202 508e5f5f 2022-07-10 op $longest = max $longest, length($_) foreach @songs;
203 508e5f5f 2022-07-10 op if ($longest < $COLS) {
204 508e5f5f 2022-07-10 op $song_pad = " " x (($COLS - $longest)/2);
207 508e5f5f 2022-07-10 op my $line = 0;
209 508e5f5f 2022-07-10 op attron(A_BOLD) if m/^>/;
210 508e5f5f 2022-07-10 op addstring $line++, 0, $song_pad . $_;
214 508e5f5f 2022-07-10 op offsets $LINES - 4, 0, $playlist_cur, $playlist_max;
215 508e5f5f 2022-07-10 op progress $LINES - 3, 0, $time_cur, $time_dur;
216 508e5f5f 2022-07-10 op show_status $LINES - 2, 0, $status;
217 508e5f5f 2022-07-10 op show_repeat $LINES - 1, 0, $repeat;
222 508e5f5f 2022-07-10 op sub getsongs {
223 508e5f5f 2022-07-10 op $last_lines = $LINES;
224 508e5f5f 2022-07-10 op @songs = excerpt($LINES / 2 - 3);
227 508e5f5f 2022-07-10 op sub getnums {
228 508e5f5f 2022-07-10 op ($playlist_cur, $playlist_max) = playlist_numbers;
231 508e5f5f 2022-07-10 op sub getstatus {
232 508e5f5f 2022-07-10 op ($status, $time_cur, $time_dur, $repeat) = status;
236 508e5f5f 2022-07-10 op return unless defined $pfile;
238 508e5f5f 2022-07-10 op open(my $fh, ">", $pfile);
239 508e5f5f 2022-07-10 op open(my $ph, "-|", "amused", "show", "-p");
241 508e5f5f 2022-07-10 op print $fh $_ while (<$ph>);
245 508e5f5f 2022-07-10 op my $fh = shift;
246 508e5f5f 2022-07-10 op my $l = <$fh>;
247 508e5f5f 2022-07-10 op die "monitor quit" unless defined($l);
249 508e5f5f 2022-07-10 op getnums if $l =~ m/load|jump|next|prev/;
250 508e5f5f 2022-07-10 op getsongs if $l =~ m/load|jump|next|prev/;
254 508e5f5f 2022-07-10 op my ($ch, $key) = getchar;
255 508e5f5f 2022-07-10 op if (defined $ch) {
256 508e5f5f 2022-07-10 op if ($ch eq " ") {
257 508e5f5f 2022-07-10 op system "amused", "toggle";
258 508e5f5f 2022-07-10 op } elsif ($ch eq "<" or $ch eq "p") {
259 508e5f5f 2022-07-10 op system "amused", "prev";
260 508e5f5f 2022-07-10 op } elsif ($ch eq ">" or $ch eq "n") {
261 508e5f5f 2022-07-10 op system "amused", "next";
262 508e5f5f 2022-07-10 op } elsif ($ch eq ",") {
263 508e5f5f 2022-07-10 op system "amused", "seek", "-5";
264 508e5f5f 2022-07-10 op } elsif ($ch eq ".") {
265 508e5f5f 2022-07-10 op system "amused", "seek", "+5";
266 508e5f5f 2022-07-10 op } elsif ($ch eq "S") {
267 508e5f5f 2022-07-10 op system "amused show | sort -u | amused load";
268 508e5f5f 2022-07-10 op } elsif ($ch eq "R") {
269 508e5f5f 2022-07-10 op system "amused show | sort -R | amused load";
270 508e5f5f 2022-07-10 op } elsif ($ch eq "s") {
272 508e5f5f 2022-07-10 op } elsif ($ch eq "q") {
275 508e5f5f 2022-07-10 op } elsif (defined $key) {
281 508e5f5f 2022-07-10 op "p:s" => \$pfile,
282 508e5f5f 2022-07-10 op "t:s" => \$trim,
283 508e5f5f 2022-07-10 op ) or pod2usage(1);
285 508e5f5f 2022-07-10 op my $mpid = open(my $monitor, "-|", "amused", "monitor",
286 508e5f5f 2022-07-10 op "load,jump,seek,next,prev,play,pause,toggle,stop,repeat")
287 508e5f5f 2022-07-10 op or die "can't spawn amused monitor";
291 508e5f5f 2022-07-10 op use_default_colors;
292 508e5f5f 2022-07-10 op init_pair $pair_n, 250, -1;
294 508e5f5f 2022-07-10 op timeout 1000;
298 508e5f5f 2022-07-10 op my $poll = IO::Poll->new();
299 508e5f5f 2022-07-10 op $poll->mask(\*STDIN => POLLIN);
300 508e5f5f 2022-07-10 op $poll->mask($monitor => POLLIN);
302 508e5f5f 2022-07-10 op my $tick = 0;
303 508e5f5f 2022-07-10 op my $tbefore = clock_gettime(CLOCK_MONOTONIC);
310 3a0b3cb8 2022-07-10 op while ($run) {
311 508e5f5f 2022-07-10 op $poll->poll(0.25);
312 508e5f5f 2022-07-10 op my $now = clock_gettime(CLOCK_MONOTONIC);
313 508e5f5f 2022-07-10 op my $elapsed = $now - $tbefore;
314 508e5f5f 2022-07-10 op if ($elapsed > 1) {
315 508e5f5f 2022-07-10 op $tbefore = $now;
316 508e5f5f 2022-07-10 op $time_cur += int($elapsed + 0.5)
317 508e5f5f 2022-07-10 op if $status =~ m/^playing/;
320 508e5f5f 2022-07-10 op ($status, $time_cur, $time_dur, $repeat) = status
321 508e5f5f 2022-07-10 op unless $tick++ % 8;
323 508e5f5f 2022-07-10 op hinput if $poll->events(\*STDIN) & POLLIN;
324 508e5f5f 2022-07-10 op hevent $monitor if $poll->events($monitor) & POLLIN;
326 508e5f5f 2022-07-10 op getsongs if $LINES != $last_lines;
334 508e5f5f 2022-07-10 op kill 'INT', $mpid;
343 508e5f5f 2022-07-10 op amused-monitor - curses interface for amused(1)
345 508e5f5f 2022-07-10 op =head1 SYNOPSIS
347 508e5f5f 2022-07-10 op B<amused-monitor> [B<-p> I<playlist>] [B<-t> I<string>]
349 508e5f5f 2022-07-10 op =head1 DESCRIPTION
351 508e5f5f 2022-07-10 op amused-monitor is a simple curses interface for amused(1).
353 508e5f5f 2022-07-10 op The following options are available:
357 508e5f5f 2022-07-10 op =item B<-p> I<playlist>
359 508e5f5f 2022-07-10 op Save the current playling queue to the file I<playlist> upon exit or
362 508e5f5f 2022-07-10 op =item B<-t> I<string>
364 508e5f5f 2022-07-10 op Trim out the given I<string> from every song in the playlist view.
368 508e5f5f 2022-07-10 op The following key-bindings are available:
374 508e5f5f 2022-07-10 op Toggle play/pause.
378 508e5f5f 2022-07-10 op Play previous song.
382 508e5f5f 2022-07-10 op Play next song.
386 508e5f5f 2022-07-10 op Seek backward by five seconds.
390 508e5f5f 2022-07-10 op Seek forward by five seconds.
394 508e5f5f 2022-07-10 op Randomize the playlist.
398 508e5f5f 2022-07-10 op Sort the playlist.
402 508e5f5f 2022-07-10 op Save the status to the file given with the B<-p> flag.
410 508e5f5f 2022-07-10 op =head1 SEE ALSO