Changeset View
Changeset View
Standalone View
Standalone View
src/console/format.php
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Soft wrap text for display on a console, respecting UTF8 character boundaries | * Soft wrap text for display on a console, respecting UTF8 character boundaries | ||||
* and ANSI color escape sequences. | * and ANSI color escape sequences. | ||||
* | * | ||||
* @param string Text to wrap. | * @param string Text to wrap. | ||||
* @param int Optional indent level. | * @param int Optional indent level. | ||||
* @param bool True to also indent the first line. | |||||
* @return string Wrapped text. | * @return string Wrapped text. | ||||
*/ | */ | ||||
function phutil_console_wrap($text, $indent = 0) { | function phutil_console_wrap($text, $indent = 0, $with_prefix = true) { | ||||
$lines = array(); | $lines = array(); | ||||
$width = (78 - $indent); | $width = (78 - $indent); | ||||
$esc = chr(27); | $esc = chr(27); | ||||
$break_pos = null; | $break_pos = null; | ||||
$len_after_break = 0; | $len_after_break = 0; | ||||
$line_len = 0; | $line_len = 0; | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | function phutil_console_wrap($text, $indent = 0, $with_prefix = true) { | ||||
} | } | ||||
$pre = null; | $pre = null; | ||||
if ($indent) { | if ($indent) { | ||||
$pre = str_repeat(' ', $indent); | $pre = str_repeat(' ', $indent); | ||||
} | } | ||||
foreach ($lines as $idx => $line) { | foreach ($lines as $idx => $line) { | ||||
$lines[$idx] = $pre.implode('', $line); | if ($idx == 0 && !$with_prefix) { | ||||
$prefix = null; | |||||
} else { | |||||
$prefix = $pre; | |||||
} | |||||
$lines[$idx] = $prefix.implode('', $line); | |||||
} | } | ||||
return implode('', $lines); | return implode('', $lines); | ||||
} | } | ||||
function phutil_console_require_tty() { | function phutil_console_require_tty() { | ||||
if (function_exists('posix_isatty') && !posix_isatty(STDIN)) { | if (function_exists('posix_isatty') && !posix_isatty(STDIN)) { | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |