|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tempest\Console; |
| 6 | + |
| 7 | +use Closure; |
| 8 | + |
| 9 | +trait HasConsole |
| 10 | +{ |
| 11 | + public function __construct(private readonly Console $console) |
| 12 | + { |
| 13 | + } |
| 14 | + |
| 15 | + public function readln(): string |
| 16 | + { |
| 17 | + return $this->console->readln(); |
| 18 | + } |
| 19 | + |
| 20 | + public function read(int $bytes): string |
| 21 | + { |
| 22 | + return $this->console->read($bytes); |
| 23 | + } |
| 24 | + |
| 25 | + public function write(string $contents): self |
| 26 | + { |
| 27 | + $this->console->write($contents); |
| 28 | + |
| 29 | + return $this; |
| 30 | + } |
| 31 | + |
| 32 | + public function writeln(string $line = ''): self |
| 33 | + { |
| 34 | + $this->console->writeln($line); |
| 35 | + |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string $question |
| 41 | + * @param array|null $options |
| 42 | + * @param mixed|null $default |
| 43 | + * @param bool $multiple |
| 44 | + * @param bool $asList |
| 45 | + * @param \Tempest\Validation\Rule[] $validation |
| 46 | + * @return string|array |
| 47 | + */ |
| 48 | + public function ask( |
| 49 | + string $question, |
| 50 | + ?array $options = null, |
| 51 | + mixed $default = null, |
| 52 | + bool $multiple = false, |
| 53 | + bool $asList = false, |
| 54 | + array $validation = [], |
| 55 | + ): string|array { |
| 56 | + return $this->console->ask( |
| 57 | + question: $question, |
| 58 | + options: $options, |
| 59 | + default: $default, |
| 60 | + multiple: $multiple, |
| 61 | + asList: $asList, |
| 62 | + validation: $validation, |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function confirm(string $question, bool $default = false): bool |
| 67 | + { |
| 68 | + return $this->console->confirm( |
| 69 | + question: $question, |
| 70 | + default: $default, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function password(string $label = 'Password', bool $confirm = false): string |
| 75 | + { |
| 76 | + return $this->console->password( |
| 77 | + label: $label, |
| 78 | + confirm: $confirm, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public function progressBar(iterable $data, Closure $handler): array |
| 83 | + { |
| 84 | + return $this->console->progressBar( |
| 85 | + data: $data, |
| 86 | + handler: $handler, |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param string $label |
| 92 | + * @param Closure(string $search): array $search |
| 93 | + * @return mixed |
| 94 | + */ |
| 95 | + public function search(string $label, Closure $search): mixed |
| 96 | + { |
| 97 | + return $this->console->search( |
| 98 | + label: $label, |
| 99 | + search: $search, |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + public function info(string $line): self |
| 104 | + { |
| 105 | + $this->console->info($line); |
| 106 | + |
| 107 | + return $this; |
| 108 | + } |
| 109 | + |
| 110 | + public function error(string $line): self |
| 111 | + { |
| 112 | + $this->console->error($line); |
| 113 | + |
| 114 | + return $this; |
| 115 | + } |
| 116 | + |
| 117 | + public function success(string $line): self |
| 118 | + { |
| 119 | + $this->console->success($line); |
| 120 | + |
| 121 | + return $this; |
| 122 | + } |
| 123 | +} |
0 commit comments