Skip to content

Commit a25f7cd

Browse files
committed
Add progress bar support
1 parent 4f96a32 commit a25f7cd

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/FileSyncStrategies/MultiDirectional.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace DivineOmega\FileSync\FileSyncStrategies;
44

5+
use DivineOmega\CliProgressBar\ProgressBar;
56
use DivineOmega\FileSync\FileListing\FileListing;
67
use DivineOmega\FileSync\FileListing\FileListingFactory;
78
use DivineOmega\FileSync\FileListing\TransferAction;
@@ -28,10 +29,26 @@ public function withProgressBar(): self
2829

2930
public function begin(): void
3031
{
31-
$transferActions = [];
32+
if ($this->showProgressBar) {
33+
$maxProgress = 1;
34+
$progressBar = new ProgressBar();
35+
$progressBar->setMaxProgress(1);
36+
$progressBar->setMessage('Getting file listings...');
37+
$progressBar->display();
38+
}
3239

3340
$fileListings = FileListingFactory::createFromFilesystems($this->filesystems);
3441

42+
if ($this->showProgressBar) {
43+
$progressBar->advance();
44+
$maxProgress += count($fileListings);
45+
$progressBar->setMaxProgress($maxProgress);
46+
$progressBar->setMessage('Determining differences...');
47+
$progressBar->display();
48+
}
49+
50+
$transferActions = [];
51+
3552
foreach($fileListings as $key => $fileListing) {
3653

3754
$otherFileListings = $fileListings;
@@ -50,10 +67,31 @@ public function begin(): void
5067
$transferActions = array_merge($transferActions, $newTransferActions);
5168

5269
}
70+
71+
if ($this->showProgressBar) {
72+
$progressBar->advance();
73+
$progressBar->display();
74+
}
75+
}
76+
77+
if ($this->showProgressBar) {
78+
$maxProgress += count($transferActions);
79+
$progressBar->setMaxProgress($maxProgress);
80+
$progressBar->setMessage('Transferring files...');
81+
$progressBar->display();
5382
}
5483

5584
foreach($transferActions as $transferAction) {
5685
$transferAction->transfer();
86+
87+
if ($this->showProgressBar) {
88+
$progressBar->advance();
89+
$progressBar->display();
90+
}
91+
}
92+
93+
if ($this->showProgressBar) {
94+
$progressBar->complete();
5795
}
5896
}
5997
}

tests/Integration/MultiDirectionalFileSyncTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function testMultiDirectionalFileSync()
5050
->with($directoryA)
5151
->with($directoryB)
5252
->with($directoryC)
53-
->withProgressBar()
5453
->begin();
5554

5655
$filesA = glob(__DIR__.'/Data/a/*.txt');

0 commit comments

Comments
 (0)