Skip to content

Commit 375d811

Browse files
committed
Add the alignment functionality
1 parent e3cbc1d commit 375d811

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

lib/cli/Table.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use cli\Shell;
1616
use cli\Streams;
1717
use cli\table\Ascii;
18+
use cli\table\Column;
1819
use cli\table\Renderer;
1920
use cli\table\Tabular;
2021

@@ -27,6 +28,7 @@ class Table {
2728
protected $_footers = array();
2829
protected $_width = array();
2930
protected $_rows = array();
31+
protected $_alignments = array();
3032

3133
/**
3234
* Initializes the `Table` class.
@@ -44,7 +46,7 @@ class Table {
4446
* @param array $rows The rows of data for this table. Optional.
4547
* @param array $footers Footers used in this table. Optional.
4648
*/
47-
public function __construct(array $headers = null, array $rows = null, array $footers = null) {
49+
public function __construct(array $headers = null, array $rows = null, array $footers = null, $alignments = []) {
4850
if (!empty($headers)) {
4951
// If all the rows is given in $headers we use the keys from the
5052
// first row for the header values
@@ -62,6 +64,8 @@ public function __construct(array $headers = null, array $rows = null, array $fo
6264
$this->setRows($rows);
6365
}
6466

67+
$this->setAlignments($alignments);
68+
6569
if (!empty($footers)) {
6670
$this->setFooters($footers);
6771
}
@@ -79,6 +83,7 @@ public function resetTable()
7983
$this->_width = array();
8084
$this->_rows = array();
8185
$this->_footers = array();
86+
$this->_alignments = array();
8287
return $this;
8388
}
8489

@@ -137,6 +142,7 @@ public function display() {
137142
*/
138143
public function getDisplayLines() {
139144
$this->_renderer->setWidths($this->_width, $fallback = true);
145+
$this->_renderer->setAlignments($this->_alignments);
140146
$border = $this->_renderer->border();
141147

142148
$out = array();
@@ -201,6 +207,15 @@ public function setFooters(array $footers) {
201207
$this->_footers = $this->checkRow($footers);
202208
}
203209

210+
/**
211+
* Set the alignments of the table.
212+
*
213+
* @param array $alignments An array of strings containing column alignments.
214+
*/
215+
public function setAlignments(array $alignments) {
216+
$this->_alignments = $alignments;
217+
//TODO: Implement error if alignment is not valid and field is not valid
218+
}
204219

205220
/**
206221
* Add a row to the table.

lib/cli/table/Ascii.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Ascii extends Renderer {
2828
protected $_border = null;
2929
protected $_constraintWidth = null;
3030
protected $_pre_colorized = false;
31+
protected $_headers = null;
3132

3233
/**
3334
* Set the widths of each column in the table.
@@ -131,6 +132,10 @@ public function border() {
131132
*/
132133
public function row( array $row ) {
133134

135+
if($this->_headers === null) {
136+
$this->_headers = array_values($row);
137+
}
138+
134139
$extra_row_count = 0;
135140

136141
if ( count( $row ) > 0 ) {
@@ -197,7 +202,10 @@ public function row( array $row ) {
197202
}
198203

199204
private function padColumn($content, $column) {
200-
return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ) ) . $this->_characters['padding'];
205+
$column_name = $this->_headers[$column];
206+
$alignment = array_key_exists($column_name, $this->_alignments) ? $this->_alignments[$column_name] : Column::ALIGN_LEFT;
207+
208+
return $this->_characters['padding'] . Colors::pad( $content, $this->_widths[ $column ], $this->isPreColorized( $column ), false, $alignment) . $this->_characters['padding'];
201209
}
202210

203211
/**

lib/cli/table/Renderer.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@
1717
*/
1818
abstract class Renderer {
1919
protected $_widths = array();
20+
protected $_alignments = array();
2021

21-
public function __construct(array $widths = array()) {
22+
public function __construct(array $widths = array(), array $alignments = array()) {
2223
$this->setWidths($widths);
24+
$this->setAlignments($alignments);
25+
}
26+
27+
/**
28+
* Set the alignments of each column in the table.
29+
*
30+
* @param array $alignments The alignments of the columns.
31+
*/
32+
public function setAlignments(array $alignments) {
33+
$this->_alignments = $alignments;
2334
}
2435

2536
/**

0 commit comments

Comments
 (0)