From c7fd150e00480467c0193411827c45c3b33c3878 Mon Sep 17 00:00:00 2001 From: Len Woodward Date: Sun, 14 Apr 2024 01:03:06 -0700 Subject: [PATCH 1/3] Extract string handling methods from DrawsBoxes --- src/Themes/Default/Concerns/DrawsBoxes.php | 42 +---------------- .../Default/Concerns/HandlesStrings.php | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 src/Themes/Default/Concerns/HandlesStrings.php diff --git a/src/Themes/Default/Concerns/DrawsBoxes.php b/src/Themes/Default/Concerns/DrawsBoxes.php index 6eb5a0ce..c7555eb4 100644 --- a/src/Themes/Default/Concerns/DrawsBoxes.php +++ b/src/Themes/Default/Concerns/DrawsBoxes.php @@ -6,6 +6,8 @@ trait DrawsBoxes { + use HandlesStrings; + 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/HandlesStrings.php b/src/Themes/Default/Concerns/HandlesStrings.php new file mode 100644 index 00000000..9507c5d1 --- /dev/null +++ b/src/Themes/Default/Concerns/HandlesStrings.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); + } +} From 88e16e18e2b1145d3d505a86ada43925be3009a3 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 30 Apr 2024 07:45:31 -0500 Subject: [PATCH 2/3] formatting --- src/Themes/Default/Concerns/DrawsBoxes.php | 2 +- .../Default/Concerns/HandlesStrings.php | 46 ------------------- 2 files changed, 1 insertion(+), 47 deletions(-) delete mode 100644 src/Themes/Default/Concerns/HandlesStrings.php diff --git a/src/Themes/Default/Concerns/DrawsBoxes.php b/src/Themes/Default/Concerns/DrawsBoxes.php index c7555eb4..0eaba8ce 100644 --- a/src/Themes/Default/Concerns/DrawsBoxes.php +++ b/src/Themes/Default/Concerns/DrawsBoxes.php @@ -6,7 +6,7 @@ trait DrawsBoxes { - use HandlesStrings; + use InteractsWithStrings; protected int $minWidth = 60; diff --git a/src/Themes/Default/Concerns/HandlesStrings.php b/src/Themes/Default/Concerns/HandlesStrings.php deleted file mode 100644 index 9507c5d1..00000000 --- a/src/Themes/Default/Concerns/HandlesStrings.php +++ /dev/null @@ -1,46 +0,0 @@ - $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); - } -} From 3eb29eb81b5d8e4d9a4f47b2d141b8374094fed1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 30 Apr 2024 07:45:37 -0500 Subject: [PATCH 3/3] add file --- .../Default/Concerns/InteractsWithStrings.php | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/Themes/Default/Concerns/InteractsWithStrings.php diff --git a/src/Themes/Default/Concerns/InteractsWithStrings.php b/src/Themes/Default/Concerns/InteractsWithStrings.php new file mode 100644 index 00000000..25a2363f --- /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); + } +}