-
Notifications
You must be signed in to change notification settings - Fork 115
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
Changes from 2 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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| use cli\Shell; | ||
| use cli\Streams; | ||
| use cli\table\Ascii; | ||
| use cli\table\Column; | ||
| use cli\table\Renderer; | ||
| use cli\table\Tabular; | ||
|
|
||
|
|
@@ -27,6 +28,7 @@ class Table { | |
| protected $_footers = array(); | ||
| protected $_width = array(); | ||
| protected $_rows = array(); | ||
| protected $_alignments = array(); | ||
|
|
||
| /** | ||
| * Initializes the `Table` class. | ||
|
|
@@ -44,7 +46,7 @@ class Table { | |
| * @param array $rows The rows of data for this table. Optional. | ||
| * @param array $footers Footers used in this table. Optional. | ||
| */ | ||
| public function __construct(array $headers = null, array $rows = null, array $footers = null) { | ||
| public function __construct(array $headers = null, array $rows = null, array $footers = null, $alignments = []) { | ||
| if (!empty($headers)) { | ||
| // If all the rows is given in $headers we use the keys from the | ||
| // first row for the header values | ||
|
|
@@ -62,6 +64,8 @@ public function __construct(array $headers = null, array $rows = null, array $fo | |
| $this->setRows($rows); | ||
| } | ||
|
|
||
| $this->setAlignments($alignments); | ||
|
|
||
| if (!empty($footers)) { | ||
| $this->setFooters($footers); | ||
| } | ||
|
|
@@ -79,6 +83,7 @@ public function resetTable() | |
| $this->_width = array(); | ||
| $this->_rows = array(); | ||
| $this->_footers = array(); | ||
| $this->_alignments = array(); | ||
| return $this; | ||
| } | ||
|
|
||
|
|
@@ -137,6 +142,7 @@ public function display() { | |
| */ | ||
| public function getDisplayLines() { | ||
| $this->_renderer->setWidths($this->_width, $fallback = true); | ||
| $this->_renderer->setAlignments($this->_alignments); | ||
| $border = $this->_renderer->border(); | ||
|
|
||
| $out = array(); | ||
|
|
@@ -201,6 +207,15 @@ public function setFooters(array $footers) { | |
| $this->_footers = $this->checkRow($footers); | ||
| } | ||
|
|
||
| /** | ||
| * Set the alignments of the table. | ||
| * | ||
| * @param array $alignments An array of strings containing column alignments. | ||
| */ | ||
| public function setAlignments(array $alignments) { | ||
| $this->_alignments = $alignments; | ||
| //TODO: Implement error if alignment is not valid and field is not valid | ||
| } | ||
|
Comment on lines
+216
to
+227
|
||
|
|
||
| /** | ||
| * Add a row to the table. | ||
|
|
||
| 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) { | ||
|
swissspidy marked this conversation as resolved.
Outdated
|
||
| $this->_headers = array_values($row); | ||
| } | ||
|
Comment on lines
+135
to
+137
|
||
|
|
||
| $extra_row_count = 0; | ||
|
|
||
| if ( count( $row ) > 0 ) { | ||
|
|
@@ -197,7 +202,10 @@ public function row( array $row ) { | |
| } | ||
|
|
||
| private function padColumn($content, $column) { | ||
| return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ) ) . $this->_characters['padding']; | ||
| $column_name = $this->_headers[$column]; | ||
|
swissspidy marked this conversation as resolved.
Outdated
|
||
| $alignment = array_key_exists($column_name, $this->_alignments) ? $this->_alignments[$column_name] : Column::ALIGN_LEFT; | ||
|
swissspidy marked this conversation as resolved.
Outdated
|
||
|
|
||
| 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; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.