line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Data::Unixish::trunc; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2019-01-06'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '1.570'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
469
|
use 5.010; |
|
1
|
|
|
|
|
5
|
|
7
|
1
|
|
|
1
|
|
343
|
use locale; |
|
1
|
|
|
|
|
486
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
31
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
9
|
1
|
|
|
1
|
|
576
|
use syntax 'each_on_array'; # to support perl < 5.12 |
|
1
|
|
|
|
|
19977
|
|
|
1
|
|
|
|
|
4
|
|
10
|
1
|
|
|
1
|
|
2909
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
24
|
|
11
|
|
|
|
|
|
|
#use Log::Any '$log'; |
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
393
|
use Data::Unixish::Util qw(%common_args); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
117
|
|
14
|
1
|
|
|
1
|
|
400
|
use Text::ANSI::Util qw(ta_trunc); |
|
1
|
|
|
|
|
4411
|
|
|
1
|
|
|
|
|
57
|
|
15
|
1
|
|
|
1
|
|
355
|
use Text::ANSI::WideUtil qw(ta_mbtrunc); |
|
1
|
|
|
|
|
44221
|
|
|
1
|
|
|
|
|
61
|
|
16
|
1
|
|
|
1
|
|
7
|
use Text::WideChar::Util qw(mbtrunc); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
257
|
|
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
|
7
|
my %args = @_; |
53
|
2
|
|
|
|
|
5
|
my ($in, $out) = ($args{in}, $args{out}); |
54
|
|
|
|
|
|
|
|
55
|
2
|
|
|
|
|
10
|
while (my ($index, $item) = each @$in) { |
56
|
9
|
|
|
|
|
64
|
push @$out, _trunc_item($item, \%args); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
140
|
[200, "OK"]; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub _trunc_item { |
63
|
18
|
|
|
18
|
|
25
|
my ($item, $args) = @_; |
64
|
18
|
100
|
100
|
|
|
59
|
return $item if !defined($item) || ref($item); |
65
|
12
|
100
|
|
|
|
26
|
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
|
|
|
|
|
20
|
return substr($item, 0, $args->{width}); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; |
79
|
|
|
|
|
|
|
# ABSTRACT: Truncate string to a certain column width |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
__END__ |