-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressDemoCommand.php
More file actions
210 lines (178 loc) · 6.9 KB
/
ProgressDemoCommand.php
File metadata and controls
210 lines (178 loc) · 6.9 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
use WebFiori\Cli\Command;
use WebFiori\Cli\ArgumentOption;
use WebFiori\Cli\Progress\ProgressBarFormat;
use WebFiori\Cli\Progress\ProgressBarStyle;
/**
* Comprehensive progress bar demonstration command.
*
* This command shows:
* - All available progress bar styles
* - Different format templates
* - Custom styling and formatting
* - Real-world usage patterns
* - Performance considerations
*/
class ProgressDemoCommand extends Command {
public function __construct() {
parent::__construct('progress-demo', [
'--style' => [
ArgumentOption::DESCRIPTION => 'Progress bar style (default, ascii, dots, arrow)',
ArgumentOption::OPTIONAL => true,
ArgumentOption::DEFAULT => 'all',
ArgumentOption::VALUES => ['all', 'default', 'ascii', 'dots', 'arrow', 'custom']
],
'--items' => [
ArgumentOption::DESCRIPTION => 'Number of items to process (10-1000)',
ArgumentOption::OPTIONAL => true,
ArgumentOption::DEFAULT => '50'
],
'--delay' => [
ArgumentOption::DESCRIPTION => 'Delay between items in milliseconds',
ArgumentOption::OPTIONAL => true,
ArgumentOption::DEFAULT => '100'
],
'--format' => [
ArgumentOption::DESCRIPTION => 'Progress bar format template',
ArgumentOption::OPTIONAL => true,
ArgumentOption::VALUES => ['basic', 'eta', 'rate', 'verbose', 'custom']
]
], 'Demonstrates progress bar functionality with different styles and formats');
}
public function exec(): int {
$style = $this->getArgValue('--style') ?? 'all';
$items = (int)($this->getArgValue('--items') ?? 50);
$delay = (int)($this->getArgValue('--delay') ?? 100);
$format = $this->getArgValue('--format');
// Validate inputs
if ($items < 10 || $items > 1000) {
$this->error('Number of items must be between 10 and 1000');
return 1;
}
if ($delay < 10 || $delay > 2000) {
$this->error('Delay must be between 10 and 2000 milliseconds');
return 1;
}
$this->showHeader($style, $items, $delay);
if ($style === 'all') {
$this->demonstrateAllStyles($items, $delay, $format);
} else {
$this->demonstrateStyle($style, $items, $delay, $format);
}
$this->showFooter();
return 0;
}
/**
* Demonstrate all available styles.
*/
private function demonstrateAllStyles(int $items, int $delay, ?string $format): void {
$styles = [
'default' => 'Default Style (Unicode)',
'ascii' => 'ASCII Style (Compatible)',
'dots' => 'Dots Style (Circular)',
'arrow' => 'Arrow Style (Directional)'
];
foreach ($styles as $styleKey => $styleTitle) {
$this->info("🎨 $styleTitle");
$this->demonstrateStyle($styleKey, $items, $delay, $format);
$this->println();
// Brief pause between styles
if ($styleKey !== 'arrow') {
usleep(500000); // 0.5 seconds
}
}
// Custom style demonstration
$this->info("🎨 Custom Style (Emoji)");
$this->demonstrateCustomStyle($items, $delay);
}
/**
* Demonstrate custom style with emojis.
*/
private function demonstrateCustomStyle(int $items, int $delay): void {
$customStyle = new ProgressBarStyle('🟩', '⬜', '🟨');
$progressBar = $this->createProgressBar($items)
->setStyle($customStyle)
->setFormat('🚀 {message} [{bar}] {percent}% | ⚡ {rate}/s | ⏱️ {eta}')
->setWidth(30);
$progressBar->start('Processing with emoji style...');
for ($i = 0; $i < $items; $i++) {
usleep($delay * 1000);
$progressBar->advance();
}
$progressBar->finish('🎉 Emoji processing complete!');
}
/**
* Demonstrate a specific style.
*/
private function demonstrateStyle(string $style, int $items, int $delay, ?string $format): void {
$progressBar = $this->createProgressBar($items);
// Apply style
switch ($style) {
case 'default':
$progressBar->setStyle(ProgressBarStyle::DEFAULT);
break;
case 'ascii':
$progressBar->setStyle(ProgressBarStyle::ASCII);
break;
case 'dots':
$progressBar->setStyle(ProgressBarStyle::DOTS);
break;
case 'arrow':
$progressBar->setStyle(ProgressBarStyle::ARROW);
break;
case 'custom':
$this->demonstrateCustomStyle($items, $delay);
return;
}
// Apply format
if ($format) {
$progressBar->setFormat($this->getFormatTemplate($format));
}
// Configure progress bar
$progressBar->setWidth(40)
->setUpdateThrottle(0.05); // Update every 50ms
// Start processing
$progressBar->start("Processing with $style style...");
for ($i = 0; $i < $items; $i++) {
// Simulate work
usleep($delay * 1000);
$progressBar->advance();
}
$progressBar->finish('Complete!');
}
/**
* Get format template by name.
*/
private function getFormatTemplate(string $format): string {
return match ($format) {
'basic' => ProgressBarFormat::DEFAULT_FORMAT,
'eta' => ProgressBarFormat::ETA_FORMAT,
'rate' => ProgressBarFormat::RATE_FORMAT,
'verbose' => ProgressBarFormat::VERBOSE_FORMAT,
'custom' => '📊 [{bar}] {percent}% | 📈 {current}/{total} | 🕐 {elapsed} | 💾 {memory}',
default => ProgressBarFormat::DEFAULT_FORMAT
};
}
/**
* Show demonstration footer.
*/
private function showFooter(): void {
$this->println();
$this->success("✨ Progress bar demonstration completed!");
$this->info("💡 Try different combinations of --style, --items, and --delay");
}
/**
* Show demonstration header.
*/
private function showHeader(string $style, int $items, int $delay): void {
$this->println("🎯 Progress Bar Demonstration");
$this->println("=============================");
$this->println();
$this->info("📊 Demo Configuration:");
$this->println(" • Style: ".($style === 'all' ? 'All styles' : ucfirst($style)));
$this->println(" • Items: $items");
$this->println(" • Delay: {$delay}ms per item");
$this->println(" • Estimated time: ".round(($items * $delay) / 1000, 1)." seconds");
$this->println();
}
}