Chris@0: defaultWidth = $defaultWidth; Chris@0: } Chris@0: Chris@0: public function setApplication(Application $application) Chris@0: { Chris@0: $this->application = $application; Chris@0: } Chris@0: Chris@0: public function setTerminal($terminal) Chris@0: { Chris@0: $this->terminal = $terminal; Chris@0: } Chris@0: Chris@0: public function getTerminal() Chris@0: { Chris@0: if (!$this->terminal && class_exists('\Symfony\Component\Console\Terminal')) { Chris@0: $this->terminal = new \Symfony\Component\Console\Terminal(); Chris@0: } Chris@0: return $this->terminal; Chris@0: } Chris@0: Chris@0: public function enableWrap($shouldWrap) Chris@0: { Chris@0: $this->shouldWrap = $shouldWrap; Chris@0: } Chris@0: Chris@0: public function prepare(CommandData $commandData, FormatterOptions $options) Chris@0: { Chris@0: $width = $this->getTerminalWidth(); Chris@0: if (!$width) { Chris@0: $width = $this->defaultWidth; Chris@0: } Chris@0: Chris@0: // Enforce minimum and maximum widths Chris@0: $width = min($width, $this->getMaxWidth($commandData)); Chris@0: $width = max($width, $this->getMinWidth($commandData)); Chris@0: Chris@0: $options->setWidth($width); Chris@0: } Chris@0: Chris@0: protected function getTerminalWidth() Chris@0: { Chris@0: // Don't wrap if wrapping has been disabled. Chris@0: if (!$this->shouldWrap) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: $terminal = $this->getTerminal(); Chris@0: if ($terminal) { Chris@0: return $terminal->getWidth(); Chris@0: } Chris@0: Chris@0: return $this->getTerminalWidthViaApplication(); Chris@0: } Chris@0: Chris@0: protected function getTerminalWidthViaApplication() Chris@0: { Chris@0: if (!$this->application) { Chris@0: return 0; Chris@0: } Chris@0: $dimensions = $this->application->getTerminalDimensions(); Chris@0: if ($dimensions[0] == null) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: return $dimensions[0]; Chris@0: } Chris@0: Chris@0: protected function getMaxWidth(CommandData $commandData) Chris@0: { Chris@0: return $this->maxWidth; Chris@0: } Chris@0: Chris@0: protected function getMinWidth(CommandData $commandData) Chris@0: { Chris@0: return $this->minWidth; Chris@0: } Chris@0: }