This repository has been archived by the owner on Mar 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressElement.php
146 lines (132 loc) · 3.61 KB
/
ProgressElement.php
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
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Cli\Component\Element;
use GrizzIt\Task\Common\TaskListInterface;
use Ulrack\Cli\Common\Io\WriterInterface;
use Ulrack\Cli\Component\Theme\VoidStyle;
use Ulrack\Cli\Common\Io\TerminalInterface;
use Ulrack\Cli\Common\Theme\StyleInterface;
use Ulrack\Cli\Common\Element\ElementInterface;
class ProgressElement implements ElementInterface
{
/**
* Contains the writer for the output.
*
* @var WriterInterface
*/
private $writer;
/**
* Contains the characters for displaying the progress.
*
* @var array
*/
private $progressCharacters;
/**
* Contains the style for the progress text.
*
* @var StyleInterface
*/
private $style;
/**
* Contains the style for the progress bar.
*
* @var StyleInterface
*/
private $progressStyle;
/**
* Contains the terminal reader.
*
* @var TerminalInterface
*/
private $terminal;
/**
* Contains the task list used to determine the progress.
*
* @var TaskListInterface
*/
private $taskList;
/**
* Constructor.
*
* @param WriterInterface $writer
* @param array $progressCharacters
* @param TerminalInterface $terminal
* @param TaskListInterface $taskList
* @param StyleInterface $style
* @param StyleInterface $progressStyle
*/
public function __construct(
WriterInterface $writer,
array $progressCharacters,
TerminalInterface $terminal,
TaskListInterface $taskList,
StyleInterface $style = null,
StyleInterface $progressStyle = null
) {
$this->writer = $writer;
$this->progressCharacters = $progressCharacters;
$this->terminal = $terminal;
$this->taskList = $taskList;
$this->style = $style ?? new VoidStyle();
$this->progressStyle = $progressStyle ?? new VoidStyle();
}
/**
* Renders the element.
*
* @return void
*/
public function render(): void
{
$maxWidth = $this->terminal->getWidth();
$progressWidth = round($maxWidth * .6);
foreach ($this->taskList as $name => $percentage) {
$this->writer->overWrite(
"\033[K" .
$this->generateProgressBar($progressWidth, $percentage) .
sprintf(
' %s (%d%%)',
$name,
round($percentage, 1)
)
);
}
$this->writer->writeLine(
"\033[K" .
$this->generateProgressBar(
$progressWidth,
100
) . ' Done. (100%)'
);
}
/**
* Generates the progress bar.
*
* @param int $maxWidth
* @param float $progress
*
* @return string
*/
private function generateProgressBar(int $maxWidth, float $progress): string
{
$maxWidth = $maxWidth - 2;
$innerProgress = 1;
if ($progress > 0) {
$innerProgress = round($maxWidth * ($progress / 100));
}
$leftOver = $maxWidth - $innerProgress;
return $this->progressCharacters['border']['left'] .
str_repeat(
$this->progressCharacters['progress']['done'],
$innerProgress
) .
$this->progressCharacters['progress']['current'] .
str_repeat(
$this->progressCharacters['progress']['pending'],
$leftOver
) .
$this->progressCharacters['border']['right'];
}
}