Skip to content

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

@eecurfew

Description

@eecurfew

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");

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions