Skip to content

CLI::strlen() only strips the library ANSI codes, so tables misalign #45

Description

@HafizMMoaz

CLI::strlen() removes styles by comparing against the exact code strings from the three style enums (src/CLI.php:171-186). Any other escape sequence is counted as visible characters, so table() and box() pad the wrong amount:

CLI::table([
    ["\033[38;5;208morange\033[0m", 'b'],
    ['plain', 'b'],
], ['col1', 'col2']);
+-------------------+------+
| col1              | col2 |
+-------------------+------+
| orange | b    |            <- 11 invisible bytes counted as width
| plain             | b    |
+-------------------+------+

Verified on PHP 8.4.19 against main (2c0159f). 256 color and true color sequences, hyperlinks (\e]8;;) and cursor moves all hit this, and users producing them is expected since the library exposes raw output helpers.

Suggested fix: strip with a regex over the CSI/OSC grammar, for example preg_replace('/\e\[[0-9;]*[A-Za-z]/', '', $text) extended for OSC, instead of a fixed list.

Secondary point in the same method: the code list is rebuilt by iterating every enum case on every call, and table() calls it once per cell, so a 20x5 table iterates the enums 100 times. The list is constant and can be cached in a static.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions