1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
use strict;
use warnings;
package String::Truncate 1.100603;
# ABSTRACT: a module for when strings are too long to be displayed in...
use Carp qw(croak);
use Sub::Install 0.03 qw(install_sub);
#pod =head1 SYNOPSIS
#pod
#pod This module handles the simple but common problem of long strings and finite
#pod terminal width. It can convert:
#pod
#pod "this is your brain" -> "this is your ..."
#pod or "...is your brain"
#pod or "this is... brain"
#pod or "... is your b..."
#pod
#pod It's simple:
#pod
#pod use String::Truncate qw(elide);
#pod
#pod my $brain = "this is your brain";
#pod
#pod elide($brain, 16); # first option
#pod elide($brain, 16, { truncate => 'left' }); # second option
#pod elide($brain, 16, { truncate => 'middle' }); # third option
#pod elide($brain, 16, { truncate => 'ends' }); # fourth option
#pod
#pod String::Trunc::trunc($brain, 16); # => "this is your bra"
#pod
#pod =func elide
#pod
#pod elide($string, $length, \%arg)
#pod
#pod This function returns the string, if it is less than or equal to C<$length>
#pod characters long. If it is longer, it truncates the string and marks the
#pod elision.
#pod
#pod Valid arguments are:
#pod
#pod truncate - elide at left, right, middle, or ends? (default: right)
#pod marker - how to mark the elision (default: ...)
#pod at_space - if true, strings will be broken at whitespace if possible
#pod
#pod =cut
my %elider_for = (
right => \&_elide_right,
left => \&_elide_left,
middle => \&_elide_middle,
ends => \&_elide_ends,
);
sub _elide_right {
&_assert_1ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $keep = $length - length($marker);
if ($at_space) {
my ($substr) = $string =~ /\A(.{0,$keep})\s/s;
$substr = substr($string, 0, $keep)
unless defined $substr and length $substr;
return $substr . $marker;
} else {
return substr($string, 0, $keep) . $marker;
}
}
sub _elide_left {
&_assert_1ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $keep = $length - length($marker);
return $marker
. reverse(_elide_right(scalar reverse($string), $keep, q{}, $at_space));
}
sub _elide_middle {
&_assert_1ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $keep = $length - length($marker);
my ($keep_left, $keep_right) = (int($keep / 2)) x 2;
$keep_left +=1 if ($keep_left + $keep_right) < $keep;
return _elide_right($string, $keep_left, q{}, $at_space)
. $marker
. _elide_left($string, $keep_right, q{}, $at_space)
}
sub _elide_ends {
&_assert_2ML; ## no critic Ampersand
my ($string, $length, $marker, $at_space) = @_;
my $midpoint = int(length($string) / 2);
my $each = int($length / 2);
return _elide_left(substr($string, 0, $midpoint), $each, $marker, $at_space)
. _elide_right(substr($string, -$midpoint), $each, $marker, $at_space)
}
sub _assert_1ML {
my ($string, $length, $marker) = @_;
croak "elision marker <$marker> is longer than allowed length $length!"
if length($marker) > $length;
}
sub _assert_2ML {
my ($string, $length, $marker) = @_;
# this should only complain if needed: elide('foobar', 3, {marker=>'...'})
# should be ok -- rjbs, 2006-02-24
croak "two elision markers <$marker> are longer than allowed length $length!"
if (length($marker) * 2) > $length;
}
sub elide {
my ($string, $length, $arg) = @_;
$arg = {} unless $arg;
my $truncate = $arg->{truncate} || 'right';
croak "invalid value for truncate argument: $truncate"
unless my $elider = $elider_for{ $truncate };
# hey, this might be really easy:
return $string if length($string) <= $length;
my $marker = defined $arg->{marker} ? $arg->{marker} : '...';
my $at_space = defined $arg->{at_space} ? $arg->{at_space} : 0;
return $elider->($string, $length, $marker, $at_space);
}
#pod =func trunc
#pod
#pod trunc($string, $length, \%arg)
#pod
#pod This acts just like C<elide>, but assumes an empty marker, so it actually
#pod truncates the string normally.
#pod
#pod =cut
sub trunc {
my ($string, $length, $arg) = @_;
$arg = {} unless $arg;
croak "marker may not be passed to trunc()" if exists $arg->{marker};
$arg->{marker} = q{};
return elide($string, $length, $arg);
}
#pod =head1 IMPORTING
#pod
#pod String::Truncate exports both C<elide> and C<trunc>, and also supports the
#pod Exporter-style ":all" tag.
#pod
#pod use String::Truncate (); # export nothing
#pod use String::Truncate qw(elide); # export just elide()
#pod use String::Truncate qw(:all); # export both elide() and trunc()
#pod use String::Truncate qw(-all); # export both elide() and trunc()
#pod
#pod When exporting, you may also supply default values:
#pod
#pod use String::Truncate -all => defaults => { length => 10, marker => '--' };
#pod
#pod # or
#pod
#pod use String::Truncate -all => { length => 10, marker => '--' };
#pod
#pod These values affect only the imported version of the functions. You may pass
#pod arguments as usual to override them, and you may call the subroutine by its
#pod fully-qualified name to get the standard behavior.
#pod
#pod =cut
use Sub::Exporter::Util ();
use Sub::Exporter 0.953 -setup => {
exports => {
Sub::Exporter::Util::merge_col(defaults => {
trunc => sub { trunc_with_defaults($_[2]) },
elide => sub { elide_with_defaults($_[2]) },
})
},
collectors => [ qw(defaults) ]
};
#pod =head1 BUILDING CODEREFS
#pod
#pod The imported builds and installs lexical closures (code references) that merge
#pod in given values to the defaults. You can build your own closures without
#pod importing them into your namespace. To do this, use the C<elide_with_defaults>
#pod and C<trunc_with_defaults> routines.
#pod
#pod =head2 elide_with_defaults
#pod
#pod my $elider = String::Truncate::elide_with_defaults(\%arg);
#pod
#pod This routine, never exported, builds a coderef which behaves like C<elide>, but
#pod uses default values when needed. All the valid arguments to C<elide> are valid
#pod here, as well as C<length>.
#pod
#pod =cut
sub _code_with_defaults {
my ($code, $skip_defaults) = @_;
sub {
my $defaults = shift || {};
my %defaults = %$defaults;
delete $defaults{$_} for @$skip_defaults;
my $length = delete $defaults{length};
sub {
my $string = $_[0];
my $length = defined $_[1] ? $_[1] : $length;
my $arg = { %defaults, (defined $_[2] ? %{ $_[2] } : ()) };
return $code->($string, $length, $arg);
}
}
}
BEGIN {
install_sub({
code => _code_with_defaults(\&elide),
as => 'elide_with_defaults',
});
}
#pod =head2 trunc_with_defaults
#pod
#pod This routine behaves exactly like elide_with_defaults, with one obvious
#pod exception: it returns code that works like C<trunc> rather than C<elide>. If a
#pod C<marker> argument is passed, it is ignored.
#pod
#pod =cut
BEGIN {
install_sub({
code => _code_with_defaults(\&trunc, ['marker']),
as => 'trunc_with_defaults',
});
}
#pod =head1 SEE ALSO
#pod
#pod L<Text::Truncate> does a very similar thing. So does L<Text::Elide>.
#pod
#pod =head1 BUGS
#pod
#pod Please report any bugs or feature requests through the web interface at
#pod L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Truncate>. I will be
#pod notified, and then you'll automatically be notified of progress on your bug as
#pod I make changes.
#pod
#pod =head1 ACKNOWLEDGEMENTS
#pod
#pod Ian Langworth gave me some good advice about naming things. (Also some bad
#pod jokes. Nobody wants String::ETOOLONG, Ian.) Hans Dieter Pearcey suggested
#pod allowing defaults just in time for a long bus ride, and I was rescued from
#pod boredom by that suggestion
#pod
#pod =cut
1; # End of String::Truncate
__END__
=pod
=encoding UTF-8
=head1 NAME
String::Truncate - a module for when strings are too long to be displayed in...
=head1 VERSION
version 1.100603
=head1 SYNOPSIS
This module handles the simple but common problem of long strings and finite
terminal width. It can convert:
"this is your brain" -> "this is your ..."
or "...is your brain"
or "this is... brain"
or "... is your b..."
It's simple:
use String::Truncate qw(elide);
my $brain = "this is your brain";
elide($brain, 16); # first option
elide($brain, 16, { truncate => 'left' }); # second option
elide($brain, 16, { truncate => 'middle' }); # third option
elide($brain, 16, { truncate => 'ends' }); # fourth option
String::Trunc::trunc($brain, 16); # => "this is your bra"
=head1 PERL VERSION
This library should run on perls released even a long time ago. It should work
on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased. The version may be increased
for any reason, and there is no promise that patches will be accepted to lower
the minimum required perl.
=head1 FUNCTIONS
=head2 elide
elide($string, $length, \%arg)
This function returns the string, if it is less than or equal to C<$length>
characters long. If it is longer, it truncates the string and marks the
elision.
Valid arguments are:
truncate - elide at left, right, middle, or ends? (default: right)
marker - how to mark the elision (default: ...)
at_space - if true, strings will be broken at whitespace if possible
=head2 trunc
trunc($string, $length, \%arg)
This acts just like C<elide>, but assumes an empty marker, so it actually
truncates the string normally.
=head1 IMPORTING
String::Truncate exports both C<elide> and C<trunc>, and also supports the
Exporter-style ":all" tag.
use String::Truncate (); # export nothing
use String::Truncate qw(elide); # export just elide()
use String::Truncate qw(:all); # export both elide() and trunc()
use String::Truncate qw(-all); # export both elide() and trunc()
When exporting, you may also supply default values:
use String::Truncate -all => defaults => { length => 10, marker => '--' };
# or
use String::Truncate -all => { length => 10, marker => '--' };
These values affect only the imported version of the functions. You may pass
arguments as usual to override them, and you may call the subroutine by its
fully-qualified name to get the standard behavior.
=head1 BUILDING CODEREFS
The imported builds and installs lexical closures (code references) that merge
in given values to the defaults. You can build your own closures without
importing them into your namespace. To do this, use the C<elide_with_defaults>
and C<trunc_with_defaults> routines.
=head2 elide_with_defaults
my $elider = String::Truncate::elide_with_defaults(\%arg);
This routine, never exported, builds a coderef which behaves like C<elide>, but
uses default values when needed. All the valid arguments to C<elide> are valid
here, as well as C<length>.
=head2 trunc_with_defaults
This routine behaves exactly like elide_with_defaults, with one obvious
exception: it returns code that works like C<trunc> rather than C<elide>. If a
C<marker> argument is passed, it is ignored.
=head1 SEE ALSO
L<Text::Truncate> does a very similar thing. So does L<Text::Elide>.
=head1 BUGS
Please report any bugs or feature requests through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Truncate>. I will be
notified, and then you'll automatically be notified of progress on your bug as
I make changes.
=head1 ACKNOWLEDGEMENTS
Ian Langworth gave me some good advice about naming things. (Also some bad
jokes. Nobody wants String::ETOOLONG, Ian.) Hans Dieter Pearcey suggested
allowing defaults just in time for a long bus ride, and I was rescued from
boredom by that suggestion
=head1 AUTHOR
Ricardo Signes <cpan@semiotic.systems>
=head1 CONTRIBUTORS
=for stopwords David Steinbrunner Ricardo SIGNES Signes
=over 4
=item *
David Steinbrunner <dsteinbrunner@pobox.com>
=item *
Ricardo SIGNES <rjbs@codesimply.com>
=item *
Ricardo Signes <rjbs@semiotic.systems>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2022 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|