Skip to content

Commit 5b1ac49

Browse files
feat: add 'q' key press to quit.
1 parent 8e5751e commit 5b1ac49

2 files changed

Lines changed: 34 additions & 9 deletions

File tree

app/Commands/ConfigCommand.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace App\Commands;
66

77
use App\Services\ConfigService;
8+
use Laravel\Prompts\ConfirmPrompt;
9+
use Laravel\Prompts\MultiSelectPrompt;
10+
use Laravel\Prompts\SelectPrompt;
811
use LaravelZero\Framework\Commands\Command;
912

10-
use function Laravel\Prompts\confirm;
11-
use function Laravel\Prompts\multiselect;
12-
use function Laravel\Prompts\select;
1313
use function Laravel\Prompts\text;
1414

1515
class ConfigCommand extends Command
@@ -63,16 +63,18 @@ private function handleList(ConfigService $config): int
6363

6464
$this->newLine();
6565
$this->line(' <options=bold>cnkill config</> — select which folder types to scan.');
66-
$this->line(' <fg=gray>Use <space> to toggle, <enter> to save. Run `cnkill config add` to add custom types.</>');
66+
$this->line(' <fg=gray>Use <space> to toggle, <enter> to save, <q> to quit. Run `cnkill config add` to add custom types.</>');
6767
$this->newLine();
6868

69-
$selected = multiselect(
69+
$prompt = new MultiSelectPrompt(
7070
label: 'Enabled folder types',
7171
options: $options,
7272
default: $currentlyEnabled,
7373
scroll: min(count($options), 20),
7474
hint: 'Changes take effect immediately for all future scans.',
7575
);
76+
$prompt->on('key', fn (string $key) => $key === 'q' ? exit(0) : null);
77+
$selected = $prompt->prompt();
7678

7779
if (! is_array($selected)) {
7880
$this->newLine();
@@ -116,7 +118,7 @@ private function handleAdd(ConfigService $config): int
116118

117119
$this->newLine();
118120
$this->line(' <options=bold>cnkill config add</> — define a custom folder type to scan.');
119-
$this->line(' <fg=gray>Press Ctrl-C at any time to cancel.</>');
121+
$this->line(' <fg=gray>Press <q> or Ctrl-C at any time to cancel.</>');
120122
$this->newLine();
121123

122124
// ── 1. Folder name / pattern ─────────────────────────────────────────
@@ -206,7 +208,9 @@ private function handleAdd(ConfigService $config): int
206208
$this->line(sprintf(' Lockfiles: <fg=gray>%s</>', $lockfiles ? implode(', ', $lockfiles) : 'none'));
207209
$this->newLine();
208210

209-
$confirmed = confirm(label: 'Save this custom type?', default: true);
211+
$confirmPrompt = new ConfirmPrompt(label: 'Save this custom type?', default: true);
212+
$confirmPrompt->on('key', fn (string $k) => $k === 'q' ? exit(0) : null);
213+
$confirmed = $confirmPrompt->prompt();
210214

211215
if (! $confirmed) {
212216
$this->newLine();
@@ -265,20 +269,24 @@ private function handleRemove(ConfigService $config): int
265269
$options[$key] = $type['label'];
266270
}
267271

268-
$key = select(
272+
$selectPrompt = new SelectPrompt(
269273
label: 'Which custom type do you want to remove?',
270274
options: $options,
271275
scroll: min(count($options), 15),
272276
);
277+
$selectPrompt->on('key', fn (string $k) => $k === 'q' ? exit(0) : null);
278+
$key = $selectPrompt->prompt();
273279

274280
$typeLabel = $customTypes[(string) $key]['label'] ?? (string) $key;
275281

276282
$this->newLine();
277-
$confirmed = confirm(
283+
$confirmPrompt = new ConfirmPrompt(
278284
label: "Remove \"{$typeLabel}\"?",
279285
default: false,
280286
hint: 'This cannot be undone.',
281287
);
288+
$confirmPrompt->on('key', fn (string $k) => $k === 'q' ? exit(0) : null);
289+
$confirmed = $confirmPrompt->prompt();
282290

283291
if (! $confirmed) {
284292
$this->newLine();

app/Providers/AppServiceProvider.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class AppServiceProvider extends ServiceProvider
1919
public function boot(): void
2020
{
2121
$this->registerPromptTheme();
22+
$this->registerPromptCancelHandler();
2223

2324
$this->app->terminating(function (): void {
2425
$this->maybeShowUpgradeNotice();
@@ -52,6 +53,22 @@ private function registerPromptTheme(): void
5253
Prompt::theme('cnkill');
5354
}
5455

56+
// -------------------------------------------------------------------------
57+
// Prompt cancel handler
58+
// -------------------------------------------------------------------------
59+
60+
/**
61+
* Register a global cancel handler so that Ctrl-C (and programmatic
62+
* cancellations triggered by pressing 'q') exit cleanly with code 0
63+
* instead of the default exit(1).
64+
*/
65+
private function registerPromptCancelHandler(): void
66+
{
67+
Prompt::cancelUsing(function (): never {
68+
exit(0);
69+
});
70+
}
71+
5572
// -------------------------------------------------------------------------
5673
// Upgrade notice
5774
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)