Skip to content

Commit

Permalink
Adding "Clear" Function For Cleaning The Terminal (#160)
Browse files Browse the repository at this point in the history
* feat(clear): adding clear command.

* chore(clear): improving playground test.

* fix(syntax): fixing issues of static analysis.

* Move rendering concern

* Fix padding after clear

* Simplify playground example

* Formatting

---------

Co-authored-by: Jess Archer <jess@jessarcher.com>
  • Loading branch information
TarsisioXavier and jessarcher authored Sep 9, 2024
1 parent 1ce74e5 commit 39a2f4e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 0 deletions.
19 changes: 19 additions & 0 deletions playground/clear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

use function Laravel\Prompts\clear;
use function Laravel\Prompts\note;
use function Laravel\Prompts\pause;

require __DIR__ . '/../vendor/autoload.php';

note('This will disappear.');

pause('Press [Enter] to continue.');

clear();

note('This will also disappear.');

pause('Press [Enter] to continue.');

clear();
35 changes: 35 additions & 0 deletions src/Clear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Laravel\Prompts;

class Clear extends Prompt
{
/**
* Clear the terminal.
*/
public function prompt(): bool
{
// Fill the previous newline count so subsequent prompts won't add padding.
static::output()->write(PHP_EOL.PHP_EOL);

$this->writeDirectly($this->renderTheme());

return true;
}

/**
* Clear the terminal.
*/
public function display(): void
{
$this->prompt();
}

/**
* Get the value of the prompt.
*/
public function value(): bool
{
return true;
}
}
3 changes: 3 additions & 0 deletions src/Concerns/Themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Prompts\Concerns;

use InvalidArgumentException;
use Laravel\Prompts\Clear;
use Laravel\Prompts\ConfirmPrompt;
use Laravel\Prompts\MultiSearchPrompt;
use Laravel\Prompts\MultiSelectPrompt;
Expand All @@ -17,6 +18,7 @@
use Laravel\Prompts\Table;
use Laravel\Prompts\TextareaPrompt;
use Laravel\Prompts\TextPrompt;
use Laravel\Prompts\Themes\Default\ClearRenderer;
use Laravel\Prompts\Themes\Default\ConfirmPromptRenderer;
use Laravel\Prompts\Themes\Default\MultiSearchPromptRenderer;
use Laravel\Prompts\Themes\Default\MultiSelectPromptRenderer;
Expand Down Expand Up @@ -60,6 +62,7 @@ trait Themes
Note::class => NoteRenderer::class,
Table::class => TableRenderer::class,
Progress::class => ProgressRenderer::class,
Clear::class => ClearRenderer::class,
],
];

Expand Down
14 changes: 14 additions & 0 deletions src/Themes/Default/ClearRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Prompts\Themes\Default;

class ClearRenderer extends Renderer
{
/**
* Clear the terminal.
*/
public function __invoke(): string
{
return "\033[H\033[J";
}
}
10 changes: 10 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ function pause(string $message = 'Press enter to continue...'): bool
}
}

if (! function_exists('\Laravel\Prompts\clear')) {
/**
* Clear the terminal.
*/
function clear(): void
{
(new Clear())->display();
}
}

if (! function_exists('\Laravel\Prompts\suggest')) {
/**
* Prompt the user for text input with auto-completion.
Expand Down
13 changes: 13 additions & 0 deletions tests/Feature/ClearPromptTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

use Laravel\Prompts\Prompt;

use function Laravel\Prompts\clear;

it('clears', function () {
Prompt::fake();

clear();

Prompt::assertOutputContains("\033[H\033[J");
});

0 comments on commit 39a2f4e

Please sign in to comment.