-
Notifications
You must be signed in to change notification settings - Fork 117
Add alignment functionality to tables #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e3cbc1d
375d811
3c67503
a9b10b7
c0071e8
9c1524b
0fc32ba
a3d6ddb
95d08e8
be3ff09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ class Ascii extends Renderer { | |
| protected $_border = null; | ||
| protected $_constraintWidth = null; | ||
| protected $_pre_colorized = false; | ||
| protected $_headers = null; | ||
|
|
||
| /** | ||
| * Set the widths of each column in the table. | ||
|
|
@@ -131,6 +132,10 @@ public function border() { | |
| */ | ||
| public function row( array $row ) { | ||
|
|
||
| if ($this->_headers === null) { | ||
| $this->_headers = array_values($row); | ||
| } | ||
|
Comment on lines
+135
to
+137
|
||
|
|
||
| $extra_row_count = 0; | ||
|
|
||
| if ( count( $row ) > 0 ) { | ||
|
|
@@ -198,8 +203,10 @@ public function row( array $row ) { | |
| } | ||
|
|
||
| private function padColumn($content, $column) { | ||
| $column_name = isset( $this->_headers[$column] ) ? $this->_headers[$column] : ''; | ||
| $alignment = ($column_name !== '' && array_key_exists($column_name, $this->_alignments)) ? $this->_alignments[$column_name] : Column::ALIGN_LEFT; | ||
| $content = str_replace( "\t", ' ', (string) $content ); | ||
| return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ) ) . $this->_characters['padding']; | ||
| return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ), false, $alignment) . $this->_characters['padding']; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| namespace cli\table; | ||
|
|
||
| interface Column { | ||
|
|
||
| const ALIGN_RIGHT = STR_PAD_LEFT; | ||
| const ALIGN_LEFT = STR_PAD_RIGHT; | ||
| const ALIGN_CENTER = STR_PAD_BOTH; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation on line 222 checks if headers exist before alignments are set, but
setAlignments()can be called in the constructor at line 68 beforesetHeaders()is called at line 64 when the constructor is invoked with an empty headers array and a non-empty alignments array. This will cause the validation to always fail in that case because$this->_headerswill be empty. Consider either validating alignments lazily when they're used, or reordering the constructor calls, or skipping validation when headers are empty.