Skip to content

Instantly share code, notes, and snippets.

@xkr47
Last active November 4, 2015 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xkr47/6de6302ae4fd335202c1 to your computer and use it in GitHub Desktop.
Save xkr47/6de6302ae4fd335202c1 to your computer and use it in GitHub Desktop.
My xmms2 read-only terminal "gui"
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
sub nonEmptyString($) {
my ($str) = @_;
return defined($str) && length($str);
}
sub urlDecode($) {
my ($str) = @_;
return $str =~ s!%([0-9A-Fa-f][0-9A-Fa-f])!pack("H2",$1)!ger;
}
sub printlnWithUrldecodeAndColorReset($) {
my ($str) = @_;
print urlDecode($str),"\033[m\n" if (nonEmptyString($str));
}
sub decorateStringUnlessEmpty($$$) {
my ($str,$prefix,$suffix) = @_;
return undef unless(nonEmptyString($str));
return join("",$prefix,$str,$suffix);
}
sub grepNonEmptyStrings {
my @strlist = @_;
return grep { nonEmptyString($_) } @strlist;
}
sub printlnNonEmptyStringsWithUrldecodeAndColorReset {
my @strlist = @_;
printlnWithUrldecodeAndColorReset(join(" ",grepNonEmptyStrings(@strlist)));
}
sub getPlaylistLen() {
open P, '-|', 'COLUMNS=2000 x2 list' or die;
my $l = 0;
while (<P>) {
$l = $1 if(m!^..\[(\d+)/\d+\]!);
}
close P;
return $l;
}
sub main {
binmode(STDOUT, ':utf8');
# get info about currently playing song + what playlist is active
my @fields = qw(playback_status position tracknr channel artist album title playtime duration timesplayed url gain_track);
my $cmd = 'COLUMNS=2000 x2 current -f \''.join('@@',map { '${'.$_.'}' } @fields).'@@x\' ; x2 playlist config';
#print STDERR $cmd;
open P, '-|', $cmd or die;
$_ = <P>;
my $playlist = <P>;
close P;
chomp;
chomp $playlist;
my ($status,$position,$tracknr,$channel,$artist,$album,$title,$playtime,$duration,$timesplayed,$url,$gain_track,$x) = split(/@@/);
die "Incomplete data\n" unless(defined($x) && $x =~ m!\Ax\s*\z!);
# get playlist length
my $playlistlen = getPlaylistLen();
# tweaking values
unless(nonEmptyString($album)) {
my $urldecoded = $url =~ s!\+! !gr;
$urldecoded = urlDecode($urldecoded);
$urldecoded = urlDecode($urldecoded); # crappy double coded urls
$urldecoded =~ s!^https?://[^/]+/?!!;
$album = $urldecoded =~ s!/[^/]+$!!r;
$album =~ s!.*/!!;
}
$playlist =~ s!^name: !!;
$position = $position == 4294967295 ? "off-list" : $position + 1;
$title =~ s!\.(?:ogg|mp[234]|aac)!!i;
my $status_col = $status eq 'Stopped' ? "\033[31mStopped\033[m" : $status eq 'Paused' ? "\033[33mPaused\033[m" : $status eq 'Playing' ? "\033[32mPlaying\033[m" : $status;
# print
printlnNonEmptyStringsWithUrldecodeAndColorReset($status_col, '#'.$timesplayed, $playtime, decorateStringUnlessEmpty($duration, '/ ', ''), '(playlist '.$playlist.')', length($gain_track) ? "[\033[32mreplaygain\033[m]":"[\033[31mno replaygain\033[m]");
printlnNonEmptyStringsWithUrldecodeAndColorReset($channel);
printlnNonEmptyStringsWithUrldecodeAndColorReset(join(" / ", grepNonEmptyStrings($artist, $album)));
printlnNonEmptyStringsWithUrldecodeAndColorReset('['.$position.'/'.$playlistlen.']', decorateStringUnlessEmpty($tracknr, '', '.'), decorateStringUnlessEmpty($title,"\033[31;1m","\033[m"));
}
main();
#!/bin/sh
urxvt -bg '#111' -geometry 100x4-160-0 -name x2-stats -title x2-stats -fn "xft:Ubuntu Mono:pixelsize=12.2" -e watch -n 1 -tc x2-stats-text.pl &
@xkr47
Copy link
Author

xkr47 commented Oct 15, 2015

I have xmms2 symlinked to ~/bin/x2
x2-stats.sh is the main script, runs x2-stats-text.pl once a second to update the info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment