Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/cli/Streams.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ public static function render( $msg ) {

// If the first argument is not an array just pass to sprintf
if( !is_array( $args[1] ) ) {
// Colorize the message first so sprintf doesn't bitch at us
// Normalize color tokens before sprintf: colorize or strip them so no raw %tokens reach sprintf
if ( Colors::shouldColorize() ) {
$args[0] = Colors::colorize( $args[0] );
} else {
$args[0] = Colors::decolorize( $args[0] );
}

// Escape percent characters for sprintf
Expand Down
16 changes: 16 additions & 0 deletions tests/Test_Cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,20 @@ function test_safe_strlen() {
mb_detect_order( $mb_detect_order );
}
}

function test_render_with_color_tokens_and_sprintf_args_colors_disabled() {
Colors::disable( true );

// Color tokens in format string must not cause "sprintf(): Too few arguments".
$result = \cli\render( '[%C%k%s%N] Starting!', '2024-01-01 12:00:00' );
$this->assertSame( '[2024-01-01 12:00:00] Starting!', $result );
}

function test_render_with_color_tokens_and_sprintf_args_colors_enabled() {
Colors::enable( true );

$result = \cli\render( '[%C%k%s%N] Starting!', '2024-01-01 12:00:00' );
$this->assertStringContainsString( '2024-01-01 12:00:00', $result );
$this->assertStringContainsString( 'Starting!', $result );
}
}