| 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.571'; # VERSION |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
574
|
use 5.010; |
|
|
1
|
|
|
|
|
6
|
|
|
7
|
1
|
|
|
1
|
|
405
|
use locale; |
|
|
1
|
|
|
|
|
579
|
|
|
|
1
|
|
|
|
|
6
|
|
|
8
|
1
|
|
|
1
|
|
90
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
9
|
1
|
|
|
1
|
|
376
|
use syntax 'each_on_array'; # to support perl < 5.12 |
|
|
1
|
|
|
|
|
24755
|
|
|
|
1
|
|
|
|
|
5
|
|
|
10
|
1
|
|
|
1
|
|
3865
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
27
|
|
|
11
|
|
|
|
|
|
|
#use Log::Any '$log'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
594
|
use Data::Unixish::Util qw(%common_args); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
128
|
|
|
14
|
1
|
|
|
1
|
|
454
|
use Text::ANSI::Util qw(ta_trunc); |
|
|
1
|
|
|
|
|
4920
|
|
|
|
1
|
|
|
|
|
96
|
|
|
15
|
1
|
|
|
1
|
|
582
|
use Text::ANSI::WideUtil qw(ta_mbtrunc); |
|
|
1
|
|
|
|
|
49754
|
|
|
|
1
|
|
|
|
|
62
|
|
|
16
|
1
|
|
|
1
|
|
10
|
use Text::WideChar::Util qw(mbtrunc); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
256
|
|
|
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
|
6
|
my %args = @_; |
|
53
|
2
|
|
|
|
|
5
|
my ($in, $out) = ($args{in}, $args{out}); |
|
54
|
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
10
|
while (my ($index, $item) = each @$in) { |
|
56
|
9
|
|
|
|
|
62
|
push @$out, _trunc_item($item, \%args); |
|
57
|
|
|
|
|
|
|
} |
|
58
|
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
127
|
[200, "OK"]; |
|
60
|
|
|
|
|
|
|
} |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _trunc_item { |
|
63
|
18
|
|
|
18
|
|
28
|
my ($item, $args) = @_; |
|
64
|
18
|
100
|
100
|
|
|
57
|
return $item if !defined($item) || ref($item); |
|
65
|
12
|
100
|
|
|
|
22
|
if ($args->{ansi}) { |
|
|
|
50
|
|
|
|
|
|
|
66
|
6
|
50
|
|
|
|
9
|
if ($args->{mb}) { |
|
67
|
0
|
|
|
|
|
0
|
return ta_mbtrunc($item, $args->{width}); |
|
68
|
|
|
|
|
|
|
} else { |
|
69
|
6
|
|
|
|
|
15
|
return ta_trunc($item, $args->{width}); |
|
70
|
|
|
|
|
|
|
} |
|
71
|
|
|
|
|
|
|
} elsif ($args->{mb}) { |
|
72
|
0
|
|
|
|
|
0
|
return mbtrunc($item, $args->{width}); |
|
73
|
|
|
|
|
|
|
} else { |
|
74
|
6
|
|
|
|
|
18
|
return substr($item, 0, $args->{width}); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
|
79
|
|
|
|
|
|
|
# ABSTRACT: Truncate string to a certain column width |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |