String...:Parts(3pm)

πŸ“¦
version 0.07 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– String::Elide::Parts πŸ’–

okay bestie, so we're doing String::Elide::Parts today β€” she's the elide module that gets priorities, and honestly? she ate. it's "Elide a string with multiple parts of different priorities" and the manual name is String::Elide::Parts(3pm) if you need to look her up later πŸŽ€

✨ VERSION ✨

this doc describes version 0.07 of String::Elide::Parts (she lives in the Perl distribution String-Elide-Parts), released on 2017-01-29. vintage queen πŸ’…

🌸 SYNOPSIS 🌸

first you gotta bring her in, no cap:

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

eliding a string with no parts at all (just vibes):

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 a string with multiple parts marked with markup β€” free the URL first (prio=3), then the "Downloading" text (prio=2), then the speed (prio=1, which is the default). watch her cook:

$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

look at the little ruler comments, they're giving very much graph paper energy πŸ“βœ¨


πŸ’œ DESCRIPTION πŸ’œ

String::Elide::Parts is like all the other string eliding modules you know, except β€” and this is the whole main character moment β€” she accepts strings marked up with parts that have different priorities. the goal? keep the important stuff alive for as long as possible while the length shrinks. prioritize correctly and she will simply not let the good info go first. it's giving self-care honestly 🫢


πŸ¦‹ FUNCTIONS πŸ¦‹

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

Elide a string if length exceeds $len.

your string can be marked up like "<elspan prio=N truncate=T marker=M>...</elspan>", which is how you get multiple parts with different priorities and different truncate directions. the default priority is 1. so: tag the less important strings with a higher priority and she'll elide those first, no cap. heads up though β€” the markup gets removed from the string before the eliding happens, so what you get back is clean ✨

Known options (the lore):

  • marker => str β€” default: '..'
  • truncate => 'left'|'middle'|'middle'|'ends' β€” default: 'right'
  • default_prio => int β€” default: 1
not me pointing out that the truncate list says 'middle' twice πŸ’€ i'm not fixing it, bestie, i'm reporting exactly what the page says. facts are facts.

🌈 HOMEPAGE 🌈

go visit the project's homepage, it's cute: <https://metacpan.org/release/String-Elide-Parts>

⭐ SOURCE ⭐

the source repo lives at: <https://github.com/perlancar/perl-String-Elide-Parts>

like, go star it, that's the whole ask πŸ™ˆ

πŸ’€ BUGS πŸ’€

got a bug or a feature request? drop it on the bugtracker: <https://rt.cpan.org/Public/Dist/Display.html?Name=String-Elide-Parts>

and when you submit a bug or a request, please include a test-file (or a patch to an existing test-file) that illustrates the bug or the desired feature. a vague "it's broken" is not the vibe check, bestie, we need receipts 🧾

πŸ‘‘ SEE ALSO πŸ‘‘

Similar elide modules

  • Text::Elide β€” simple, doesn't have many options, and elides at word boundaries.
  • String::Truncate β€” has a similar interface to String::Elide::Parts and has some options.
  • String::Elide::Lines β€” based on this module, but works on a line-basis.

squad goals fr fr 🌸


πŸ’– AUTHOR πŸ’–

perlancar <perlancar@cpan.org> β€” thank you for your service, icon 🫑

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

it's free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. periodt πŸ’…


okay that's the whole page and every single flag, default, example and link survived the vibe check ✨ go elide something responsibly, bestie.

this was page String...:Parts(3pm), perl v5.36.0, dated 2022-10-16. mwah, love u, bye πŸ¦‹πŸ’œ

πŸ“¦ where this page comes from