File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -43,4 +43,9 @@ public static function stripAnsiEscapeSequence(string $str) : string
43
43
{
44
44
return (string ) preg_replace ('/\x1b[^m]*m/ ' , '' , $ str );
45
45
}
46
+
47
+ public static function length (string $ str , bool $ ignoreAnsiEscapeSequence = true ) : string
48
+ {
49
+ return mb_strlen ($ ignoreAnsiEscapeSequence ? self ::stripAnsiEscapeSequence ($ str ) : $ str );
50
+ }
46
51
}
Original file line number Diff line number Diff line change @@ -91,4 +91,28 @@ public function testSplitItemBug() : void
91
91
StringUtil::wordwrap ($ test , 11 )
92
92
);
93
93
}
94
+
95
+ public function testLengthIgnoresAnsiEscapeSequences () : void
96
+ {
97
+ $ result = StringUtil::length ("\x1b[7mfoo \x1b[0m " );
98
+ $ this ->assertEquals (3 , $ result );
99
+
100
+ $ result = StringUtil::length ("foobar \x1b[00m \x1b[01;31m " );
101
+ $ this ->assertEquals (6 , $ result );
102
+
103
+ $ result = StringUtil::length ("foo \x1b[00mbar \x1b[01;31mbaz \x1b[00m!!! \x1b[01;31m " );
104
+ $ this ->assertEquals (12 , $ result );
105
+ }
106
+
107
+ public function testLengthIncludingAnsiEscapeSequences () : void
108
+ {
109
+ $ result = StringUtil::length ("\x1b[7mfoo \x1b[0m " , false );
110
+ $ this ->assertEquals (11 , $ result );
111
+
112
+ $ result = StringUtil::length ("foobar \x1b[00m \x1b[01;31m " , false );
113
+ $ this ->assertEquals (19 , $ result );
114
+
115
+ $ result = StringUtil::length ("foo \x1b[00mbar \x1b[01;31mbaz \x1b[00m!!! \x1b[01;31m " , false );
116
+ $ this ->assertEquals (38 , $ result );
117
+ }
94
118
}
You can’t perform that action at this time.
0 commit comments