1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\Core\Io; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
final class ConsoleIo |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var InputInterface |
21
|
|
|
*/ |
22
|
|
|
private $input; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var OutputInterface |
26
|
|
|
*/ |
27
|
|
|
private $output; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $lastMessage; |
33
|
|
|
|
34
|
|
|
public function __construct(InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
$this->input = $input; |
37
|
|
|
$this->output = $output; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string|array $messages |
42
|
|
|
* @param bool $newline |
43
|
|
|
*/ |
44
|
|
|
public function write($messages, bool $newline = true) |
45
|
|
|
{ |
46
|
|
|
$this->output->write($messages, $newline); |
47
|
|
|
$this->lastMessage = implode($newline ? "\n" : '', (array) $messages); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string|array $messages |
52
|
|
|
* @param bool $newline |
53
|
|
|
* @param int $size |
54
|
|
|
*/ |
55
|
|
|
public function overwrite($messages, bool $newline = true, int $size = null) |
56
|
|
|
{ |
57
|
|
|
// messages can be an array, let's convert it to string anyway |
58
|
|
|
$messages = implode($newline ? "\n" : '', (array) $messages); |
59
|
|
|
|
60
|
|
|
// since overwrite is supposed to overwrite last message... |
61
|
|
|
if (!isset($size)) { |
62
|
|
|
// removing possible formatting of lastMessage with strip_tags |
63
|
|
|
$size = strlen(strip_tags($this->lastMessage)); |
64
|
|
|
} |
65
|
|
|
// ...let's fill its length with backspaces |
66
|
|
|
$this->write(str_repeat("\x08", $size), false); |
67
|
|
|
|
68
|
|
|
// write the new message |
69
|
|
|
$this->write($messages, false); |
70
|
|
|
|
71
|
|
|
$fill = $size - strlen(strip_tags($messages)); |
72
|
|
|
if ($fill > 0) { |
73
|
|
|
// whitespace whatever has left |
74
|
|
|
$this->write(str_repeat(' ', $fill), false); |
75
|
|
|
// move the cursor back |
76
|
|
|
$this->write(str_repeat("\x08", $fill), false); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($newline) { |
80
|
|
|
$this->write(''); |
81
|
|
|
} |
82
|
|
|
$this->lastMessage = $messages; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|