File Coverage

blib/lib/Data/Unixish/trunc.pm
Criterion Covered Total %
statement 37 39 94.8
branch 6 8 75.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 1 1 100.0
total 58 62 93.5


line stmt bran cond sub pod time code
1             package Data::Unixish::trunc;
2              
3             our $DATE = '2019-10-26'; # DATE
4             our $VERSION = '1.572'; # VERSION
5              
6 1     1   484 use 5.010;
  1         6  
7 1     1   375 use locale;
  1         528  
  1         4  
8 1     1   32 use strict;
  1         1  
  1         19  
9 1     1   330 use syntax 'each_on_array'; # to support perl < 5.12
  1         19641  
  1         4  
10 1     1   3059 use warnings;
  1         2  
  1         25  
11             #use Log::Any '$log';
12              
13 1     1   401 use Data::Unixish::Util qw(%common_args);
  1         3  
  1         108  
14 1     1   408 use Text::ANSI::Util qw(ta_trunc);
  1         4965  
  1         64  
15 1     1   427 use Text::ANSI::WideUtil qw(ta_mbtrunc);
  1         45477  
  1         60  
16 1     1   6 use Text::WideChar::Util qw(mbtrunc);
  1         2  
  1         337  
17              
18             our %SPEC;
19              
20             $SPEC{trunc} = {
21             v => 1.1,
22             summary => 'Truncate string to a certain column width',
23             description => <<'_',
24              
25             This function can handle text containing wide characters and ANSI escape codes.
26              
27             Note: to truncate by character instead of column width (note that wide
28             characters like Chinese can have width of more than 1 column in terminal), you
29             can turn of `mb` option even when your text contains wide characters.
30              
31             _
32             args => {
33             %common_args,
34             width => {
35             schema => ['int*', min => 0],
36             req => 1,
37             pos => 0,
38             cmdline_aliases => { w => {} },
39             },
40             ansi => {
41             summary => 'Whether to handle ANSI escape codes',
42             schema => ['bool', default => 0],
43             },
44             mb => {
45             summary => 'Whether to handle wide characters',
46             schema => ['bool', default => 0],
47             },
48             },
49             tags => [qw/itemfunc text/],
50             };
51             sub trunc {
52 2     2 1 5 my %args = @_;
53 2         5 my ($in, $out) = ($args{in}, $args{out});
54              
55 2         9 while (my ($index, $item) = each @$in) {
56 9         56 push @$out, _trunc_item($item, \%args);
57             }
58              
59 2         108 [200, "OK"];
60             }
61              
62             sub _trunc_item {
63 18     18   26 my ($item, $args) = @_;
64 18 100 100     59 return $item if !defined($item) || ref($item);
65 12 100       19 if ($args->{ansi}) {
    50          
66 6 50       12 if ($args->{mb}) {
67 0         0 return ta_mbtrunc($item, $args->{width});
68             } else {
69 6         12 return ta_trunc($item, $args->{width});
70             }
71             } elsif ($args->{mb}) {
72 0         0 return mbtrunc($item, $args->{width});
73             } else {
74 6         21 return substr($item, 0, $args->{width});
75             }
76             }
77              
78             1;
79             # ABSTRACT: Truncate string to a certain column width
80              
81             __END__