Webmozart\Console\UI\Component\LabeledParagraph::render PHP Method

render() public method

Renders the paragraph.
public render ( IO $io, integer $indentation )
$io Webmozart\Console\Api\IO\IO The I/O.
$indentation integer The number of spaces to indent.
    public function render(IO $io, $indentation = 0)
    {
        $linePrefix = str_repeat(' ', $indentation);
        $visibleLabel = $io->removeFormat($this->label);
        $styleTagLength = strlen($this->label) - strlen($visibleLabel);
        $textOffset = $this->aligned && $this->alignment ? $this->alignment->getTextOffset() - $indentation : 0;
        $textOffset = max($textOffset, strlen($visibleLabel) + $this->padding);
        $textPrefix = str_repeat(' ', $textOffset);
        // 1 trailing space
        $textWidth = $io->getTerminalDimensions()->getWidth() - 1 - $textOffset - $indentation;
        // TODO replace wordwrap() by implementation that is aware of format codes
        $text = str_replace("\n", "\n" . $linePrefix . $textPrefix, wordwrap($this->text, $textWidth));
        // Add the total length of the style tags ("<b>", ...)
        $labelWidth = $textOffset + $styleTagLength;
        $io->write(rtrim(sprintf("%s%-{$labelWidth}s%s", $linePrefix, $this->label, rtrim($text))) . "\n");
    }

Usage Example

Esempio n. 1
0
    public function testRenderWithLabelDistanceWrapsText()
    {
        $para = new LabeledParagraph('Label', self::LOREM_IPSUM, 6);
        $para->render($this->io);
        $expected = <<<'EOF'
Label      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
           nonumy eirmod tempor invidunt

EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }