Skip to content

Commit 2e5ec11

Browse files
Ability to set constraint width on renderer
1 parent 08f1edb commit 2e5ec11

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

lib/cli/table/Ascii.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@ class Ascii extends Renderer {
2525
'padding' => ' ',
2626
);
2727
protected $_border = null;
28+
protected $_constraintWidth = null;
2829

2930
/**
3031
* Set the widths of each column in the table.
3132
*
3233
* @param array $widths The widths of the columns.
3334
*/
3435
public function setWidths(array $widths) {
35-
static $terminal_width;
3636

37-
if ( ! isset( $terminal_width ) ) {
38-
$terminal_width = (int) shell_exec( 'tput cols' );
37+
if ( is_null( $this->_constraintWidth ) ) {
38+
$this->_constraintWidth = (int) shell_exec( 'tput cols' );
3939
}
4040
$col_count = count( $widths );
4141
$col_borders_count = $col_count * strlen( $this->_characters['border'] );
4242
$table_borders_count = strlen( $this->_characters['border'] ) * 1;
4343
$col_padding_count = $col_count * strlen( $this->_characters['padding'] ) * 2;
44-
$max_width = $terminal_width - $col_borders_count - $table_borders_count - $col_padding_count;
44+
$max_width = $this->_constraintWidth - $col_borders_count - $table_borders_count - $col_padding_count;
4545

4646
if ( $widths && $max_width && array_sum( $widths ) > $max_width ) {
4747

@@ -76,6 +76,15 @@ public function setWidths(array $widths) {
7676
$this->_widths = $widths;
7777
}
7878

79+
/**
80+
* Set the contraint width for the table
81+
*
82+
* @param int $constraintWidth
83+
*/
84+
public function setConstraintWidth( $constraintWidth ) {
85+
$this->_constraintWidth = $constraintWidth;
86+
}
87+
7988
/**
8089
* Set the characters used for rendering the Ascii table.
8190
*

0 commit comments

Comments
 (0)