From 23ea808e8a145653e0ab29e30d4385e49f40a920 Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Tue, 30 Apr 2024 05:46:16 -0700 Subject: [PATCH] Extract string handling methods from DrawsBoxes trait (#142) * Extract string handling methods from DrawsBoxes * formatting * add file --------- Co-authored-by: Taylor Otwell --- src/Themes/Default/Concerns/DrawsBoxes.php | 42 +---------------- .../Default/Concerns/InteractsWithStrings.php | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 src/Themes/Default/Concerns/InteractsWithStrings.php diff --git a/src/Themes/Default/Concerns/DrawsBoxes.php b/src/Themes/Default/Concerns/DrawsBoxes.php index 6eb5a0c..0eaba8c 100644 --- a/src/Themes/Default/Concerns/DrawsBoxes.php +++ b/src/Themes/Default/Concerns/DrawsBoxes.php @@ -6,6 +6,8 @@ trait DrawsBoxes { + use InteractsWithStrings; + protected int $minWidth = 60; /** @@ -55,44 +57,4 @@ protected function box( return $this; } - - /** - * Get the length of the longest line. - * - * @param array $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); - } } diff --git a/src/Themes/Default/Concerns/InteractsWithStrings.php b/src/Themes/Default/Concerns/InteractsWithStrings.php new file mode 100644 index 0000000..25a2363 --- /dev/null +++ b/src/Themes/Default/Concerns/InteractsWithStrings.php @@ -0,0 +1,46 @@ + $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); + } +}