File Coverage

blib/lib/String/Elide/Tiny.pm
Criterion Covered Total %
statement 24 25 96.0
branch 15 16 93.7
condition 4 4 100.0
subroutine 2 2 100.0
pod 1 1 100.0
total 46 48 95.8


line stmt bran cond sub pod time code
1             package String::Elide::Tiny;
2              
3             our $DATE = '2019-09-11'; # DATE
4             our $VERSION = '0.002'; # VERSION
5              
6             # be tiny
7             #use strict 'subs', 'vars';
8             #use warnings;
9              
10             sub import {
11 1     1   6 my $pkg = shift;
12 1         2 my $caller = caller;
13 1         2 for my $sym (@_) {
14 1 50       3 if ($sym eq 'elide') { *{"$caller\::$sym"} = \&{$sym} }
  1         2  
  1         1397  
  1         2  
15 0         0 else { die "$sym is not exported!" }
16             }
17             }
18              
19             sub elide {
20 17     17 1 140 my ($str, $max_len, $opts) = @_;
21              
22 17   100     47 $opts ||= {};
23              
24 17         23 my $str_len = length $str;
25              
26 17 100       36 my $marker = defined $opts->{marker} ? $opts->{marker} : "...";
27 17         20 my $marker_len = length $marker;
28 17 100       40 return substr($marker, 0, $max_len) if $max_len < $marker_len;
29              
30 16 100       44 return $str if $str_len <= $max_len;
31              
32 9   100     47 my $truncate = $opts->{truncate} || 'right';
33 9 100       27 if ($truncate eq 'left') {
    100          
    100          
34 1         6 return $marker . substr($str, $str_len - $max_len + $marker_len);
35             } elsif ($truncate eq 'middle') {
36 2         7 my $left = substr($str, 0,
37             ($max_len - $marker_len)/2);
38 2         5 my $right = substr($str,
39             $str_len - ($max_len - $marker_len - length($left)));
40 2         10 return $left . $marker . $right;
41             } elsif ($truncate eq 'ends') {
42 2 100       5 if ($max_len <= 2 * $marker_len) {
43 1         5 return substr($marker . $marker, 0, $max_len);
44             }
45 1         9 return $marker . substr($str, ($str_len - $max_len)/2 + $marker_len,
46             $max_len - 2*$marker_len) . $marker;
47             } else { # right
48 4         24 return substr($str, 0, $max_len - $marker_len) . $marker;
49             }
50             }
51              
52             1;
53             # ABSTRACT: A very simple text truncating function, elide()
54              
55             __END__
56              
57             =pod
58              
59             =encoding UTF-8
60              
61             =head1 NAME
62              
63             String::Elide::Tiny - A very simple text truncating function, elide()
64              
65             =head1 VERSION
66              
67             This document describes version 0.002 of String::Elide::Tiny (from Perl distribution String-Elide-Tiny), released on 2019-09-11.
68              
69             =head1 SYNOPSIS
70              
71             use String::Elide::Tiny qw(elide);
72              
73             # ruler: 0----5---10---15---20
74             my $text = "this is your brain";
75             elide($text, 16); # -> "this is your ..."
76             elide($text, 14); # -> "this is yo ..."
77              
78             # marker option:
79             elide($text, 14, {marker=>"xxx"}); # -> "this is youxxx"
80              
81             # truncate option:
82             elide($text, 14, {truncate=>"left"}); # -> "... your brain"
83             elide($text, 14, {truncate=>"middle"}); # -> "this ... brain"
84             elide($text, 14, {truncate=>"ends"}); # -> "...is your ..."
85              
86             =head1 DESCRIPTION
87              
88             This module offers L</elide>() function that is very simple; it's not
89             word-aware. It has options to choose marker or to select side(s) to truncate.
90              
91             =head1 FUNCTIONS
92              
93             =head2 elide
94              
95             Usage:
96              
97             my $truncated = elide($str, $max_len [ , \%opts ])
98              
99             Elide a string with " ..." if length exceeds C<$max_len>.
100              
101             Known options:
102              
103             =over
104              
105             =item * truncate
106              
107             String, either C<right>, C<left>, C<middle>, C<ends>.
108              
109             =item * marker
110              
111             String. Default: "...".
112              
113             =back
114              
115             =head1 HOMEPAGE
116              
117             Please visit the project's homepage at L<https://metacpan.org/release/String-Elide-Tiny>.
118              
119             =head1 SOURCE
120              
121             Source repository is at L<https://github.com/perlancar/perl-String-Elide-Tiny>.
122              
123             =head1 BUGS
124              
125             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=String-Elide-Tiny>
126              
127             When submitting a bug or request, please include a test-file or a
128             patch to an existing test-file that illustrates the bug or desired
129             feature.
130              
131             =head1 SEE ALSO
132              
133             L<Text::Elide> is also quite simple and elides at word boundaries, but it's not
134             tiny enough.
135              
136             L<Text::Truncate> is tiny enough, but does not support truncating at the
137             left/both ends.
138              
139             L<String::Elide::Parts> can elide at different points of the string.
140              
141             L<String::Truncate> has similar interface like String::Elide::Parts and has some
142             options. But it's not tiny: it has a couple of non-core dependencies.
143              
144             L<String::Elide::Lines> is based on this module but works on a line-basis.
145              
146             =head1 AUTHOR
147              
148             perlancar <perlancar@cpan.org>
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is copyright (c) 2019 by perlancar@cpan.org.
153              
154             This is free software; you can redistribute it and/or modify it under
155             the same terms as the Perl 5 programming language system itself.
156              
157             =cut