The alignment takes {@link LabeledParagraph} instances and aligns the texts next to the labels so that all texts start at the same offset. Pass the paragraphs that you want to align to {@link add()}. When you call {@link align()}, the text offset is calculated. You can retrieve the calculated offset with {@link getTextOffset()}.
Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
 public function testRenderWithAlignmentIgnoresIfTextOffsetToSmall()
 {
     $alignment = new LabelAlignment();
     $alignment->setTextOffset(5);
     $para = new LabeledParagraph('Label', 'Text');
     $para->setAlignment($alignment);
     $para->render($this->io);
     $this->assertSame("Label  Text\n", $this->io->fetchOutput());
 }
Exemple #2
0
 /**
  * Renders all elements in the layout.
  *
  * @param IO  $io          The I/O.
  * @param int $indentation The number of spaces to indent.
  */
 public function render(IO $io, $indentation = 0)
 {
     $this->alignment->align($io, $indentation);
     foreach ($this->elements as $i => $element) {
         $element->render($io, $this->indentations[$i] + $indentation);
     }
     $this->elements = array();
 }
 /**
  * Renders the paragraph.
  *
  * @param IO  $io          The I/O.
  * @param int $indentation 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");
 }