summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xclerk.pl43
1 files changed, 34 insertions, 9 deletions
diff --git a/clerk.pl b/clerk.pl
index 7da11c8..0822918 100755
--- a/clerk.pl
+++ b/clerk.pl
@@ -20,6 +20,7 @@ use HTTP::Date;
use Scalar::Util qw(looks_like_number);
use IPC::Run qw( timeout start );
use List::Util qw(any max maxstr);
+use List::UtilsBy qw(zip_by);
use Net::MPD;
use Pod::Usage qw(pod2usage);
use POSIX qw(tzset);
@@ -337,7 +338,7 @@ sub formatted_albums {
}
my @albums;
- my $fmtstr = join "", map {"%-${_}.${_}s\t"} ($rvar{max_width}->@{qw/albumartist date album/});
+ my $fmtrow = util_format_row($rvar{max_width}->@{qw/albumartist date album/}, 0);
my @skeys;
if ($sorted) {
@@ -348,8 +349,7 @@ sub formatted_albums {
for my $k (@skeys) {
my @vals = ((map { $_ // "Unknown" } $uniq_albums{$k}->@{qw/AlbumArtist Date Album/}), $uniq_albums{$k}->{Dir});
- my $strval = sprintf $fmtstr."%s\n", @vals;
- push @albums, $strval;
+ push @albums, $fmtrow->(@vals);
}
return \@albums;
@@ -357,13 +357,13 @@ sub formatted_albums {
sub formatted_tracks {
my ($rdb) = @_;
- my $fmtstr = join "", map {"%-${_}.${_}s\t"} ($rvar{max_width}->@{qw/track title artist album rating/});
- $fmtstr .= "%-s\n";
+ my $fmtrow = util_format_row($rvar{max_width}->@{qw/track title artist album rating/}, 0);
my @tracks = map {
- sprintf $fmtstr,
- (map { $_ // "-" } $_->@{qw/Track Title Artist Album/}),
- "r=" . ($_->{rating} // '0'),
- $_->{uri};
+ $fmtrow->(
+ (map { $_ // "-" } $_->@{qw/Track Title Artist Album/}),
+ "r=" . ($_->{rating} // '0'),
+ $_->{uri}
+ );
} @{$rdb};
return \@tracks;
@@ -546,6 +546,31 @@ sub util_parse_selection {
map { (split /[\t\n]/, $_)[-1] } (split /\n/, decode('UTF-8', $sel));
}
+sub util_try_init_widechar_support {
+ $main::{'util_strcols'} = eval { require Text::CharWidth; 1; }?
+ $Text::CharWidth::{'mbswidth'} : $CORE::{'length'};
+}
+
+BEGIN {
+ util_try_init_widechar_support();
+}
+
+sub util_format_row {
+ my @lengths = @_;
+
+ return sub {
+ my $line = join "\t", zip_by {
+ my ($maxlen, $word) = @_;
+
+ return $word if $maxlen == 0;
+ my $padlen = $maxlen - util_strcols($word);
+
+ $padlen < 0? substr($word, 0, $maxlen) : $word . (' ' x $padlen);
+ } \@lengths, \@_;
+ return $line .= "\n";
+ }
+}
+
sub mpd_add_items {
$mpd->add($_) for @{$_[0]};
}