FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnetLab/Test/REPLinWPF.cs at master · harperjohnarc/pythonnetLab · GitHub
harperjohnarc
pythonnetLab
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnetLab
/
Test
/
REPLinWPF.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
263 lines (231 loc) · 7.53 KB
Breadcrumbs
pythonnetLab
/
Test
/
REPLinWPF.cs
Copy path
File metadata and controls
263 lines (231 loc) · 7.53 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using
Python
.
Runtime
;
using
System
;
using
System
.
IO
;
using
System
.
Text
;
using
System
.
Threading
;
using
System
.
Windows
;
using
System
.
Windows
.
Controls
;
using
System
.
Windows
.
Input
;
namespace
Test
{
class
REPLinWPF
{
static
AutoResetEvent
mEvent
=
new
AutoResetEvent
(
false
)
;
public
static
void
Main
(
)
{
Thread
th
=
new
Thread
(
(
)
=>
{
Window
window
=
new
Window
(
)
;
window
.
Content
=
new
REPLTextBox
(
)
;
window
.
ShowDialog
(
)
;
mEvent
.
Set
(
)
;
}
)
;
th
.
SetApartmentState
(
ApartmentState
.
STA
)
;
th
.
Start
(
)
;
mEvent
.
WaitOne
(
)
;
}
}
public
class
REPLTextBox
:
UserControl
{
public
REPLTextBox
(
)
{
this
.
TB
=
new
TextBox
(
)
{
HorizontalAlignment
=
HorizontalAlignment
.
Stretch
,
VerticalAlignment
=
VerticalAlignment
.
Stretch
}
;
this
.
Content
=
this
.
TB
;
this
.
Prompt
=
">>> "
;
this
.
HistoryCommand
=
new
StringBuilder
(
)
;
this
.
TB
.
Text
=
""
;
this
.
TextBasePosition
=
0
;
using
(
Py
.
GIL
(
)
)
{
this
.
WelcomeMsg
=
$
"Python
{
PythonEngine
.
Version
}
\n
"
;
}
this
.
Clear
(
)
;
this
.
TB
.
KeyDown
+=
new
KeyEventHandler
(
this
.
_OnKeyDown
)
;
this
.
TB
.
PreviewKeyDown
+=
new
KeyEventHandler
(
this
.
_OnPreviewKeyDown
)
;
this
.
TB
.
PreviewTextInput
+=
new
TextCompositionEventHandler
(
this
.
_OnPreviewTextInput
)
;
this
.
PreviewKeyUp
+=
new
KeyEventHandler
(
this
.
_OnPreviewKeyUp
)
;
this
.
TB
.
KeyUp
+=
new
KeyEventHandler
(
this
.
_OnKeyUp
)
;
using
(
Py
.
GIL
(
)
)
{
PyScope
=
Py
.
CreateScope
(
)
;
}
Console
.
SetOut
(
new
TBTextWriter
(
this
)
)
;
}
private
TextBox
TB
;
private
PyScope
PyScope
;
// It's not enough when block input is needed.
private
int
TextBasePosition
;
private
string
WelcomeMsg
;
private
string
HistoryText
;
public
string
Prompt
{
get
;
private
set
;
}
//StringBuilder will be replace with a custom class
// it will be used in _OnKeyUp
public
StringBuilder
HistoryCommand
{
get
;
private
set
;
}
//text behind Base Postion cannot be deleted with backspace
private
void
UpdateTextBasePosition
(
)
{
this
.
TextBasePosition
=
this
.
TB
.
Text
.
Length
;
this
.
HistoryText
=
this
.
TB
.
Text
;
}
private
void
Clear
(
)
{
this
.
TB
.
Text
=
this
.
WelcomeMsg
;
this
.
TB
.
Text
+=
this
.
Prompt
;
this
.
UpdateTextBasePosition
(
)
;
this
.
TB
.
ScrollToEnd
(
)
;
this
.
HistoryCommand
.
Clear
(
)
;
}
private
void
WriteLine
(
string
value
)
{
this
.
TB
.
Text
+=
value
;
this
.
TB
.
Text
+=
Environment
.
NewLine
;
}
public
void
Write
(
string
value
)
{
this
.
TB
.
Text
+=
value
;
}
//It will triggered first
//But, it's only triggered after cursor moved by user
private
void
_OnPreviewKeyDown
(
object
sender
,
KeyEventArgs
e
)
{
if
(
(
int
)
e
.
Key
<
34
)
{
return
;
}
if
(
this
.
TB
.
SelectionStart
<
this
.
TextBasePosition
)
{
this
.
TB
.
SelectionStart
=
this
.
TextBasePosition
;
this
.
TB
.
SelectionLength
=
0
;
e
.
Handled
=
true
;
}
}
private
void
_OnKeyDown
(
object
sender
,
KeyEventArgs
e
)
{
if
(
e
.
Key
==
Key
.
Return
|
e
.
Key
==
Key
.
Enter
)
// Catch a carrage return
{
this
.
SendCommand
(
)
;
}
}
/// <summary>
/// send the command to python
/// </summary>
private
void
SendCommand
(
)
{
string
rawCommand
=
this
.
TB
.
Text
.
Remove
(
0
,
this
.
TextBasePosition
)
.
Trim
(
)
;
if
(
rawCommand
.
EndsWith
(
":"
)
)
{
// Not implement
this
.
TB
.
Text
+=
Environment
.
NewLine
;
this
.
TB
.
Text
+=
this
.
Prompt
;
this
.
Focus
(
)
;
this
.
TB
.
Select
(
this
.
TB
.
Text
.
Length
,
0
)
;
//move cursor to the end
return
;
}
if
(
!
string
.
IsNullOrEmpty
(
rawCommand
)
)
{
this
.
TB
.
Text
+=
Environment
.
NewLine
;
using
(
Py
.
GIL
(
)
)
{
try
{
PyObject
script
=
PythonEngine
.
Compile
(
rawCommand
,
""
,
RunFlagType
.
Single
)
;
var
res
=
this
.
PyScope
.
Execute
(
script
)
;
if
(
res
!=
null
)
{
this
.
WriteLine
(
res
.
ToString
(
)
)
;
}
}
catch
(
Exception
ex
)
{
this
.
WriteLine
(
ex
.
Message
)
;
}
}
}
this
.
TB
.
Text
+=
this
.
Prompt
;
this
.
TB
.
SelectionStart
=
this
.
TB
.
Text
.
Length
;
this
.
TB
.
ScrollToEnd
(
)
;
this
.
HistoryCommand
.
Append
(
rawCommand
)
;
this
.
UpdateTextBasePosition
(
)
;
}
private
void
_OnPreviewTextInput
(
object
sender
,
TextCompositionEventArgs
e
)
{
if
(
this
.
TB
.
SelectionStart
<
this
.
TextBasePosition
)
{
this
.
UpdateTextBasePosition
(
)
;
e
.
Handled
=
true
;
}
}
/// <summary>
/// 防止用户删除提示符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private
void
_OnPreviewKeyUp
(
object
sender
,
KeyEventArgs
e
)
{
int
index
=
this
.
TB
.
SelectionStart
;
if
(
e
.
Key
==
Key
.
Left
&&
index
<
this
.
TextBasePosition
)
{
this
.
TB
.
SelectionStart
=
this
.
TextBasePosition
;
this
.
TB
.
SelectionLength
=
0
;
e
.
Handled
=
true
;
}
if
(
e
.
Key
==
Key
.
Back
&&
index
<
this
.
TextBasePosition
)
{
//recover
this
.
TB
.
SelectionStart
=
0
;
this
.
TB
.
SelectionLength
=
this
.
TextBasePosition
-
1
;
this
.
TB
.
SelectedText
=
this
.
HistoryText
;
//set the cursor
this
.
TB
.
SelectionStart
=
this
.
TextBasePosition
;
this
.
TB
.
SelectionLength
=
0
;
e
.
Handled
=
true
;
}
}
private
void
_OnKeyUp
(
object
sender
,
KeyEventArgs
e
)
{
switch
(
e
.
Key
)
{
case
Key
.
Up
:
// Command history next
break
;
case
Key
.
Down
:
// Command history previous
break
;
case
Key
.
Escape
:
// Command history exit
break
;
default
:
break
;
}
}
}
public
class
TBTextWriter
:
TextWriter
{
public
TBTextWriter
(
REPLTextBox
tb
)
{
this
.
TB
=
tb
;
}
private
REPLTextBox
TB
;
public
override
Encoding
Encoding
{
get
{
return
Encoding
.
UTF8
;
}
}
public
override
void
Write
(
string
value
)
{
this
.
TB
.
Write
(
value
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL