String::Truncate(3)

πŸ’– String::Truncate πŸ’–

okay so this is the String::Truncate(3) manual page from User Contributed Perl Documentation, and it's literally here to save you when your strings are too long for the terminal 😭✨ bestie, we've all been there.

✨ NAME ✨

String::Truncate β€” a module for when strings are too long to be displayed in... 🫠

🌈 VERSION 🌈

version 1.100603 β€” iconic, no notes πŸ’…

πŸŽ€ 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, fr fr:

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"

πŸͺ 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.

heads up, bestie: that's a real "no promises" situation β€” the minimum perl version can get bumped whenever, and patches to lower it are not guaranteed to be accepted πŸ’€

πŸ› οΈ FUNCTIONS πŸ› οΈ

βœ‚οΈ elide βœ‚οΈ

elide($string, $length, \%arg)

This function returns the string, if it is less than or equal to $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

βœ‚οΈ trunc βœ‚οΈ

trunc($string, $length, \%arg)

This acts just like elide, but assumes an empty marker, so it actually truncates the string normally. clean, simple, slay πŸŽ€

πŸ“¦ IMPORTING πŸ“¦

String::Truncate exports both elide and 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. πŸ’–

🧚 BUILDING CODEREFS 🧚

When imported, they build and install lexical closures (code references) that merge given values into the defaults. You can build your own closures without importing them into your namespace. To do this, use the elide_with_defaults and trunc_with_defaults routines. πŸ¦‹

πŸͺ„ elide_with_defaults πŸͺ„

my $elider = String::Truncate::elide_with_defaults(\%arg);

This routine, never exported, builds a coderef which behaves like elide, but uses default values when needed. All the valid arguments to elide are valid here, as well as length. ✨

πŸͺ„ trunc_with_defaults πŸͺ„

This routine behaves exactly like elide_with_defaults, with one obvious exception: it returns code that works like trunc rather than elide. If a marker argument is passed, it is ignored. πŸ™ˆ

πŸ‘€ SEE ALSO πŸ‘€

Text::Truncate does a very similar thing. So does Text::Elide. cute little cousins, honestly πŸ’…

🐞 BUGS 🐞

Please report any bugs or feature requests through the web interface at 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. 🫢

🫢 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. πŸ˜­πŸ’–

πŸ‘‘ AUTHOR πŸ‘‘

Ricardo Signes β€” <cpan@semiotic.systems> πŸ‘‘

🌟 CONTRIBUTORS 🌟

  • David Steinbrunner <dsteinbrunner@pobox.com>
  • Ricardo SIGNES <rjbs@codesimply.com>
  • Ricardo Signes <rjbs@semiotic.systems>

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. 🌈


okay that's the whole String::Truncate(3) page, built from perl v5.42.0, dated 2023-01-02 β€” mwah πŸ’‹βœ¨ no cap, go truncate something cute.

πŸ“¦ where this page comes from