From 2e311dccf5dc52891ce177e0a056c47b2f86ad1c Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Sun, 24 Mar 2024 21:27:47 -0400 Subject: [PATCH] Refactor to use suggest() prompt. --- src/Commands/Concerns/CanPromptForDomain.php | 35 +++++++++++++++++++ .../Concerns/ResolvesDomainFromInput.php | 6 ++-- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/Commands/Concerns/CanPromptForDomain.php diff --git a/src/Commands/Concerns/CanPromptForDomain.php b/src/Commands/Concerns/CanPromptForDomain.php new file mode 100644 index 0000000..f4d95e9 --- /dev/null +++ b/src/Commands/Concerns/CanPromptForDomain.php @@ -0,0 +1,35 @@ +mapWithKeys(fn ($name) => [Str::lower($name) => $name]); + + // Prompt for the domain + $domainName = suggest( + label: 'What is the domain?', + options: fn ($value) => collect($choices) + ->filter(fn ($name) => Str::contains($name, $value, ignoreCase: true)) + ->toArray(), + placeholder: 'Start typing to search...', + required: true + ); + + // Normalize the case of the domain name + // if it is an existing domain. + if ($match = $choices->get(Str::lower($domainName))) { + $domainName = $match; + } + + return $domainName; + } +} diff --git a/src/Commands/Concerns/ResolvesDomainFromInput.php b/src/Commands/Concerns/ResolvesDomainFromInput.php index fd32c54..33e1587 100644 --- a/src/Commands/Concerns/ResolvesDomainFromInput.php +++ b/src/Commands/Concerns/ResolvesDomainFromInput.php @@ -10,6 +10,8 @@ trait ResolvesDomainFromInput { + use CanPromptForDomain; + protected ?Domain $domain = null; protected function getOptions() @@ -81,9 +83,7 @@ public function handle() // If the domain is not set, prompt for it if (! $this->domain) { - $this->domain = new Domain( - $this->anticipate('What is the domain?', DomainResolver::domainChoices()) - ); + $this->domain = new Domain($this->promptForDomainName()); } parent::handle();