1 | <?php |
||
2 | |||
3 | namespace BlueConsole; |
||
4 | |||
5 | class Animation |
||
6 | { |
||
7 | /** |
||
8 | * @var \DateInterval |
||
9 | */ |
||
10 | protected $interval; |
||
11 | |||
12 | public function __construct(int $steps = 6, string $cursor = '*') |
||
13 | { |
||
14 | $counter = 0; |
||
15 | $forward = true; |
||
16 | $before = 0; |
||
17 | $after = $steps - \strlen($cursor); |
||
18 | |||
19 | echo "\n"; |
||
20 | |||
21 | while (true) { |
||
22 | echo '['; |
||
23 | |||
24 | for ($i = 1; $i <= $before; $i++) { |
||
25 | echo ' '; |
||
26 | } |
||
27 | |||
28 | echo $cursor; |
||
29 | |||
30 | for ($i = 0; $i <= $after; $i++) { |
||
31 | echo ' '; |
||
32 | } |
||
33 | |||
34 | echo ']'; |
||
35 | echo "\r"; |
||
36 | |||
37 | $counter++; |
||
38 | |||
39 | if ($forward) { |
||
40 | $before++; |
||
41 | $after--; |
||
42 | } else { |
||
43 | $before--; |
||
44 | $after++; |
||
45 | } |
||
46 | |||
47 | if ($counter === ($steps - (\strlen($cursor) -1))) { |
||
48 | $counter = 0; |
||
49 | $forward = !$forward; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
50 | } |
||
51 | |||
52 | sleep(1); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | public function setWaitTime(\DateInterval $dateInterval) |
||
57 | { |
||
58 | // $interval = new \DateInterval('PT10S'); |
||
59 | $dateInterval->invert = true; |
||
60 | //dump($interval); |
||
61 | |||
62 | $d1 = new \DateTime(); |
||
63 | while (true) { |
||
64 | $d2 = new \DateTime(); |
||
65 | $d2->add($dateInterval); |
||
66 | |||
67 | $iv = $d2->diff($d1); |
||
68 | dump($iv->s); |
||
69 | if ($iv->s === 0) { |
||
70 | echo "end\n"; |
||
71 | exit(0); |
||
72 | } |
||
73 | sleep(1); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | public function setMessage(string $message) |
||
78 | { |
||
79 | |||
80 | } |
||
81 | |||
82 | public function run($process) |
||
83 | { |
||
84 | //display message with animation |
||
85 | //execute external process |
||
86 | //wait and display animation |
||
87 | //process ended -> display message and go fodder |
||
88 | } |
||
89 | |||
90 | protected function displayAnimation() |
||
91 | { |
||
92 | $counter = 0; |
||
93 | $forward = true; |
||
94 | $before = 0; |
||
95 | $after = $steps - \strlen($cursor); |
||
96 | |||
97 | while (true) { |
||
98 | echo '['; |
||
99 | |||
100 | for ($i = 1; $i <= $before; $i++) { |
||
101 | echo ' '; |
||
102 | } |
||
103 | |||
104 | echo $cursor; |
||
105 | |||
106 | for ($i = 0; $i <= $after; $i++) { |
||
107 | echo ' '; |
||
108 | } |
||
109 | |||
110 | echo ']'; |
||
111 | echo "\r"; |
||
112 | |||
113 | $counter++; |
||
114 | |||
115 | if ($forward) { |
||
116 | $before++; |
||
117 | $after--; |
||
118 | } else { |
||
119 | $before--; |
||
120 | $after++; |
||
121 | } |
||
122 | |||
123 | if ($counter === ($steps - (\strlen($cursor) -1))) { |
||
124 | $counter = 0; |
||
125 | $forward = !$forward; |
||
0 ignored issues
–
show
|
|||
126 | } |
||
127 | |||
128 | sleep(1); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | protected function executeProcess() |
||
133 | { |
||
134 | |||
135 | } |
||
136 | } |
||
137 |