-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract string handling methods from DrawsBoxes trait (#142)
* Extract string handling methods from DrawsBoxes * formatting * add file --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
733c937
commit 23ea808
Showing
2 changed files
with
48 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Laravel\Prompts\Themes\Default\Concerns; | ||
|
||
trait InteractsWithStrings | ||
{ | ||
/** | ||
* Get the length of the longest line. | ||
* | ||
* @param array<string> $lines | ||
*/ | ||
protected function longest(array $lines, int $padding = 0): int | ||
{ | ||
return max( | ||
$this->minWidth, | ||
collect($lines) | ||
->map(fn ($line) => mb_strwidth($this->stripEscapeSequences($line)) + $padding) | ||
->max() | ||
); | ||
} | ||
|
||
/** | ||
* Pad text ignoring ANSI escape sequences. | ||
*/ | ||
protected function pad(string $text, int $length, string $char = ' '): string | ||
{ | ||
$rightPadding = str_repeat($char, max(0, $length - mb_strwidth($this->stripEscapeSequences($text)))); | ||
|
||
return "{$text}{$rightPadding}"; | ||
} | ||
|
||
/** | ||
* Strip ANSI escape sequences from the given text. | ||
*/ | ||
protected function stripEscapeSequences(string $text): string | ||
{ | ||
// Strip ANSI escape sequences. | ||
$text = preg_replace("/\e[^m]*m/", '', $text); | ||
|
||
// Strip Symfony named style tags. | ||
$text = preg_replace("/<(info|comment|question|error)>(.*?)<\/\\1>/", '$2', $text); | ||
|
||
// Strip Symfony inline style tags. | ||
return preg_replace("/<(?:(?:[fb]g|options)=[a-z,;]+)+>(.*?)<\/>/i", '$1', $text); | ||
} | ||
} |