Skip to content

Commit

Permalink
Show abort status in progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Nov 26, 2019
1 parent c37d0aa commit 0488f7a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
);
SymfonyProgressBar::setFormatDefinition(
'portal',
'[%host%] Downloaded: %current_volume% | %speed% | %elapsed%/%estimated% | %percent%%'
'[%host%] Downloaded: %current_volume% | %speed% | %elapsed%/%estimated% |%aborted% %percent%%'
);
SymfonyProgressBar::setPlaceholderFormatterDefinition('current_volume', function (SymfonyProgressBar $bar) {
return Helper::formatMemory($bar->getProgress());
Expand All @@ -31,6 +31,9 @@
SymfonyProgressBar::setPlaceholderFormatterDefinition('host', function (SymfonyProgressBar $bar) {
return $bar->host;
});
SymfonyProgressBar::setPlaceholderFormatterDefinition('aborted', function (SymfonyProgressBar $bar) {
return empty($bar->aborted) ? '' : ' aborted at';
});

class ProgressBar
{
Expand Down Expand Up @@ -65,4 +68,10 @@ public function finish(): void
{
$this->wrappedProgressBar->finish();
}

public function abort(): void
{
$this->wrappedProgressBar->aborted = true;
$this->wrappedProgressBar->display();
}
}
1 change: 1 addition & 0 deletions src/Responder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public function __invoke(Socket $socket): \Generator
$this->logger->debug("$remoteAddress finished download");
} catch (StreamException $exception) {
$this->logger->debug("$remoteAddress aborted download");
$progressBar->abort();
}

$socket->end();
Expand Down
6 changes: 5 additions & 1 deletion tests/ResponderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Ostrolucky\Stdinho\Responder;
use PHPUnit\Framework\TestCase;
use Psr\Log\Test\TestLogger;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\ConsoleSectionOutput;
use function Amp\Promise\wait;

class ResponderTest extends TestCase
Expand All @@ -25,12 +27,14 @@ public function testResponderHandlesClientAbruptDisconnect(): void
$responder = new Responder(
$logger = new TestLogger(),
new ResolvedBufferer(__FILE__),
$this->createMock(ConsoleOutput::class),
$output = $this->createMock(ConsoleOutput::class),
[],
$this->createMock(InputStream::class),
new Deferred()
);

$output->method('section')->willReturn($sectionOutput = $this->createMock(ConsoleSectionOutput::class));
$sectionOutput->method('getFormatter')->willReturn($this->createMock(OutputFormatterInterface::class));
$writer = new ResourceOutputStream($resource = fopen('php://memory', 'rwb'));
$socket = $this->createMock(Socket::class);

Expand Down

0 comments on commit 0488f7a

Please sign in to comment.