phplucidframe / console-table

ConsoleTable helps you to display tabular data in a terminal/shell/console
Other
99 stars 22 forks source link

Issue with non-autoresizing headers #14

Open hron84 opened 3 years ago

hron84 commented 3 years ago

Actual behavior

+----+------+-----+--------+------------+---+
| ID | Name | Key | Status | Page count |
+----+------+-----+--------+------------+---+
| 12345689  | Kategória szövege sok-sok betű | CK         | 12365478  | global | CURRENT 
+----+------+-----+--------+------------+---+

As you see, the header fixes to the size of the header string and does not autosize to the content. It contains utf8 characters but I had an issue with strings without multibyte characters, but even if it would handle multibyte strings badly the name column should wider than 5 chars.

Code:

  $ctable = new LucidFrame\Console\ConsoleTable();
  $ctable = $ctable->setHeaders(['ID', 'Name', 'Key', 'Status', 'Page count']);
  foreach($spaces as $row) {
    $ctable->addRow($row);
  }

  $ctable->display();

phplucidframe/console-table dev-master a973d91 Console Table

Faq commented 3 years ago

Yeah, had to use for each column something like this to limit length of it:

                ->addColumn(mb_strimwidth($data[1], 0, 39, "..."))
                ->addColumn($data[2])
                ->addColumn(mb_strimwidth($data[3], 0, 30, "..."))
                ->addColumn(mb_strimwidth($data[4], 0, 22, "..."))
hron84 commented 3 years ago

Thanks @Faq I will check it soon. Is there any way to specify the width of the column?

Faq commented 3 years ago

As I know, no.

cithukyaw commented 2 years ago

@hron84 Do you persist in seeing the issue? I can see the correct auto-resize when I tried to reproduce it.

console-table – test php 2021-11-13 15 49 24

Vitexus commented 10 months ago

@hron84 here:

obrazek

zoltanlaca commented 4 months ago

You must use addHeader() and addColumn() like this:

$table = new \LucidFrame\Console\ConsoleTable();
$header = ['ID', 'Name', 'Key', 'Status', 'Page count', '-'];
$rows = [
    [12345689, 'Kategória szövege sok-sok betű', 'CK', 12365478, 'global', 'CURRENT']
];

foreach ($header as $column) {
    if (reset($header) === $column) {
        $table->addHeader($column);
    } else {
        $table->addColumn($column);
    }
}

foreach ($rows as $row) {
    $table->addRow();
    foreach ($row as $column) {
        $table->addColumn($column);
    }
}
$table->display();

and the result will look like this:

+----------+--------------------------------+-----+----------+------------+---------+
| ID       | Name                           | Key | Status   | Page count | -       |
+----------+--------------------------------+-----+----------+------------+---------+
| 12345689 | Kategória szövege sok-sok betű | CK  | 12365478 | global     | CURRENT |
+----------+--------------------------------+-----+----------+------------+---------+