NAME

String::Elide::Parts - Elide a string with multiple parts of different priorities

VERSION

This document describes version 0.07 of String::Elide::Parts (from Perl distribution String-Elide-Parts), released on 2017-01-29.

SYNOPSIS

use String::Elide::Parts qw(elide);

Eliding string with no parts:

my $text = "this is your brain";
#                                            0----5---10---15---20
elide($text, 16);                       # -> "this is your ..."
elide($text, 16, {truncate=>"left"});   # -> "...is your brain"
elide($text, 16, {truncate=>"middle"}); # -> "this is... brain"
elide($text, 16, {truncate=>"ends"});   # -> "... is your b..."

elide($text, 16, {marker=>"--"});       # -> "this is your b--"

Eliding string with multiple parts marked with markup. We want to elide URL first (prio=3), then the Downloading text (prio=2), then the speed (prio=1, default):

$text = "<elspan prio=2>Downloading</elspan> <elspan prio=3 truncate=middle>http://www.example.com/somefile</elspan> 320.0k/5.5M";
#                      0----5---10---15---20---25---30---35---40---45---50---55---60
elide($text, 56); # -> "Downloading http://www.example.com/somefile 320.0k/5.5M"
elide($text, 55); # -> "Downloading http://www.example.com/somefile 320.0k/5.5M"
elide($text, 50); # -> "Downloading http://www.e..com/somefile 320.0k/5.5M"
elide($text, 45); # -> "Downloading http://ww..m/somefile 320.0k/5.5M"
elide($text, 40); # -> "Downloading http://..omefile 320.0k/5.5M"
elide($text, 35); # -> "Downloading http..efile 320.0k/5.5M"
elide($text, 30); # -> "Downloading ht..le 320.0k/5.5M"
elide($text, 25); # -> "Downloading . 320.0k/5.5M"
elide($text, 24); # -> "Downloading  320.0k/5.5M"
elide($text, 23); # -> "Download..  320.0k/5.5M"
elide($text, 20); # -> "Downl..  320.0k/5.5M"
elide($text, 15); # -> "..  320.0k/5.5M"
elide($text, 13); # -> "  320.0k/5.5M"
elide($text, 10); # -> " 320.0k/.."
elide($text,  5); # -> " 32.."
#                      0----5---10---15---20---25---30---35---40---45---50---55---60

DESCRIPTION

String::Elide::Parts is similar to other string eliding modules, with one main difference: it accepts string marked with parts of different priorities. The goal is to retain more important information as much as possible when length is reduced.

FUNCTIONS

elide($str, $len[, \%opts]) => str

Elide a string if length exceeds $len.

String can be marked with <elspan prio=N truncate=T marker=M>...</elspan> so there can be multiple parts with different priorities and truncate direction. The default priority is 1. You can mark less important strings with higher priority to let it be elided first. The markup will be removed from the string before eliding.

Known options:

  • marker => str (default: '..')

  • truncate => 'left'|'middle'|'middle'|'ends' (default: 'right')

  • default_prio => int (default: 1)

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/String-Elide-Parts.

SOURCE

Source repository is at https://github.com/perlancar/perl-String-Elide-Parts.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=String-Elide-Parts

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

Similar elide modules

Text::Elide is simple, does not have many options, and elides at word boundaries.

String::Truncate has similar interface like String::Elide::Parts and has some options.

String::Elide::Lines is based on this module but works on a line-basis.

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.