Skip to content

Commit 23ea808

Browse files
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>
1 parent 733c937 commit 23ea808

File tree

2 files changed

+48
-40
lines changed

2 files changed

+48
-40
lines changed

src/Themes/Default/Concerns/DrawsBoxes.php

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
trait DrawsBoxes
88
{
9+
use InteractsWithStrings;
10+
911
protected int $minWidth = 60;
1012

1113
/**
@@ -55,44 +57,4 @@ protected function box(
5557

5658
return $this;
5759
}
58-
59-
/**
60-
* Get the length of the longest line.
61-
*
62-
* @param array<string> $lines
63-
*/
64-
protected function longest(array $lines, int $padding = 0): int
65-
{
66-
return max(
67-
$this->minWidth,
68-
collect($lines)
69-
->map(fn ($line) => mb_strwidth($this->stripEscapeSequences($line)) + $padding)
70-
->max()
71-
);
72-
}
73-
74-
/**
75-
* Pad text ignoring ANSI escape sequences.
76-
*/
77-
protected function pad(string $text, int $length, string $char = ' '): string
78-
{
79-
$rightPadding = str_repeat($char, max(0, $length - mb_strwidth($this->stripEscapeSequences($text))));
80-
81-
return "{$text}{$rightPadding}";
82-
}
83-
84-
/**
85-
* Strip ANSI escape sequences from the given text.
86-
*/
87-
protected function stripEscapeSequences(string $text): string
88-
{
89-
// Strip ANSI escape sequences.
90-
$text = preg_replace("/\e[^m]*m/", '', $text);
91-
92-
// Strip Symfony named style tags.
93-
$text = preg_replace("/<(info|comment|question|error)>(.*?)<\/\\1>/", '$2', $text);
94-
95-
// Strip Symfony inline style tags.
96-
return preg_replace("/<(?:(?:[fb]g|options)=[a-z,;]+)+>(.*?)<\/>/i", '$1', $text);
97-
}
9860
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Laravel\Prompts\Themes\Default\Concerns;
4+
5+
trait InteractsWithStrings
6+
{
7+
/**
8+
* Get the length of the longest line.
9+
*
10+
* @param array<string> $lines
11+
*/
12+
protected function longest(array $lines, int $padding = 0): int
13+
{
14+
return max(
15+
$this->minWidth,
16+
collect($lines)
17+
->map(fn ($line) => mb_strwidth($this->stripEscapeSequences($line)) + $padding)
18+
->max()
19+
);
20+
}
21+
22+
/**
23+
* Pad text ignoring ANSI escape sequences.
24+
*/
25+
protected function pad(string $text, int $length, string $char = ' '): string
26+
{
27+
$rightPadding = str_repeat($char, max(0, $length - mb_strwidth($this->stripEscapeSequences($text))));
28+
29+
return "{$text}{$rightPadding}";
30+
}
31+
32+
/**
33+
* Strip ANSI escape sequences from the given text.
34+
*/
35+
protected function stripEscapeSequences(string $text): string
36+
{
37+
// Strip ANSI escape sequences.
38+
$text = preg_replace("/\e[^m]*m/", '', $text);
39+
40+
// Strip Symfony named style tags.
41+
$text = preg_replace("/<(info|comment|question|error)>(.*?)<\/\\1>/", '$2', $text);
42+
43+
// Strip Symfony inline style tags.
44+
return preg_replace("/<(?:(?:[fb]g|options)=[a-z,;]+)+>(.*?)<\/>/i", '$1', $text);
45+
}
46+
}

0 commit comments

Comments
 (0)