public static getLength ( $string, Webmozart\Console\Api\Formatter\Formatter $formatter = null ) | ||
$formatter | Webmozart\Console\Api\Formatter\Formatter |
/** * Draws a bordered row of cells. * * @param IO $io The I/O. * @param BorderStyle $style The border style. * @param string[] $row The row cells. * @param int[] $columnLengths The lengths of the cells. * @param int[] $alignments The alignments of the cells. * @param string $cellFormat The cell format. * @param Style $cellStyle The cell style. * @param string $paddingChar The character used to pad cells. * @param int $indentation The number of spaces to indent. */ public static function drawRow(IO $io, BorderStyle $style, array $row, array $columnLengths, array $alignments, $cellFormat, Style $cellStyle = null, $paddingChar, $indentation = 0) { $totalLines = 0; // Split all cells into lines foreach ($row as $col => $cell) { $row[$col] = explode("\n", $cell); $totalLines = max($totalLines, count($row[$col])); } $nbColumns = count($row); $borderVLChar = $io->format($style->getLineVLChar(), $style->getStyle()); $borderVCChar = $io->format($style->getLineVCChar(), $style->getStyle()); $borderVRChar = $io->format($style->getLineVRChar(), $style->getStyle()); for ($i = 0; $i < $totalLines; ++$i) { $line = str_repeat(' ', $indentation); $line .= $borderVLChar; foreach ($row as $col => &$remainingLines) { $cellLine = $remainingLines ? array_shift($remainingLines) : ''; $totalPadLength = $columnLengths[$col] - StringUtil::getLength($cellLine, $io); $paddingLeft = ''; $paddingRight = ''; if ($totalPadLength > 0) { $alignment = isset($alignments[$col]) ? $alignments[$col] : Alignment::LEFT; switch ($alignment) { case Alignment::LEFT: $paddingRight = str_repeat($paddingChar, $totalPadLength); break; case Alignment::RIGHT: $paddingLeft = str_repeat($paddingChar, $totalPadLength); break; case Alignment::CENTER: $leftPadLength = floor($totalPadLength / 2); $paddingLeft = str_repeat($paddingChar, $leftPadLength); $paddingRight = str_repeat($paddingChar, $totalPadLength - $leftPadLength); break; } } $line .= $io->format(sprintf($cellFormat, $paddingLeft . $cellLine . $paddingRight), $cellStyle); $line .= $col < $nbColumns - 1 ? $borderVCChar : $borderVRChar; } // Remove trailing space $io->write(rtrim($line) . "\n"); } }
private function getCellWrapper(Formatter $formatter, $screenWidth, $excessColumnWidth, $indentation) { $borderStyle = $this->style->getBorderStyle(); $borderWidth = StringUtil::getLength($borderStyle->getLineVLChar()) + ($this->nbColumns - 1) * StringUtil::getLength($borderStyle->getLineVCChar()) + StringUtil::getLength($borderStyle->getLineVRChar()); $availableWidth = $screenWidth - $indentation - $borderWidth - $this->nbColumns * $excessColumnWidth; $wrapper = new CellWrapper(); foreach ($this->headerRow as $headerCell) { $wrapper->addCell($headerCell); } foreach ($this->rows as $row) { foreach ($row as $cell) { $wrapper->addCell($cell); } } $wrapper->fit($availableWidth, $this->nbColumns, $formatter); return $wrapper; }
private function initRows(Formatter $formatter) { $row = null; $col = 0; foreach ($this->cells as $i => $cell) { if (0 === $col) { $this->wrappedRows[] = array(); $this->cellLengths[] = array(); $row =& $this->wrappedRows[count($this->wrappedRows) - 1]; $cellLengths =& $this->cellLengths[count($this->cellLengths) - 1]; } $row[$col] = $cell; $cellLengths[$col] = StringUtil::getLength($cell, $formatter); $this->columnLengths[$col] = max($this->columnLengths[$col], $cellLengths[$col]); $col = ($col + 1) % $this->nbColumns; } // Fill last row up if ($col > 0) { while ($col < $this->nbColumns) { $row[$col] = ''; $cellLengths[$col] = 0; ++$col; } } $this->totalWidth = array_sum($this->columnLengths); }
private function getCellWrapper(Formatter $formatter, $screenWidth, $excessColumnWidth, $indentation) { $borderStyle = $this->style->getBorderStyle(); $wrapper = new CellWrapper(); foreach ($this->cells as $cell) { $wrapper->addCell($cell); } $nbColumns = min($this->maxNbColumns, $wrapper->getEstimatedNbColumns($screenWidth)); do { $borderWidth = StringUtil::getLength($borderStyle->getLineVLChar()) + ($nbColumns - 1) * StringUtil::getLength($borderStyle->getLineVCChar()) + StringUtil::getLength($borderStyle->getLineVRChar()); $availableWidth = $screenWidth - $indentation - $borderWidth - $nbColumns * $excessColumnWidth; $wrapper->fit($availableWidth, $nbColumns, $formatter); --$nbColumns; } while ($wrapper->hasWordCuts() && $nbColumns >= $this->minNbColumns); return $wrapper; }