/** * @param InputInterface $input * @param OutputInterface $output * * @return string */ protected function execute(InputInterface $input, OutputInterface $output) { $table = new TextTable(['columnWidths' => [25, 10, 20, 36], 'decorator' => 'ascii', 'AutoSeparate' => TextTable::AUTO_SEPARATE_HEADER, 'padding' => 1]); $table->appendRow(['store_name', 'store_id', 'springbot_store_id', 'springbot_guid']); foreach ($this->storeManager->getStores() as $store) { $springbotStoreId = $this->storeConfig->getSpringbotStoreId($store->getId()); $springbotGuid = strtolower($this->storeConfig->getGuid($store->getId())); $table->appendRow([substr($store->getName(), 0, 23), $store->getId(), $springbotStoreId, $springbotGuid]); } $output->writeln($table->render()); }
/** * @param InputInterface $input * @param OutputInterface $output * * @return string */ protected function execute(InputInterface $input, OutputInterface $output) { $headers = ['queue', 'class', 'method', 'args', 'priority', 'attempts', 'error']; $this->_jobCollection->setCurPage($input->getArgument(self::PAGE_ARGUMENT)); $this->_jobCollection->setPageSize($input->getArgument(self::PER_PAGE_ARGUMENT)); $this->_jobCollection->addOrder('priority', JobCollection::SORT_ORDER_ASC); $this->_jobCollection->addOrder('next_run_at', JobCollection::SORT_ORDER_ASC); if ($queue = $input->getArgument(self::QUEUE_ARGUMENT)) { $this->_jobCollection->addFieldToFilter('queue', $queue); } $jobs = $this->_jobCollection->toArray(); // Pre-calculate the max column widths $maxWidths = []; foreach ($headers as $header) { $maxWidths[$header] = strlen($header) + 2; } foreach ($jobs['items'] as $job) { foreach ($headers as $header) { $newLength = strlen($job[$header]) + 2; if ($newLength > $maxWidths[$header]) { $maxWidths[$header] = $newLength; } } } $maxWidths = array_values($maxWidths); $table = new TextTable(['columnWidths' => $maxWidths, 'decorator' => 'ascii', 'AutoSeparate' => TextTable::AUTO_SEPARATE_HEADER, 'padding' => 1]); $table->appendRow($headers); foreach ($jobs['items'] as $job) { $rowArr = []; foreach ($headers as $header) { $rowArr[$header] = $job[$header]; } $table->appendRow($rowArr); } $output->writeln($table->render()); }
/** * Render a text table containing the data provided, that will fit inside console window's width. * * @param $data * @param $cols * @param $consoleWidth * @return string */ protected function renderTable($data, $cols, $consoleWidth) { $result = ''; $padding = 2; // If there is only 1 column, just concatenate it if ($cols == 1) { foreach ($data as $row) { $result .= $row[0] . "\n"; } return $result; } // Get the string wrapper supporting UTF-8 character encoding $strWrapper = StringUtils::getWrapper('UTF-8'); // Determine max width for each column $maxW = array(); for ($x = 1; $x <= $cols; $x += 1) { $maxW[$x] = 0; foreach ($data as $row) { $maxW[$x] = max($maxW[$x], $strWrapper->strlen($row[$x - 1]) + $padding * 2); } } /* * Check if the sum of x-1 columns fit inside console window width - 10 * chars. If columns do not fit inside console window, then we'll just * concatenate them and output as is. */ $width = 0; for ($x = 1; $x < $cols; $x += 1) { $width += $maxW[$x]; } if ($width >= $consoleWidth - 10) { foreach ($data as $row) { $result .= implode(" ", $row) . "\n"; } return $result; } /* * Use Zend\Text\Table to render the table. * The last column will use the remaining space in console window * (minus 1 character to prevent double wrapping at the edge of the * screen). */ $maxW[$cols] = $consoleWidth - $width - 1; $table = new Table\Table(); $table->setColumnWidths($maxW); $table->setDecorator(new Table\Decorator\Blank()); $table->setPadding(2); foreach ($data as $row) { $table->appendRow($row); } return $table->render(); }
public function testTableComplex() { $table = new Table\Table(array('columnWidths' => array(10, 10, 10))); $row = new Table\Row(); $row->appendColumn(new Table\Column('foobar')); $row->appendColumn(new Table\Column('foobar', null, 2)); $table->appendRow($row); $row = new Table\Row(); $row->appendColumn(new Table\Column('foobar')); $row->appendColumn(new Table\Column('foobar', null, 2)); $table->appendRow($row); $row = new Table\Row(); $row->appendColumn(new Table\Column('foobar', null, 3)); $table->appendRow($row); $row = new Table\Row(); $row->appendColumn(new Table\Column('foobar')); $row->appendColumn(new Table\Column('foobar')); $row->appendColumn(new Table\Column('foobar')); $table->appendRow($row); $this->assertEquals($table->render(), "┌──────────┬─────────────────────┐\n" . "│foobar │foobar │\n" . "├──────────┼─────────────────────┤\n" . "│foobar │foobar │\n" . "├──────────┴─────────────────────┤\n" . "│foobar │\n" . "├──────────┬──────────┬──────────┤\n" . "│foobar │foobar │foobar │\n" . "└──────────┴──────────┴──────────┘\n"); }
private function parseTable(\DOMElement $node, $context) { /*{{{*/ $trs = $this->xpath->query('txt:tr', $node); $num_cols = 0; $rows = array(); foreach ($trs as $tr) { $row = new Zend_Row(); $tds = $this->xpath->query('txt:td', $tr); $len = $tds->length; if ($len > $num_cols) { $num_cols = $len; } foreach ($tds as $td) { $td_text = ""; foreach ($td->childNodes as $child) { $td_text .= $this->parseNode($child, $context); } $column = new Zend_Column($td_text); if ($td->hasAttribute('colspan')) { $column->setColSpan((int) $td->getAttribute('colspan')); } $row->appendColumn($column); } $rows[] = $row; } if ($num_cols) { $col_width = (int) floor($this->wordwrap / $num_cols); $table = new Zend_Table(array('columnWidths' => array_fill(0, $num_cols, $col_width), 'decorator' => 'ascii', 'padding' => 1)); foreach ($rows as $row) { $table->appendRow($row); } return $table->render(); } }
/** * Convert the table tag * * @param DOMElement $node * @return string */ public static function table($node) { // check if thead exists if (0 !== $node[0]->getElementsByTagName('thead')->length) { $head = true; } else { $head = false; } $rows = $node[0]->getElementsByTagName('row'); $table = array(); $totRow = $rows->length; $j = 0; foreach ($rows as $row) { $cols = $row->getElementsByTagName('entry'); $totCol = $cols->length; if (!isset($widthCol)) { $widthCol = array_fill(0, $totCol, 0); } $i = 0; foreach ($cols as $col) { $table[$j][$i] = self::formatText($col->nodeValue); $length = strlen($table[$j][$i]); if ($length > $widthCol[$i]) { $widthCol[$i] = $length; } $i++; } $j++; } $tableText = new Table\Table(array('columnWidths' => $widthCol, 'decorator' => 'ascii')); for ($j = 0; $j < $totRow; $j++) { $row = new Table\Row(); for ($i = 0; $i < $totCol; $i++) { $row->appendColumn(new Table\Column($table[$j][$i])); } $tableText->appendRow($row); } $output = $tableText->render(); // if thead exists change the table style with head (= instead of -) if ($head) { $table = explode("\n", $output); $newOutput = ''; $i = 0; foreach ($table as $row) { if ('+-' === substr($row, 0, 2)) { $i++; } if (2 === $i) { $row = str_replace('-', '=', $row); } $newOutput .= "{$row}\n"; } return $newOutput; } return $output; }