diff options
author | yushyin <yuyushyin@gmail.com> | 2017-10-09 23:02:22 +0200 |
---|---|---|
committer | yushyin <yuyushyin@gmail.com> | 2017-10-09 23:52:31 +0200 |
commit | 21e844c17ee40e2e29935b07bb441420cc19cf0e (patch) | |
tree | 2a7c8af309acefbbc456e52f67b271f8dee0f43d | |
parent | ed02e2436923617a75c689a0cdd71df08ab56ab3 (diff) | |
download | perl-app-clerk-feature/widechar.tar.gz perl-app-clerk-feature/widechar.tar.xz |
Optional support for wide characters in tracks and albums viewfeature/widechar
-rwxr-xr-x | clerk.pl | 43 |
1 files changed, 34 insertions, 9 deletions
@@ -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]}; } |