Skip to content

Commit a45e558

Browse files
Copilotswissspidy
andcommitted
Fix sprintf(): Too few arguments when color tokens in format string and colorization disabled
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 894bc74 commit a45e558

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/cli/Streams.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public static function render( $msg ) {
4444
// Colorize the message first so sprintf doesn't bitch at us
4545
if ( Colors::shouldColorize() ) {
4646
$args[0] = Colors::colorize( $args[0] );
47+
} else {
48+
$args[0] = Colors::decolorize( $args[0] );
4749
}
4850

4951
// Escape percent characters for sprintf

tests/Test_Cli.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,20 @@ function test_safe_strlen() {
553553
mb_detect_order( $mb_detect_order );
554554
}
555555
}
556+
557+
function test_render_with_color_tokens_and_sprintf_args_colors_disabled() {
558+
Colors::disable( true );
559+
560+
// Color tokens in format string must not cause "sprintf(): Too few arguments".
561+
$result = \cli\render( '[%C%k%s%N] Starting!', '2024-01-01 12:00:00' );
562+
$this->assertSame( '[2024-01-01 12:00:00] Starting!', $result );
563+
}
564+
565+
function test_render_with_color_tokens_and_sprintf_args_colors_enabled() {
566+
Colors::enable( true );
567+
568+
$result = \cli\render( '[%C%k%s%N] Starting!', '2024-01-01 12:00:00' );
569+
$this->assertStringContainsString( '2024-01-01 12:00:00', $result );
570+
$this->assertStringContainsString( 'Starting!', $result );
571+
}
556572
}

0 commit comments

Comments
 (0)