Skip to content

Commit 83ecde3

Browse files
committed
refactor(command): refactor MusicCommand.php
- Use Laravel Prompts functions for user input - Update table and confirm prompts - Update download choice prompt
1 parent 6f686ba commit 83ecde3

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

app/Commands/MusicCommand.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
use Symfony\Component\Console\Input\InputInterface;
3030
use Symfony\Component\Console\Output\OutputInterface;
3131

32+
use function Laravel\Prompts\confirm;
33+
use function Laravel\Prompts\multiselect;
34+
use function Laravel\Prompts\table;
35+
use function Laravel\Prompts\text;
36+
3237
final class MusicCommand extends Command
3338
{
3439
use Sanitizer;
@@ -71,7 +76,7 @@ public function handle(Timer $timer, ResourceUsageFormatter $resourceUsageFormat
7176
})
7277
->when(windows_os(), fn () => $this->line($this->config['windows_tip']))
7378
->pipe(function () use ($timer, &$songs, &$sanitizedSongs, $resourceUsageFormatter, &$choices, &$lastKey): Collection {
74-
$keyword = str($this->argument('keyword') ?? $this->ask($this->config['search_tip'], '腰乐队'))->trim()->toString();
79+
$keyword = str($this->argument('keyword') ?? text($this->config['search_tip'], '关键字', '腰乐队', true))->trim()->toString();
7580
$sources = array_filter((array) $this->option('sources')) ?: $this->config['sources'];
7681

7782
$this->line(sprintf($this->config['searching'], $keyword));
@@ -85,35 +90,35 @@ public function handle(Timer $timer, ResourceUsageFormatter $resourceUsageFormat
8590
}
8691

8792
$sanitizedSongs = $this->sanitizes($songs, $keyword);
88-
$this->table($this->config['table_header'], $sanitizedSongs);
93+
table($this->config['table_header'], $sanitizedSongs);
8994
$this->info($resourceUsageFormatter->resourceUsage($duration));
90-
if (! $this->confirm($this->config['confirm_download'], true)) {
95+
if (! confirm($this->config['confirm_download'])) {
9196
$this->rehandle();
9297
}
9398

9499
$choices = collect($sanitizedSongs)
95100
->transform(static fn (array $song): string => implode(' ', Arr::except($song, [0])))
96101
->add($this->config['download_all_songs']);
97102

98-
return collect($this->choice(
103+
return collect(multiselect(
99104
$this->config['download_choice_tip'],
100105
$choices->all(),
101-
$lastKey = ($choices->count() - 1),
102-
null,
106+
$lastKey = [$choices->last()],
107+
10,
103108
true
104109
));
105110
})
106111
->transform(static fn (string $selectedValue): bool|int|string => $choices->search($selectedValue))
107-
->pipe(static function (Collection $selectedKeys) use ($lastKey, $songs): Collection {
108-
if (\in_array($lastKey, $selectedKeys->all(), true)) {
112+
->pipe(static function (Collection $selectedKeys) use ($choices, $songs): Collection {
113+
if (\in_array($choices->count() - 1, $selectedKeys->all(), true)) {
109114
return collect($songs);
110115
}
111116

112117
return collect($songs)->only($selectedKeys->all());
113118
})
114119
->each(function (array $song, int $index) use ($sanitizedSongs): void {
115120
try {
116-
$this->table($sanitizedSongs[$index], []);
121+
table($sanitizedSongs[$index], []);
117122
$savePath = Utils::getSavePath($song, $this->option('dir'));
118123
$this->music->download($song['url'], $savePath);
119124
$this->line(sprintf($this->config['download_success_tip'], $savePath));

0 commit comments

Comments
 (0)