1616 * The `Table` class is used to display data in a tabular format.
1717 */
1818class Table {
19+ protected $ _renderer ;
1920 protected $ _headers = array ();
2021 protected $ _width = array ();
2122 protected $ _rows = array ();
2223
23- /**
24- * Checks whether the output of the current script is a TTY or a pipe / redirect
25- *
26- * Returns true if STDOUT output is being redirected to a pipe or a file; false is
27- * output is being sent directly to the terminal.
28- *
29- * @return bool
30- */
31- protected function isPiped () {
32- return ( function_exists ( 'posix_isatty ' ) && !posix_isatty ( STDOUT ) );
33- }
34-
3524 /**
3625 * Initializes the `Table` class.
3726 *
@@ -64,6 +53,24 @@ public function __construct(array $headers = null, array $rows = null) {
6453 $ this ->setHeaders ($ headers );
6554 $ this ->setRows ($ rows );
6655 }
56+
57+ if (\cli \Shell::isPiped ()) {
58+ $ this ->setRenderer (new \cli \table \Tabular ());
59+ } else {
60+ $ this ->setRenderer (new \cli \table \Ascii ());
61+ }
62+ }
63+
64+ /**
65+ * Sets the renderer used by this table.
66+ *
67+ * @param cli\table\Renderer $renderer The renderer to use for output.
68+ * @see cli\table\Renderer
69+ * @see cli\table\Standard
70+ * @see cli\table\Tabular
71+ */
72+ public function setRenderer (\cli \table \Renderer $ renderer ) {
73+ $ this ->_renderer = $ renderer ;
6774 }
6875
6976 /**
@@ -88,57 +95,29 @@ protected function checkRow(array $row) {
8895 * If STDOUT is a pipe or redirected to a file, should output simple
8996 * tab-separated text. Otherwise, renders table with ASCII table borders
9097 *
91- * @uses cli\Table ::isPiped() Determine what format to output
98+ * @uses cli\Shell ::isPiped() Determine what format to output
9299 *
93100 * @see cli\Table::renderRow()
94101 */
95102 public function display () {
96- $ borderStr = '+ ' ;
97- foreach ($ this ->_headers as $ column => $ header ) {
98- $ borderStr .= '- ' . str_repeat ('- ' , $ this ->_width [$ column ]) . '-+ ' ;
99- }
103+ $ this ->_renderer ->setWidths ($ this ->_width );
104+ $ border = $ this ->_renderer ->border ();
100105
101- if ( $ this -> isPiped () ) {
102- \cli \line ($ this -> renderPipedRow ( $ this -> _headers ) );
103- foreach ( $ this -> _rows as $ row ) {
104- \cli \line ($ this ->renderPipedRow ( $ row ));
105- }
106- return ;
106+ if (isset ( $ border ) ) {
107+ \cli \line ($ border );
108+ }
109+ \cli \line ($ this ->_renderer -> row ( $ this -> _headers ));
110+ if ( isset ( $ border )) {
111+ \ cli \line ( $ border ) ;
107112 }
108-
109- \cli \line ($ borderStr );
110- \cli \line ($ this ->renderRow ($ this ->_headers ));
111- \cli \line ($ borderStr );
112113
113114 foreach ($ this ->_rows as $ row ) {
114- \cli \line ($ this ->renderRow ($ row ));
115+ \cli \line ($ this ->_renderer -> row ($ row ));
115116 }
116117
117- \cli \line ($ borderStr );
118- }
119-
120- /**
121- * Renders a row for output.
122- *
123- * @param array $row The table row.
124- * @return string The formatted table row.
125- */
126- protected function renderRow (array $ row ) {
127- $ render = '| ' ;
128- foreach ($ row as $ column => $ val ) {
129- $ render .= ' ' . Colors::pad ($ val , $ this ->_width [$ column ]) . ' | ' ;
118+ if (isset ($ border )) {
119+ \cli \line ($ border );
130120 }
131- return $ render ;
132- }
133-
134- /**
135- * Renders a row for piped output.
136- *
137- * @param array $row The table row.
138- * @return string The formatted table row.
139- */
140- protected function renderPipedRow (array $ row ) {
141- return implode ( "\t" , array_values ( $ row ) );
142121 }
143122
144123 /**
0 commit comments