String::Elide::Parts(3pm)

String::Elide::Parts3pm

Elide a string with multiple parts of different priorities

Usage

 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

Version

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

Overview

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>.

Releases

3 releases since Debian 11 (Bullseye); the text changed 1 time.

  1. 11
  2. 12–13

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.

Author

perlancar <[email protected]>

From libstring-elide-parts-perl 0.07-3 in Debian 13 (Trixie). Parsed by carta from the page's man source.