29 Apr 2021 21:31:00 UTC
- Distribution: String-TtyLength
- Module version: 0.03
- Source (raw)
- Browse (raw)
- How to Contribute
- Repository (git clone)
- Testers (167 / 0 / 0)
- KwaliteeBus factor: 1
- 100.00% Coverage
- License: perl_5
- Perl: v5.6.0
- Activity24 month
- Download (10.71KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest versionNEILB Neil Bowers
- SEE ALSO
- COPYRIGHT AND LICENSE
String::TtyLength - length or width of string excluding ANSI tty codes
use String::TtyLength 0.02 qw/ tty_length tty_width /; $length = tty_length("\e[1mbold text\e[0m"); print "length = $length\n"; # 9 $width = tty_width("😄"); print "width = $width\n"; # 2
This module provides two functions which tell you the length and width of a string as it will appear on a terminal (tty), excluding any ANSI escape codes.
tty_lengthreturns the length of a string excluding any ANSI tty / terminal escape codes.
tty_widthreturns the number of columns on a terminal that the string will take up, also excluding any escape codes.
For non-wide characters, the functions will return the same value. But consider the following:
my $emoji = "😄"; my $length = tty_length($emoji); # 1 my $width = tty_width($emoji); # 2
If you're trying to align text in columns, then you'll probably want
tty_width; if you just want to know the number of characters, using
Takes a single string, and returns the length of the string, excluding any escape sequences.
Note: the escape sequences could include cursor movement, so the length returned by this function might not be the number of characters that would be visible on screen. But
length_of_string_excluding_escape_sequences()was just too long.
Takes a single string and returns the number of columns that the string will take up on a terminal.
You may find that sometimes
tty_width()returns the wrong number. If you do, please submit a bug, or email me at the address below.
This was added in version 0.02 of
String::TtyLength, so you should require at least that version of this module, as per the SYNOPSIS.
colorstrip()in Term::ANSIColor will remove ANSI color escape sequences from a string, but not all ANSI escape sequences.
Neil Bowers <firstname.lastname@example.org>
This software is copyright (c) 2020 by Neil Bowers.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Module Install Instructions
To install String::TtyLength, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install String::TtyLength
For more information on module installation, please visit the detailed CPAN module installation guide.