6
6
import re
7
7
from . import subprocess_repl
8
8
9
- # PowerShell in interactive mode shows no prompt, so we must hold it by hand.
10
- # Every command prepended with other command, which will output only one character ('.')
11
- # When user command leads to no output (for example, 'cd ..'), we get only this character,
12
- # and then we send command to show prompt explicitly.
13
- # No output at all means, that PowerShell needs more input (multiline mode).
14
- # In this case we proceeds sending user input without modifications.
15
-
16
9
class PowershellRepl (subprocess_repl .SubprocessRepl ):
17
10
TYPE = "powershell"
18
- PREPENDER = b"."
19
11
20
12
def __init__ (self , encoding , ** kwds ):
21
13
if not encoding :
@@ -26,42 +18,18 @@ def __init__(self, encoding, **kwds):
26
18
raise LookupError ("Can't detect encoding from chcp" )
27
19
encoding = "cp" + chcp_encoding .groups ()[0 ]
28
20
print (encoding )
29
-
30
21
super (PowershellRepl , self ).__init__ (encoding , ** kwds )
31
22
32
- # Using this to detect whether PowerShell returns some output or it needs more input
33
- # PowerShell in interactive mode doesn't show prompt, so we must hold it by hand
34
- # It's a hack and, for example, we can send 'Write-Host "" -NoNewLine' with no output, but in outhr cases it may work well
35
- self .got_output = True
36
- self .multiline = False
37
-
38
23
def read_bytes (self ):
39
24
# this is windows specific problem, that you cannot tell if there
40
25
# are more bytes ready, so we read only 1 at a times
41
26
42
27
result = super (PowershellRepl , self ).read_bytes ()
43
-
44
- # Consumes output (it must be equal to PREPENDER)
45
- if result and not self .got_output :
46
- self .got_output = True
47
- self .multiline = False
48
- # Don't return PREPENDER, read another input
49
- return self .read_bytes ()
50
-
51
28
return result
52
29
53
30
def write_bytes (self , bytes ):
54
31
# Drop flag on new input
55
- self .got_output = False
56
- if not self .multiline :
57
- # Turn multiline mode on, it will be turned off, when PowerShell returns some output
58
- self .multiline = True
59
- self .prepend ()
60
32
self .do_write (bytes )
61
33
62
34
def do_write (self , bytes ):
63
35
super (PowershellRepl , self ).write_bytes (bytes )
64
-
65
- def prepend (self ):
66
- """ Command to prepend every output with special mark to detect multiline mode """
67
- self .do_write (b'Write-Host "' + PowershellRepl .PREPENDER + b'" -NoNewLine; ' )
0 commit comments