File: progress.php

package info (click to toggle)
php-laravel-prompts 0.1.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: php: 5,928; xml: 14; makefile: 8
file content (57 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

use function Laravel\Prompts\progress;

require __DIR__.'/../vendor/autoload.php';

$states = [
    'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
    'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
];

progress(
    label: 'Adding States',
    steps: $states,
    callback: function ($item, $progress) {
        usleep(250_000);

        if ($item === 'Arkansas') {
            $progress->label = 'Arkansas is not a state! Nice try.';
        }

        return $item.' added.';
    },
);

progress(
    label: 'Adding States With Label',
    steps: $states,
    callback: function ($item, $progress) {
        usleep(250_000);
        $progress
            ->label('Adding '.$item)
            ->hint("{$item} has ".strlen($item).' characters');
    },
);

$progress = progress(
    label: 'Adding States Manually',
    steps: $states,
);

$progress->start();

foreach ($states as $state) {
    usleep(250_000);
    $progress
        ->hint($state)
        ->advance();
}

$progress->finish();

progress(
    'Processing with Exception',
    $states,
    fn ($item) => $item === 'Arkansas' ? throw new Exception('Issue with Arkansas!') : usleep(250_000),
);