prompts icon indicating copy to clipboard operation
prompts copied to clipboard

Potentially a bug in how `trailingNewLines` is calculated? (Windows only)

Open eecurfew opened this issue 6 months ago • 0 comments

Laravel Prompts Version

0.3.5

Laravel Version

12.15.0

PHP Version

8.4.6

Operating System & Version

Windows 11

Terminal Application

VS Code

Description

Inside Laravel\Prompts\Output\ConsoleOutput::doWrite(), trailing new lines are calculated like so:

$trailingNewLines = strlen($message) - strlen(rtrim($message, \PHP_EOL));

On Windows PHP_EOL is 2 chars \r\n.

Steps To Reproduce

Can't really showcase well, because it's more of an internal thing, but for a unorthodox reproduction see:

// Somewhere early in Laravel's lifecycle, e.g. in Provider...
$output = new \Laravel\Prompts\Output\ConsoleOutput;

dump($output->newLinesWritten()); // 1

$output->writeln("Hi!");

dd($output->newLinesWritten()); // 2

Potential solution

Since it's not uncommon for people to write "\n" instead of PHP_EOL, it might be better to only count \ns?

$trailingNewLines = strlen($message) - strlen(rtrim($message, \PHP_EOL));
if (\PHP_OS_FAMILY === 'Windows') $trailingNewLines = substr_count(substr($message, $trailingNewLines), "\n");

eecurfew avatar May 27 '25 22:05 eecurfew