logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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

Author

       perlancar <perlancar@cpan.org>

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.

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

Name

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

See Also

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

Source

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

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

Version

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

See Also