Skip to content

Commit

Permalink
Merge pull request #937 from hydephp/change-input-string-handler-term…
Browse files Browse the repository at this point in the history
…ination-sequence

Allow input stream termination by sending end-of-transmission character
  • Loading branch information
caendesilva authored Feb 8, 2023
2 parents ad5afcc + 7864de5 commit dc92431
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
16 changes: 14 additions & 2 deletions packages/publications/src/Commands/Helpers/InputStreamHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function explode;
use function fgets;
use Hyde\Hyde;
use function str_contains;
use function trim;

/**
Expand All @@ -20,6 +21,7 @@
class InputStreamHandler
{
public const TERMINATION_SEQUENCE = '<<<';
public const END_OF_TRANSMISSION = "\x04";

private static ?array $mockedStreamBuffer = null;

Expand Down Expand Up @@ -49,7 +51,7 @@ protected function getLinesFromInputStream(): array

protected function shouldTerminate(string $line): bool
{
return $line === self::TERMINATION_SEQUENCE;
return $line === self::TERMINATION_SEQUENCE || str_contains($line, self::END_OF_TRANSMISSION);
}

/** @codeCoverageIgnore Allows for mocking of the standard input stream */
Expand All @@ -59,12 +61,22 @@ protected function readInputStream(): string
return array_shift(self::$mockedStreamBuffer) ?? '';
}

return fgets(STDIN);
return fgets(STDIN) ?: self::END_OF_TRANSMISSION;
}

/** @internal Allows for mocking of the standard input stream */
public static function mockInput(string $input): void
{
self::$mockedStreamBuffer = explode("\n", $input);
}

public static function terminationMessage(): string
{
return sprintf('Terminate with <comment>%s</comment> or press %s to finish', self::TERMINATION_SEQUENCE, self::getShortcut());
}

protected static function getShortcut(): string
{
return '<comment>Ctrl+D</comment>'.(PHP_OS_FAMILY === 'Windows' ? ' then <comment>Enter</comment>' : '');
}
}
4 changes: 2 additions & 2 deletions packages/publications/src/Commands/MakePublicationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ protected function captureFieldInput(PublicationFieldDefinition $field): ?Public

protected function captureTextFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
{
$this->infoComment("Enter lines for field [$field->name] <fg=gray>(terminate with '<<<')</>");
$this->infoComment(sprintf("Enter lines for field [$field->name] (%s)", InputStreamHandler::terminationMessage()));

return new PublicationFieldValue(PublicationFieldTypes::Text, implode("\n", InputStreamHandler::call()));
}

protected function captureArrayFieldInput(PublicationFieldDefinition $field): PublicationFieldValue
{
$this->infoComment("Enter values for field [$field->name]");
$this->infoComment(sprintf("Enter values for field [$field->name] (%s)", InputStreamHandler::terminationMessage()));

return new PublicationFieldValue(PublicationFieldTypes::Array, InputStreamHandler::call());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function validateTagName(): void

protected function collectTags(): void
{
$this->info('Enter the tag values: (end with an empty line)');
$this->info(sprintf('Enter the tag values: (%s)', InputStreamHandler::terminationMessage()));
$this->tags = [$this->tagName => InputStreamHandler::call()];
}

Expand Down
28 changes: 28 additions & 0 deletions packages/publications/tests/Feature/InputStreamHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public function testCanTerminateWithHereSequenceAfterCarriageReturns()
$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testCanTerminateWithEndOfTransmissionSequence()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n\x04");

$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testCanCollectMultipleInputLines()
{
InputStreamHandler::mockInput("foo\nbar\nbaz\n<<<");
Expand All @@ -57,6 +64,27 @@ public function testCanEnterMultipleUnixEndings()
$this->assertSame(0, $this->makeCommand(['foo', 'bar', 'baz'])->handle());
}

public function testTerminationMessage()
{
$message = 'Terminate with <comment><<<</comment> or press <comment>Ctrl+D</comment>';
if (PHP_OS_FAMILY === 'Windows') {
$message .= ' then <comment>Enter</comment>';
}
$expected = "$message to finish";

$this->assertSame($expected, InputStreamHandler::terminationMessage());
}

public function testTerminationSequenceConstant()
{
$this->assertSame('<<<', InputStreamHandler::TERMINATION_SEQUENCE);
}

public function testEndOfTransmissionConstant()
{
$this->assertSame("\x04", InputStreamHandler::END_OF_TRANSMISSION);
}

protected function makeCommand(array $expected): TestCommand
{
$command = new TestCommand;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function testCanCreateNewPublicationTag()

$this->artisan('make:publicationTag')
->expectsQuestion('Tag name', 'foo')
->expectsOutput('Enter the tag values: (end with an empty line)')
->expectsOutputToContain('Enter the tag values:')
->expectsOutput('Adding the following tags:')
->expectsOutput(' foo: foo, bar, baz')
->expectsOutput('Saving tag data to [file://'.str_replace('\\', '/', Hyde::path('tags.yml')).']')
Expand All @@ -53,7 +53,7 @@ public function testCanCreateNewPublicationTagWithTagNameArgument()

$this->artisan('make:publicationTag foo')
->expectsOutput('Using tag name [foo] from command line argument')
->expectsOutput('Enter the tag values: (end with an empty line)')
->expectsOutputToContain('Enter the tag values:')
->expectsOutput('Adding the following tags:')
->expectsOutput(' foo: foo, bar, baz')
->expectsOutput('Saving tag data to [file://'.str_replace('\\', '/', Hyde::path('tags.yml')).']')
Expand Down

0 comments on commit dc92431

Please sign in to comment.