Use symfony console to handle input
This commit is contained in:
parent
12bd79396f
commit
5e1da51506
4 changed files with 65 additions and 466577 deletions
466550
assets/words.txt
466550
assets/words.txt
File diff suppressed because it is too large
Load diff
12
bin/anagram-finder
Executable file
12
bin/anagram-finder
Executable file
|
|
@ -0,0 +1,12 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/../autoload.php';
|
||||
|
||||
use Lvl0\AnagramFinder\TUI\Commands\FindAnagramWordsCommand;
|
||||
use Symfony\Component\Console\Application;
|
||||
|
||||
$application = new Application('Anagram Finder', '1.0.0');
|
||||
$application->addCommand(new FindAnagramWordsCommand());
|
||||
$application->setDefaultCommand('anagram:find-words');
|
||||
$application->run();
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . '/../autoload.php';
|
||||
|
||||
use Lvl0\AnagramFinder\Core\Matcher;
|
||||
use Lvl0\AnagramFinder\TUI\ArgHandler;
|
||||
use Lvl0\AnagramFinder\TUI\Output;
|
||||
|
||||
use function Laravel\Prompts\text;
|
||||
|
||||
const DICTIONARY = __DIR__ . '/../assets/words.txt';
|
||||
|
||||
Output::line("ANAGRAM FINDER");
|
||||
|
||||
$args = ArgHandler::make($argv);
|
||||
$anagram = $args->get(1) ?: text("Enter the anagram", required: true);
|
||||
$minLength = $args->get(2) ?: text(label: "Enter the minimum length of the results. (default=3)", placeholder: 3, default: 3, required: true);
|
||||
$top10 = $args->hasOption('top');
|
||||
|
||||
Output::line();
|
||||
Output::line("Results for your anagram: " . $anagram);
|
||||
|
||||
$matches = Matcher::findWords($anagram, $minLength);
|
||||
if ($top10) {
|
||||
$matches = $matches->slice(0, 10);
|
||||
}
|
||||
$matches->each(fn(string $word) => Output::line($word . " (" . strlen($word) . ")"));
|
||||
53
src/Commands/FindAnagramWordsCommand.php
Normal file
53
src/Commands/FindAnagramWordsCommand.php
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace Lvl0\AnagramFinder\TUI\Commands;
|
||||
|
||||
use Lvl0\AnagramFinder\Core\Matcher;
|
||||
use Symfony\Component\Console\Attribute\Argument;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Attribute\Option;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
use function Laravel\Prompts\clear;
|
||||
use function Laravel\Prompts\info;
|
||||
use function Laravel\Prompts\spin;
|
||||
use function Laravel\Prompts\table;
|
||||
use function Laravel\Prompts\text;
|
||||
|
||||
#[AsCommand(name: 'anagram:find-words')]
|
||||
class FindAnagramWordsCommand extends Command
|
||||
{
|
||||
public function __invoke(
|
||||
#[Argument]
|
||||
?string $anagram,
|
||||
#[Argument]
|
||||
?int $minLength,
|
||||
#[Option]
|
||||
bool $top = false,
|
||||
): int {
|
||||
clear();
|
||||
|
||||
info("ANAGRAM FINDER");
|
||||
|
||||
$anagram = $anagram ??= text("Enter the anagram", required: true);
|
||||
$minLength = $minLength ?: text(label: "Enter the minimum length of the results. (default=3)", placeholder: 3, default: 3, required: true);
|
||||
|
||||
info("Results for your anagram: " . $anagram);
|
||||
|
||||
$matches = spin(
|
||||
callback: fn() => Matcher::findWords($anagram, $minLength),
|
||||
message: 'Checking wordlist...',
|
||||
);
|
||||
|
||||
if ($top) {
|
||||
$matches = $matches->slice(0, 10);
|
||||
}
|
||||
|
||||
table(
|
||||
headers: ["word", "length"],
|
||||
rows: $matches->map(fn(string $word) => ['word' => $word, 'length' => strlen($word)])->toArray(),
|
||||
);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue