/** * Takes a string and writes it to the command line, wrapping to a maximum * width. If no maximum width is specified, will wrap to the window's max * width. * * If an int is passed into $pad_left, then all strings after the first * will padded with that many spaces to the left. Useful when printing * short descriptions that need to start on an existing line. * * @param string $string * @param int $max * @param int $pad_left * * @return string */ public static function wrap(string $string = null, int $max = 0, int $pad_left = 0) : string { if (empty($string)) { return ''; } if ($max == 0) { $max = CLI::getWidth(); } if (CLI::getWidth() < $max) { $max = CLI::getWidth(); } $max = $max - $pad_left; $lines = wordwrap($string, $max); if ($pad_left > 0) { $lines = explode("\n", $lines); $first = true; array_walk($lines, function (&$line, $index) use($max, $pad_left, &$first) { if (!$first) { $line = str_repeat(" ", $pad_left) . $line; } else { $first = false; } }); $lines = implode("\n", $lines); } return $lines; }