Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* develop: clear the terminal on an iteration basis
- Loading branch information
Showing
10 changed files
with
278 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Innmind\LabStation; | ||
|
||
use Innmind\CLI\Environment; | ||
use Innmind\Immutable\Str; | ||
|
||
final class Iteration | ||
{ | ||
private bool $shouldClearTerminal = true; | ||
|
||
public function start(): void | ||
{ | ||
$this->shouldClearTerminal = true; | ||
} | ||
|
||
public function failing(): void | ||
{ | ||
$this->shouldClearTerminal = false; | ||
} | ||
|
||
public function end(Environment $env): void | ||
{ | ||
if (!$this->shouldClearTerminal) { | ||
return; | ||
} | ||
|
||
if ($env->arguments()->contains('--keep-output')) { | ||
return; | ||
} | ||
|
||
// clear terminal | ||
$env->output()->write(Str::of("\033[2J\033[H")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Tests\Innmind\LabStation; | ||
|
||
use Innmind\LabStation\Iteration; | ||
use Innmind\CLI\Environment; | ||
use Innmind\Stream\Writable; | ||
use Innmind\Immutable\{ | ||
Sequence, | ||
Str, | ||
}; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class IterationTest extends TestCase | ||
{ | ||
public function testEndingAnIterationWithoutAStartWillClearTheTerminal() | ||
{ | ||
$iteration = new Iteration; | ||
$env = $this->createMock(Environment::class); | ||
$env | ||
->expects($this->any()) | ||
->method('arguments') | ||
->willReturn(Sequence::strings()); | ||
$env | ||
->expects($this->any()) | ||
->method('output') | ||
->willReturn($output = $this->createMock(Writable::class)); | ||
$output | ||
->expects($this->once()) | ||
->method('write') | ||
->with(Str::of("\033[2J\033[H")); | ||
|
||
$this->assertNull($iteration->end($env)); | ||
} | ||
|
||
public function testNormalIterationWithoutAFailureWillClearTheTerminal() | ||
{ | ||
$iteration = new Iteration; | ||
$env = $this->createMock(Environment::class); | ||
$env | ||
->expects($this->any()) | ||
->method('arguments') | ||
->willReturn(Sequence::strings()); | ||
$env | ||
->expects($this->any()) | ||
->method('output') | ||
->willReturn($output = $this->createMock(Writable::class)); | ||
$output | ||
->expects($this->once()) | ||
->method('write') | ||
->with(Str::of("\033[2J\033[H")); | ||
|
||
$this->assertNull($iteration->start()); | ||
$this->assertNull($iteration->end($env)); | ||
} | ||
|
||
public function testNormalIterationWithAFailureWillNotClearTheTerminal() | ||
{ | ||
$iteration = new Iteration; | ||
$env = $this->createMock(Environment::class); | ||
$env | ||
->expects($this->any()) | ||
->method('arguments') | ||
->willReturn(Sequence::strings()); | ||
$env | ||
->expects($this->never()) | ||
->method('output'); | ||
|
||
$this->assertNull($iteration->start()); | ||
$this->assertNull($iteration->failing()); | ||
$this->assertNull($iteration->end($env)); | ||
} | ||
|
||
public function testNormalIterationWithoutAFailureWillNotClearTheTerminalWhenExplicitlyAskToKeepOutput() | ||
{ | ||
$iteration = new Iteration; | ||
$env = $this->createMock(Environment::class); | ||
$env | ||
->expects($this->any()) | ||
->method('arguments') | ||
->willReturn(Sequence::strings('--keep-output')); | ||
$env | ||
->expects($this->never()) | ||
->method('output'); | ||
|
||
$this->assertNull($iteration->start()); | ||
$this->assertNull($iteration->end($env)); | ||
} | ||
|
||
public function testNormalIterationWithAFailureWillNotClearTheTerminalWhenExplicitlyAskToKeepOutput() | ||
{ | ||
$iteration = new Iteration; | ||
$env = $this->createMock(Environment::class); | ||
$env | ||
->expects($this->any()) | ||
->method('arguments') | ||
->willReturn(Sequence::strings('--keep-output')); | ||
$env | ||
->expects($this->never()) | ||
->method('output'); | ||
|
||
$this->assertNull($iteration->start()); | ||
$this->assertNull($iteration->failing()); | ||
$this->assertNull($iteration->end($env)); | ||
} | ||
|
||
public function testClearTheTerminalEvenWhenPreviousIterationFailed() | ||
{ | ||
$iteration = new Iteration; | ||
$env = $this->createMock(Environment::class); | ||
$env | ||
->expects($this->any()) | ||
->method('arguments') | ||
->willReturn(Sequence::strings()); | ||
$env | ||
->expects($this->any()) | ||
->method('output') | ||
->willReturn($output = $this->createMock(Writable::class)); | ||
$output | ||
->expects($this->once()) | ||
->method('write') | ||
->with(Str::of("\033[2J\033[H")); | ||
|
||
$iteration->start(); | ||
$iteration->failing(); | ||
$iteration->end($env); | ||
$this->assertNull($iteration->start()); | ||
$this->assertNull($iteration->end($env)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.