line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## no critic: TestingAndDebugging::RequireUseStrict |
2
|
|
|
|
|
|
|
package String::Elide::Tiny; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
# be tiny |
5
|
|
|
|
|
|
|
#IFUNBUILT |
6
|
|
|
|
|
|
|
# use strict; |
7
|
|
|
|
|
|
|
# use warnings; |
8
|
|
|
|
|
|
|
#END IFUNBUILT |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
11
|
|
|
|
|
|
|
our $DATE = '2023-08-08'; # DATE |
12
|
|
|
|
|
|
|
our $DIST = 'String-Elide-Tiny'; # DIST |
13
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub import { |
16
|
1
|
|
|
1
|
|
8
|
my $pkg = shift; |
17
|
1
|
|
|
|
|
2
|
my $caller = caller; |
18
|
|
|
|
|
|
|
#IFUNBUILT |
19
|
|
|
|
|
|
|
# no strict 'refs'; |
20
|
|
|
|
|
|
|
#END IFUNBUILT |
21
|
1
|
|
|
|
|
3
|
for my $sym (@_) { |
22
|
1
|
50
|
|
|
|
5
|
if ($sym eq 'elide') { *{"$caller\::$sym"} = \&{$sym} } |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
1599
|
|
|
1
|
|
|
|
|
3
|
|
23
|
0
|
|
|
|
|
0
|
else { die "$sym is not exported!" } |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub elide { |
28
|
25
|
|
|
25
|
1
|
196
|
my ($str, $max_len, $opts) = @_; |
29
|
|
|
|
|
|
|
|
30
|
25
|
50
|
|
|
|
63
|
die "Please specify str" unless defined $str; |
31
|
25
|
50
|
|
|
|
50
|
die "Please specify max_len" unless defined $max_len; |
32
|
|
|
|
|
|
|
|
33
|
25
|
100
|
|
|
|
61
|
return "" if $max_len <= 0; |
34
|
|
|
|
|
|
|
|
35
|
24
|
|
100
|
|
|
69
|
$opts ||= {}; |
36
|
|
|
|
|
|
|
|
37
|
24
|
|
|
|
|
35
|
my $str_len = length $str; |
38
|
24
|
100
|
|
|
|
68
|
return $str if $str_len <= $max_len; |
39
|
|
|
|
|
|
|
|
40
|
16
|
100
|
|
|
|
43
|
my $marker = defined $opts->{marker} ? $opts->{marker} : "..."; |
41
|
16
|
|
|
|
|
20
|
my $marker_len = length $marker; |
42
|
16
|
100
|
|
|
|
43
|
return substr($marker, 0, $max_len) if $max_len < $marker_len; |
43
|
|
|
|
|
|
|
|
44
|
13
|
|
100
|
|
|
39
|
my $truncate = $opts->{truncate} || 'right'; |
45
|
13
|
100
|
|
|
|
39
|
if ($truncate eq 'left') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
46
|
1
|
|
|
|
|
7
|
return $marker . substr($str, $str_len - $max_len + $marker_len); |
47
|
|
|
|
|
|
|
} elsif ($truncate eq 'middle') { |
48
|
2
|
|
|
|
|
8
|
my $left = substr($str, 0, |
49
|
|
|
|
|
|
|
($max_len - $marker_len)/2); |
50
|
2
|
|
|
|
|
5
|
my $right = substr($str, |
51
|
|
|
|
|
|
|
$str_len - ($max_len - $marker_len - length($left))); |
52
|
2
|
|
|
|
|
11
|
return $left . $marker . $right; |
53
|
|
|
|
|
|
|
} elsif ($truncate eq 'ends') { |
54
|
4
|
100
|
|
|
|
12
|
if ($max_len <= 2 * $marker_len) { |
55
|
3
|
|
|
|
|
15
|
return substr($marker . $marker, 0, $max_len); |
56
|
|
|
|
|
|
|
} |
57
|
1
|
|
|
|
|
9
|
return $marker . substr($str, ($str_len - $max_len)/2 + $marker_len, |
58
|
|
|
|
|
|
|
$max_len - 2*$marker_len) . $marker; |
59
|
|
|
|
|
|
|
} else { # right |
60
|
6
|
|
|
|
|
35
|
return substr($str, 0, $max_len - $marker_len) . $marker; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
1; |
65
|
|
|
|
|
|
|
# ABSTRACT: A very simple text truncating function, elide() |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
__END__ |