|
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->getInput()->once('data', function ($line) use ($callable) { |
|
37
|
|
|
$this->io->getReadline()->deleteChar(0); |
|
38
|
|
|
$callable(trim($line)); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function emptyLine() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->write(''); |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function comment(string $message) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->write($message, 'comment'); |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function write(string $message = '', $level = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($level != '') { |
|
61
|
|
|
$message = "<{$level}>$message</{$level}>"; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$formattedMessage = (new OutputFormatter(true))->format($message); |
|
65
|
|
|
|
|
66
|
|
|
$formattedMessage = str_replace('<dim>', "\e[2m", $formattedMessage); |
|
67
|
|
|
$formattedMessage = str_replace('</dim>', "\e[22m", $formattedMessage); |
|
68
|
|
|
|
|
69
|
|
|
$this->io->writeln($formattedMessage); |
|
70
|
|
|
|
|
71
|
|
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function displayScreen(Screen $screen, $clearScreen = true) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->previousScreen = $this->currentScreen; |
|
77
|
|
|
|
|
78
|
|
|
$this->currentScreen = $screen; |
|
79
|
|
|
|
|
80
|
|
|
$screen |
|
81
|
|
|
->useTerminal($this) |
|
82
|
|
|
->clearPrompt() |
|
83
|
|
|
->removeAllListeners() |
|
84
|
|
|
->registerListeners(); |
|
85
|
|
|
|
|
86
|
|
|
if ($clearScreen) { |
|
87
|
|
|
$screen->clear(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$screen->draw(); |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function goBack() |
|
96
|
|
|
{ |
|
97
|
|
|
if (is_null($this->previousScreen)) { |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->currentScreen = $this->previousScreen; |
|
102
|
|
|
|
|
103
|
|
|
$this->displayScreen($this->currentScreen); |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getPreviousScreen(): Screen |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->previousScreen; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function refreshScreen() |
|
114
|
|
|
{ |
|
115
|
|
|
if (is_null($this->currentScreen)) { |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->displayScreen($this->currentScreen); |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function isDisplayingScreen(string $screenClassName): bool |
|
125
|
|
|
{ |
|
126
|
|
|
if (is_null($this->currentScreen)) { |
|
127
|
|
|
return false; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $screenClassName === get_class($this->currentScreen); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function removeAllListeners() |
|
134
|
|
|
{ |
|
135
|
|
|
$this->io->removeAllListeners(); |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function prompt(string $prompt) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->io->getReadline()->setPrompt($prompt); |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function clearPrompt() |
|
148
|
|
|
{ |
|
149
|
|
|
$this->io->getReadline()->setPrompt(''); |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|