Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add karnel command similar #245

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions src/System/Integrate/Console/Karnel.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public function handle($arguments)
{
// handle command empty
$baseArgs = $arguments[1] ?? '--help';
$commands = [];

foreach ($this->commands() as $cmd) {
$commands = array_merge($commands, $cmd->patterns(), $cmd->cmd());

if ($cmd->isMatch($baseArgs)) {
$this->app->set(
$cmd->class(),
Expand All @@ -50,20 +53,57 @@ public function handle($arguments)
}
}

// did you mean
$count = 0;
$similar = (new Style('Did you mean?'))->textLightYellow()->newLines();
foreach ($this->similar($baseArgs, $commands, 4) as $term => $level) {
$similar->push(' > ')->push($term)->textYellow()->newLines();
$count++;
}

// if command not register
(new Style())
->push('Command Not Found, run help command')->textRed()->newLines(2)
->push('> ')->textDim()
->push('php ')->textYellow()
->push('cli ')
->push('--help')->textDim()
->newLines()
->out()
;
if (0 === $count) {
(new Style())
->push('Command Not Found, run help command')->textRed()->newLines(2)
->push('> ')->textDim()
->push('php ')->textYellow()
->push('cli ')
->push('--help')->textDim()
->newLines()
->out()
;

return $this->exit_code = 1;
}

$similar->out();

return $this->exit_code = 1;
}

/**
* Return similar from given array, compare with key.
*
* @param string[] $matchs
*
* @return array<string, int> Sorted from simalar
*/
private function similar(string $find, $matchs, int $threshold = -1)
{
$closest = [];
$find = strtolower($find);

foreach ($matchs as $match) {
$level = levenshtein($find, strtolower($match));
if ($level <= $threshold) {
$closest[$match] = $level;
}
}
asort($closest);

return $closest;
}

/**
* Get karne exit status code.
*
Expand Down
63 changes: 50 additions & 13 deletions tests/Integrate/Console/KarnelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ protected function tearDown(): void
$this->app->flush();
}

/** @test */
public function itCanReturnNothingBecauseCommandNotFound()
{
$karnel = new NormalCommand($this->app);
ob_start();
$exit = $karnel->handle(['cli', 'test']);
$out = ob_get_clean();

$this->assertEquals(1, $exit);
$hasContent = Str::contains($out, 'Command Not Found, run help command');
$this->assertTrue($hasContent);
}

/** @test */
public function itCanCallCommandUsingFullCommand()
{
Expand Down Expand Up @@ -166,6 +153,56 @@ public function itCanCallCommandWithDefaultOption()
$hasContent = Str::contains($out, 'test');
$this->assertTrue($hasContent);
}

/** @test */
public function itCanReturnNothingBecauseCommandNotFound()
{
$karnel = new NormalCommand($this->app);
ob_start();
$exit = $karnel->handle(['cli', 'test']);
ob_get_clean();

$this->assertEquals(1, $exit);
}

/** @test */
public function itCanReturnCommandNotFoundBecauseNotClosetAnotherCommand()
{
$karnel = new NormalCommand($this->app);
ob_start();
$exit = $karnel->handle(['cli', 'xzy']);
$out = ob_get_clean();

$this->assertEquals(1, $exit);
$condition = Str::contains($out, 'Command Not Found, run help command');
$this->assertTrue($condition);
}

/** @test */
public function itCanReturnSuggestionCommand()
{
$karnel = new NormalCommand($this->app);
ob_start();
$exit = $karnel->handle(['cli', 'test']);
$out = ob_get_clean();

$this->assertEquals(1, $exit);
$condition = Str::contains($out, 'Did you mean?');
$this->assertTrue($condition);
}

/** @test */
public function itCanGivenClosetCommand()
{
$karnel = new NormalCommand($this->app);
ob_start();
$exit = $karnel->handle(['cli', 'use:']);
$out = ob_get_clean();

$this->assertEquals(1, $exit);
$condition = Str::contains($out, 'use:full');
$this->assertTrue($condition);
}
}

class NormalCommand extends Karnel
Expand Down
Loading