FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tapper/src/Console/Application.php at master · tapperphp/tapper · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
tapperphp
/
tapper
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
7
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tapper
/
src
/
Console
/
Application.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
225 lines (185 loc) · 7.07 KB
Breadcrumbs
tapper
/
src
/
Console
/
Application.php
Copy path
File metadata and controls
225 lines (185 loc) · 7.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace
Tapper
\
Console
;
use
DI
\
Container
;
use
PhpTui
\
Term
\
Actions
;
use
PhpTui
\
Term
\
Event
;
use
PhpTui
\
Term
\
Event
\
CharKeyEvent
;
use
PhpTui
\
Term
\
Event
\
CodedKeyEvent
;
use
PhpTui
\
Term
\
Event
\
MouseEvent
;
use
PhpTui
\
Term
\
EventParser
;
use
PhpTui
\
Term
\
KeyCode
;
use
PhpTui
\
Term
\
MouseEventKind
;
use
PhpTui
\
Term
\
Terminal
;
use
PhpTui
\
Tui
\
Bridge
\
PhpTerm
\
PhpTermBackend
;
use
PhpTui
\
Tui
\
Display
\
Area
;
use
PhpTui
\
Tui
\
Display
\
Display
;
use
PhpTui
\
Tui
\
Extension
\
Core
\
Widget
\
CompositeWidget
;
use
React
\
EventLoop
\
LoopInterface
;
use
React
\
Stream
\
ReadableResourceStream
;
use
Tapper
\
Console
\
Components
\
Filter
;
use
Tapper
\
Console
\
State
\
AppState
;
use
Tapper
\
Console
\
Windows
\
Main
;
use
Tapper
\
Console
\
Windows
\
Popup
;
use
Tapper
\
Server
;
class
Application
{
const
RESIZE_RATE
=
1
/
4
;
const
RENDER_RATE
=
1
/
60
;
private
Component
$
window
;
private
Popup
$
popup
;
private
Filter
$
filter
;
private
Area
$
area
;
private
bool
$
shouldDraw
=
true
;
public
function
__construct
(
private
LoopInterface
$
loop
,
private
Terminal
$
terminal
,
private
Display
$
display
,
private
PhpTermBackend
$
phpTermBackend
,
private
EventParser
$
eventParser
,
private
EventBus
$
eventBus
,
private
CommandInvoker
$
commandInvoker
,
private
Container
$
container
,
private
AppState
$
appState
,
private
Server
$
server
,
) {}
public
function
run
(?
int
$
port
=
null
):
int
{
ErrorHandler::
install
(
$
this
->
appState
);
$
this
->
area
=
$
this
->
phpTermBackend
->
size
();
$
this
->
appState
->
version
=
'
v0.1.1
'
;
$
this
->
appState
->
port
=
$
port
;
$
this
->
terminal
->
execute
(Actions::
alternateScreenEnable
());
$
this
->
terminal
->
execute
(Actions::
cursorHide
());
$
this
->
terminal
->
execute
(Actions::
enableMouseCapture
());
$
this
->
terminal
->
enableRawMode
();
$
this
->
terminal
->
execute
(Actions::
moveCursor
(
0
,
0
));
$
this
->
display
->
clear
();
$
this
->
init
();
$
this
->
startRendering
();
$
this
->
startInputHandling
();
$
this
->
server
->
run
(
$
port
);
$
this
->
loop
->
addSignal
(
SIGINT
,
function
() {
$
this
->
close
();
});
$
this
->
loop
->
addSignal
(
SIGTERM
,
function
() {
$
this
->
close
();
});
$
this
->
loop
->
run
();
return
0
;
}
private
function
init
():
void
{
$
this
->
window
=
$
this
->
container
->
make
(Main::class);
$
this
->
popup
=
$
this
->
container
->
make
(Popup::class);
$
this
->
filter
=
$
this
->
container
->
make
(Filter::class);
// Filter's own key bindings are non-global (see Filter.php), so it needs its
// isActive toggled explicitly — mirrors Main::afterInit()'s LogList/Details dance.
// Synchronous (no futureTick): typing a filter query can drain several char events
// from one stdin read, all dispatched within the same tick, and Filter needs to be
// active in time to catch them — see the matching note in Main::afterInit().
$
this
->
appState
->
observe
(
'
typingMode
'
,
function
(
bool
$
typing
):
void
{
if
(
$
typing
) {
$
this
->
filter
->
activate
();
}
else
{
$
this
->
filter
->
deactivate
();
}
});
}
private
function
startRendering
():
void
{
$
this
->
appState
->
setOnChange
(
fn
() =>
$
this
->
shouldDraw
=
true
);
$
this
->
loop
->
addPeriodicTimer
(
self
::
RESIZE_RATE
,
function
() {
if
(
$
this
->
area
!=
$
this
->
phpTermBackend
->
size
()) {
$
this
->
area
=
$
this
->
phpTermBackend
->
size
();
$
this
->
draw
(
$
this
->
area
);
$
this
->
eventBus
->
emit
(
'
resize
'
);
$
this
->
shouldDraw
=
true
;
}
});
$
this
->
loop
->
addPeriodicTimer
(
self
::
RENDER_RATE
,
function
() {
if
(
$
this
->
shouldDraw
) {
$
this
->
shouldDraw
=
false
;
$
this
->
draw
(
$
this
->
area
);
}
});
}
private
function
draw
(
Area
$
area
):
void
{
$
widgets
= [
$
this
->
window
->
render
(
$
area
)];
if
(
$
this
->
appState
->
popupOpen
) {
$
widgets
[] =
$
this
->
popup
->
render
(
$
area
);
}
if
(
$
this
->
appState
->
typingMode
) {
$
widgets
[] =
$
this
->
filter
->
render
(
$
area
);
}
$
composite
= CompositeWidget::
fromWidgets
(...
$
widgets
);
$
this
->
display
->
draw
(
$
composite
);
}
private
function
startInputHandling
():
void
{
$
stdin
=
new
ReadableResourceStream
(
STDIN
,
$
this
->
loop
);
$
stdin
->
on
(
'
data
'
,
function
(
$
data
) {
$
this
->
eventParser
->
advance
(
$
data
,
false
);
foreach
(
$
this
->
eventParser
->
drain
()
as
$
event
) {
// Captured before handleEvent(): if this exact event is the one that just
// flipped typingMode on (e.g. the '/' that starts a filter), it shouldn't
// also be treated as typed filter input — that key press activates typing
// mode, it doesn't type into it.
$
wasTyping
=
$
this
->
appState
->
typingMode
;
$
this
->
handleEvent
(
$
event
);
if
(
$
wasTyping
) {
$
this
->
handleEventInTypingMode
(
$
event
);
}
}
// `stty raw` (enabled by Terminal::enableRawMode()) disables ISIG, so Ctrl+C
// never reaches us as SIGINT — it arrives as byte 0x03 (ETX) on stdin instead.
// Ctrl+C always quits; 'q' only quits outside typing mode, since it's a normal
// character you'd type into the filter box otherwise.
if
(
$
data
===
"\x03"
|| (!
$
this
->
appState
->
typingMode
&&
$
data
===
'
q
'
)) {
$
this
->
close
();
}
});
}
private
function
handleEvent
(
Event
$
event
):
void
{
$
supportedEvents
= [
CharKeyEvent::class,
CodedKeyEvent::class,
MouseEvent::class,
];
if
(
in_array
(
$
event
::class,
$
supportedEvents
)) {
$
this
->
eventBus
->
emit
(
$
event
);
}
}
private
function
handleEventInTypingMode
(
Event
$
event
):
void
{
if
(!
$
this
->
appState
->
typingMode
) {
return
;
}
if
(
$
event
instanceof
MouseEvent) {
if
(
$
event
->
kind
!== MouseEventKind::Down) {
return
;
}
$
this
->
appState
->
typingMode
=
false
;
}
if
(
$
event
instanceof
CharKeyEvent) {
// Emit the event's own char, not the raw stdin chunk — a single stdin read can
// drain multiple events (fast typing/paste), and they'd otherwise all share the
// same raw bytes, duplicating input.
$
this
->
eventBus
->
emit
(
'
input
'
, [
'
char
'
=>
$
event
->
char
]);
return
;
}
if
(
$
event
instanceof
CodedKeyEvent &&
$
event
->
code
=== KeyCode::Esc) {
$
this
->
appState
->
typingMode
=
false
;
return
;
}
}
public
function
close
():
void
{
$
this
->
loop
->
stop
();
$
this
->
terminal
->
disableRawMode
();
$
this
->
terminal
->
execute
(Actions::
disableMouseCapture
());
$
this
->
terminal
->
execute
(Actions::
cursorShow
());
$
this
->
terminal
->
execute
(Actions::
alternateScreenDisable
());
}
}
Back
|
FazBrowse Home
|
New Git URL