Skip to content

Commit 4935020

Browse files
Copilotswissspidy
andcommitted
Refactor: Add ellipsis constants and simplify pre-colorized check
- Define ELLIPSIS and ELLIPSIS_WIDTH as class constants for better maintainability - Remove redundant width check in pre-colorized condition (already validated earlier) - Addresses code review feedback from @swissspidy Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 867bc04 commit 4935020

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/cli/table/Ascii.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class Ascii extends Renderer {
2424
*/
2525
private const VALID_WRAPPING_MODES = array( 'wrap', 'word-wrap', 'truncate' );
2626

27+
/**
28+
* Ellipsis character(s) used for truncation.
29+
*/
30+
private const ELLIPSIS = '...';
31+
32+
/**
33+
* Width of the ellipsis in characters.
34+
*/
35+
private const ELLIPSIS_WIDTH = 3;
36+
2737
protected $_characters = array(
2838
'corner' => '+',
2939
'line' => '-',
@@ -264,17 +274,14 @@ protected function wrapText( $text, $width, $encoding, $is_precolorized ) {
264274

265275
// Handle truncate mode
266276
if ( 'truncate' === $this->_wrapping_mode ) {
267-
$ellipsis = '...';
268-
$ellipsis_width = 3;
269-
270-
if ( $width <= $ellipsis_width ) {
277+
if ( $width <= self::ELLIPSIS_WIDTH ) {
271278
// Not enough space for ellipsis, just truncate
272279
return array( \cli\safe_substr( $text, 0, $width, true /*is_width*/, $encoding ) );
273280
}
274281

275282
// Truncate and add ellipsis
276-
$truncated = \cli\safe_substr( $text, 0, $width - $ellipsis_width, true /*is_width*/, $encoding );
277-
return array( $truncated . $ellipsis );
283+
$truncated = \cli\safe_substr( $text, 0, $width - self::ELLIPSIS_WIDTH, true /*is_width*/, $encoding );
284+
return array( $truncated . self::ELLIPSIS );
278285
}
279286

280287
// Handle word-wrap mode
@@ -287,7 +294,7 @@ protected function wrapText( $text, $width, $encoding, $is_precolorized ) {
287294
$line = $text;
288295

289296
// Use the new color-aware wrapping for pre-colorized content
290-
if ( $is_precolorized && Colors::width( $line, true, $encoding ) > $width ) {
297+
if ( $is_precolorized ) {
291298
$wrapped_lines = Colors::wrapPreColorized( $line, $width, $encoding );
292299
} else {
293300
// For non-colorized content, use character-boundary wrapping

0 commit comments

Comments
 (0)