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
|
|
|
$this->io->writeln($formattedMessage); |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function displayScreen(Screen $screen, $clearScreen = true) |
72
|
|
|
{ |
73
|
|
|
$this->previousScreen = $this->currentScreen; |
74
|
|
|
|
75
|
|
|
$this->currentScreen = $screen; |
76
|
|
|
|
77
|
|
|
$screen |
78
|
|
|
->useTerminal($this) |
79
|
|
|
->clearPrompt() |
80
|
|
|
->removeAllListeners() |
81
|
|
|
->registerListeners(); |
82
|
|
|
|
83
|
|
|
if ($clearScreen) { |
84
|
|
|
$screen->clear(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$screen->draw(); |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function goBack() |
93
|
|
|
{ |
94
|
|
|
if (is_null($this->previousScreen)) { |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->currentScreen = $this->previousScreen; |
99
|
|
|
|
100
|
|
|
$this->displayScreen($this->currentScreen); |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function refreshScreen() |
106
|
|
|
{ |
107
|
|
|
if (is_null($this->currentScreen)) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->displayScreen($this->currentScreen); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function isDisplayingScreen(string $screenClassName): bool |
117
|
|
|
{ |
118
|
|
|
if (is_null($this->currentScreen)) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $screenClassName === get_class($this->currentScreen); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function removeAllListeners() |
126
|
|
|
{ |
127
|
|
|
$this->io->removeAllListeners(); |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function prompt(string $prompt) |
133
|
|
|
{ |
134
|
|
|
$this->io->getReadline()->setPrompt($prompt); |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function clearPrompt() |
140
|
|
|
{ |
141
|
|
|
$this->io->getReadline()->setPrompt(''); |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|