Data/Unixish/trunc.pm

filter: Statementstmt 95.6% 44 / 46 Branchbran 80.0% 8 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 94.5% 69 / 73 CC 7 SCAR 11.0
CC
7
Cov
93%
_trunc_item
6.3
trunc
2.0
BEGIN
1.0
CRAP 3.0
« Data/Unixish/chain.pm ↑ Summary Data/Unixish/tail.pm »
1 package Data::Unixish::trunc;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use syntax 'each_on_array'; # to support perl < 5.12
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 #use Log::Any '$log';
9  
10 2 ▶use Data::Unixish::Util qw(%common_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Text::ANSI::Util qw(ta_trunc);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Text::ANSI::WideUtil qw(ta_mbtrunc);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Text::WideChar::Util qw(mbtrunc);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
16 2 our $DATE = '2025-02-24'; # DATE
17 2 our $DIST = 'Data-Unixish'; # DIST
18 2 our $VERSION = '1.574'; # VERSION
19  
20 2 our %SPEC;
21  
22 $SPEC{trunc} = {
23 2 v => 1.1,
24 summary => 'Truncate string to a certain column width',
25 description => <<'MARKDOWN',
26  
27 This function can handle text containing wide characters and ANSI escape codes.
28  
29 Note: to truncate by character instead of column width (note that wide
30 characters like Chinese can have width of more than 1 column in terminal), you
31 can turn of `mb` option even when your text contains wide characters.
32  
33 MARKDOWN
34 args => {
35 %common_args,
36 width => {
37 schema => ['int*', min => 0],
38 req => 1,
39 pos => 0,
40 cmdline_aliases => { w => {} },
41 },
42 ansi => {
43 summary => 'Whether to handle ANSI escape codes',
44 schema => ['bool', default => 0],
45 },
46 mb => {
47 summary => 'Whether to handle wide characters',
48 schema => ['bool', default => 0],
49 },
50 },
51 tags => [qw/itemfunc text/],
52 };
53 2 ▶sub trunc {
Subroutine trunc: called CC=2 SCAR=6.9
Pod documented
54 2 my %args = @_;
55 2 my ($in, $out) = ($args{in}, $args{out});
56  
57 2 ▶ while (my ($index, $item) = each @$in) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$in) 9 2 11
58 9 push @$out, _trunc_item($item, \%args);
59 }
60  
61 2 [200, "OK"];
62 }
63  
64 18 ▶sub _trunc_item {
Subroutine _trunc_item: called CC=6 SCAR=18.5
65 18 my ($item, $args) = @_;
66 18 ▶ return $item if !defined($item) || ref($item);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $item or ref $item 6 12 18
Condition100%3/3
not defined $item or ref $item
l!l&&r!l&&!r
2412
MC/DC100%2/2
not defined $item or ref $item
not defined $item ref $item
Truth table100%3/3
not(A) or B
A= not defined $itemB= ref $item
ABresult
1X1
011
000
67 12 ▶ if ($args->{ansi}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'ansi'}) { } 6 6 12
elsif ($args->{'mb'}) { } 0 6 6
68 6 ▶ if ($args->{mb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'mb'}) { } 0 6 6
69 0 return ta_mbtrunc($item, $args->{width});
70 } else {
71 6 return ta_trunc($item, $args->{width});
72 }
73 } elsif ($args->{mb}) {
74 0 return mbtrunc($item, $args->{width});
75 } else {
76 6 return substr($item, 0, $args->{width});
77 }
78 }
79  
80 2 1;
81 # ABSTRACT: Truncate string to a certain column width
82  
83 __END__
« Data/Unixish/chain.pm ↑ Summary Data/Unixish/tail.pm »