1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\PhpUnitWatcher; |
4
|
|
|
|
5
|
|
|
use Clue\React\Stdio\Stdio; |
6
|
|
|
use Spatie\PhpUnitWatcher\Screens\Screen; |
7
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatter; |
8
|
|
|
|
9
|
|
|
class Terminal |
10
|
|
|
{ |
11
|
|
|
/** @var \Clue\React\Stdio\Stdio */ |
12
|
|
|
protected $io; |
13
|
|
|
|
14
|
|
|
/** @var \Spatie\PhpUnitWatcher\Screens\Screen */ |
15
|
|
|
protected $previousScreen = null; |
16
|
|
|
|
17
|
|
|
/** @var \Spatie\PhpUnitWatcher\Screens\Screen */ |
18
|
|
|
protected $currentScreen = null; |
19
|
|
|
|
20
|
|
|
public function __construct(Stdio $io) |
21
|
|
|
{ |
22
|
|
|
$this->io = $io; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function on(string $eventName, callable $callable) |
26
|
|
|
{ |
27
|
|
|
$this->io->on($eventName, function ($line) use ($callable) { |
28
|
|
|
$callable(trim($line)); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function onKeyPress(callable $callable) |
35
|
|
|
{ |
36
|
|
|
$this->io->once('data', function ($line) use ($callable) { |
37
|
|
|
$callable(trim($line)); |
38
|
|
|
}); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function emptyLine() |
44
|
|
|
{ |
45
|
|
|
$this->write(''); |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function comment(string $message) |
51
|
|
|
{ |
52
|
|
|
$this->write($message, 'comment'); |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function write(string $message = '', $level = null) |
58
|
|
|
{ |
59
|
|
|
if ($level != '') { |
60
|
|
|
$message = "<{$level}>$message</{$level}>"; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$formattedMessage = (new OutputFormatter(true))->format($message); |
64
|
|
|
|
65
|
|
|
$formattedMessage = str_replace('<dim>', "\e[2m", $formattedMessage); |
66
|
|
|
$formattedMessage = str_replace('</dim>', "\e[22m", $formattedMessage); |
67
|
|
|
|
68
|
|
|
$this->io->write($formattedMessage.PHP_EOL); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function displayScreen(Screen $screen, $clearScreen = true) |
74
|
|
|
{ |
75
|
|
|
$this->previousScreen = $this->currentScreen; |
76
|
|
|
|
77
|
|
|
$this->currentScreen = $screen; |
78
|
|
|
|
79
|
|
|
$screen |
80
|
|
|
->useTerminal($this) |
81
|
|
|
->clearPrompt() |
82
|
|
|
->removeAllListeners() |
83
|
|
|
->registerListeners(); |
84
|
|
|
|
85
|
|
|
if ($clearScreen) { |
86
|
|
|
$screen->clear(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$screen->draw(); |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function goBack() |
95
|
|
|
{ |
96
|
|
|
if (is_null($this->previousScreen)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->currentScreen = $this->previousScreen; |
101
|
|
|
|
102
|
|
|
$this->displayScreen($this->currentScreen); |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getPreviousScreen(): Screen |
108
|
|
|
{ |
109
|
|
|
return $this->previousScreen; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function refreshScreen() |
113
|
|
|
{ |
114
|
|
|
if (is_null($this->currentScreen)) { |
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->displayScreen($this->currentScreen); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function isDisplayingScreen(string $screenClassName): bool |
124
|
|
|
{ |
125
|
|
|
if (is_null($this->currentScreen)) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $screenClassName === get_class($this->currentScreen); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function removeAllListeners() |
133
|
|
|
{ |
134
|
|
|
$this->io->removeAllListeners(); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function prompt(string $prompt) |
140
|
|
|
{ |
141
|
|
|
$this->getReadline()->setPrompt($prompt); |
|
|
|
|
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function clearPrompt() |
147
|
|
|
{ |
148
|
|
|
$this->getReadline()->setPrompt(''); |
|
|
|
|
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getReadline() |
154
|
|
|
{ |
155
|
|
|
return $this->io->getReadline(); |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getStdio() |
159
|
|
|
{ |
160
|
|
|
return $this->io; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.