本文整理匯總了PHP中CLI::getWidth方法的典型用法代碼示例。如果您正苦於以下問題:PHP CLI::getWidth方法的具體用法?PHP CLI::getWidth怎麽用?PHP CLI::getWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類CLI
的用法示例。
在下文中一共展示了CLI::getWidth方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: wrap
/**
* 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;
}