Skip to content

Commit 797392e

Browse files
authored
fix(console): select default option in ask component (#1139)
1 parent 3fc2a15 commit 797392e

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

src/Tempest/Console/src/Components/Concerns/OpensInEditor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ private function supportsOpeningInEditor(): bool
2222
return is_subclass_of(static::class, CanOpenInEditor::class) && ((bool) $this->getEditorCommand());
2323
}
2424

25-
private function getEditorCommand(): string
25+
private function getEditorCommand(): ?string
2626
{
27-
return env('TEMPEST_EDITOR') ?? env('EDITOR');
27+
return env('TEMPEST_EDITOR') ?? env('EDITOR') ?? null;
2828
}
2929

3030
public function openInEditor(?string $text): string

src/Tempest/Console/src/Components/Interactive/SingleChoiceComponent.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private function getControls(): array
8181
}
8282

8383
if ($this->default !== null) {
84-
$controls['alt+enter'] = 'default';
84+
$controls['tab'] = 'select default';
8585
}
8686

8787
return [
@@ -138,12 +138,10 @@ public function enter(): null|int|string|Stringable|UnitEnum
138138
return $this->options->getRawActiveOption($this->default);
139139
}
140140

141-
#[HandlesKey(Key::ALT_ENTER)]
142-
public function altEnter(): null|int|string|Stringable|UnitEnum
141+
#[HandlesKey(Key::TAB)]
142+
public function tab(): void
143143
{
144144
$this->options->setActive($this->default);
145-
146-
return $this->default;
147145
}
148146

149147
#[HandlesKey(Key::ESCAPE)]

src/Tempest/Console/src/Key.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum Key: string
1212
case CTRL_LEFT = "\eb";
1313
case RIGHT = "\e[C";
1414
case CTRL_RIGHT = "\ef";
15+
case TAB = "\t";
1516
case ENTER = "\n";
1617
case ALT_ENTER = "\e\n";
1718
case BACKSPACE = "\x7F";

tests/Integration/Console/Components/SingleChoiceComponentTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ public function test_default(): void
8585
->call(function (): void {
8686
$component = new SingleChoiceComponent(label: 'Enter a name', options: ['Brent', 'Paul', 'Aidan', 'Roman'], default: 'Paul');
8787

88-
$this->assertSame('Paul', $component->altEnter());
88+
$component->tab();
89+
90+
$this->assertSame('Paul', $component->enter());
8991
});
9092
}
9193
}

tests/Integration/Console/Fixtures/InteractiveCommand.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ public function single(): void
6666
'b',
6767
'c',
6868
],
69-
default: 1,
70-
multiple: true,
69+
default: 'b',
7170
);
7271

7372
$result = json_encode($result);
@@ -93,6 +92,17 @@ public function single_without_default(): void
9392
$this->console->writeln("You picked <em>{$result}</em>");
9493
}
9594

95+
#[ConsoleCommand('interactive:multiline')]
96+
public function multiline(): void
97+
{
98+
$result = $this->console->ask(
99+
question: 'Write something',
100+
multiline: true,
101+
);
102+
103+
$this->console->writeln($result);
104+
}
105+
96106
#[ConsoleCommand('interactive:multiple')]
97107
public function multiple(): void
98108
{

0 commit comments

Comments
 (0)