1String::TtyLength(3) User Contributed Perl Documentation String::TtyLength(3)
2
3
4
6 String::TtyLength - length or width of string excluding ANSI tty codes
7
9 use String::TtyLength 0.02 qw/ tty_length tty_width /;
10 $length = tty_length("\e[1mbold text\e[0m");
11 print "length = $length\n";
12 # 9
13
14 $width = tty_width("😄");
15 print "width = $width\n";
16 # 2
17
19 This module provides two functions which tell you the length and width
20 of a string as it will appear on a terminal (tty), excluding any ANSI
21 escape codes.
22
23 "tty_length" returns the length of a string excluding any ANSI tty /
24 terminal escape codes.
25
26 "tty_width" returns the number of columns on a terminal that the string
27 will take up, also excluding any escape codes.
28
29 For non-wide characters, the functions will return the same value. But
30 consider the following:
31
32 my $emoji = "😄";
33 my $length = tty_length($emoji); # 1
34 my $width = tty_width($emoji); # 2
35
36 If you're trying to align text in columns, then you'll probably want
37 "tty_width"; if you just want to know the number of characters, using
38 "tty_length".
39
40 tty_length( STRING )
41 Takes a single string, and returns the length of the string, excluding
42 any escape sequences.
43
44 Note: the escape sequences could include cursor movement, so the length
45 returned by this function might not be the number of characters that
46 would be visible on screen. But
47 length_of_string_excluding_escape_sequences() was just too long.
48
49 tty_width( STRING )
50 Takes a single string and returns the number of columns that the string
51 will take up on a terminal.
52
53 You may find that sometimes tty_width() returns the wrong number. If
54 you do, please submit a bug, or email me at the address below.
55
56 This was added in version 0.02 of "String::TtyLength", so you should
57 require at least that version of this module, as per the SYNOPSIS.
58
60 colorstrip() in Term::ANSIColor will remove ANSI color escape sequences
61 from a string, but not all ANSI escape sequences.
62
64 <https://github.com/neilb/String-TtyLength>
65
67 Neil Bowers <neilb@cpan.org>
68
70 This software is copyright (c) 2020 by Neil Bowers.
71
72 This is free software; you can redistribute it and/or modify it under
73 the same terms as the Perl 5 programming language system itself.
74
75
76
77perl v5.38.0 2023-07-21 String::TtyLength(3)