mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-02 21:41:19 +08:00
created Console::wrapText() to imrove console help output on small screens
This commit is contained in:
@ -635,6 +635,46 @@ class BaseConsole
|
||||
return $size = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Word wrap text with indentation to fit the screen size
|
||||
*
|
||||
* If screen size could not be detected, or the indentation is greater than the screen size, the text will not be wrapped.
|
||||
*
|
||||
* The first line will **not** be indented, so `Console::wrapText("Lorem ipsum dolor sit amet.", 4)` will result in the
|
||||
* following output, given the screen width is 16 characters:
|
||||
*
|
||||
* ```
|
||||
* Lorem ipsum
|
||||
* dolor sit
|
||||
* amet.
|
||||
* ```
|
||||
*
|
||||
* @param string $text the text to be wrapped
|
||||
* @param integer $indent number of spaces to use for indentation.
|
||||
* @param boolean $refresh whether to force refresh of screen size.
|
||||
* This will be passed to [[getScreenSize()]].
|
||||
* @return string the wrapped text.
|
||||
* @since 2.0.3
|
||||
*/
|
||||
public static function wrapText($text, $indent = 0, $refresh = false)
|
||||
{
|
||||
$size = static::getScreenSize($refresh);
|
||||
if ($size === false || $size[0] <= $indent) {
|
||||
return $text;
|
||||
}
|
||||
$pad = str_repeat(' ', $indent);
|
||||
$lines = explode("\n", wordwrap($text, $size[0] - $indent, "\n", true));
|
||||
$first = true;
|
||||
foreach($lines as $i => $line) {
|
||||
if ($first) {
|
||||
$first = false;
|
||||
continue;
|
||||
}
|
||||
$lines[$i] = $pad . $line;
|
||||
}
|
||||
return implode("\n", $lines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets input from STDIN and returns a string right-trimmed for EOLs.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user