-
Notifications
You must be signed in to change notification settings - Fork 1
/
ansipipe.py
77 lines (64 loc) · 2.21 KB
/
ansipipe.py
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
"""
ANSI|pipe Python connection module
Redirect standard I/O through ANSI|pipe executable
to enable UTF-8, ANSI escape sequences and dual mode CLI/GUI executables
when packaging Python applications to a Windows executable.
based on DualModeI.cpp from dualsybsystem.
Python version (c) 2015 Rob Hagemans
Licensed under the Expat MIT licence.
See LICENSE.md or http://opensource.org/licenses/mit-license.php
"""
import os
import sys
import platform
if platform.system() == 'Windows':
pid = os.getpid()
# construct named pipe names
name_out = '\\\\.\\pipe\\ANSIPIPE_%d_POUT' % pid
name_in = '\\\\.\\pipe\\ANSIPIPE_%d_PIN' % pid
name_err = '\\\\.\\pipe\\ANSIPIPE_%d_PERR' % pid
# attach named pipes to stdin/stdout/stderr
try:
sys.stdout = open(name_out, 'wb', 0)
sys.stdin = open(name_in, 'rb', 0)
sys.stderr = open(name_err, 'wb', 0)
ok = True;
except EnvironmentError:
sys.stdout = sys.__stdout__
sys.stdin = sys.__stdin__
sys.stderr = sys.__stderr__
ok = False;
# minimal replacements for tty.setraw() and termios.tcsa
# using ansipipe-only escape sequences
ONLCR = 4
ECHO = 8
ICRNL = 256
TCSADRAIN = 1
termios_state = ICRNL | ECHO
if ok:
def setraw(fd, dummy=None):
""" Set raw terminal mode (Windows stub). """
tcsetattr(fd, dummy, 0)
def tcsetattr(fd, dummy, attr):
""" Set terminal attributes (Windows stub). """
if (fd == sys.stdin.fileno()):
num = 254
sys.stdout.write('\x1b]%d;ECHO\x07' % (num + (attr & ECHO != 0)))
sys.stdout.write('\x1b]%d;ICRNL\x07' % (num + (attr & ICRNL != 0)))
sys.stdout.write('\x1b]%d;ONLCR\x07' % (num + (attr & ONLCR != 0)))
termios_state = attr
def tcgetattr(fd):
""" Get terminal attributes (Windows stub). """
if (fd == sys.stdin.fileno()):
return termios_state
else:
return 0
else:
def setraw(fd, dummy=None):
pass
def tcsetattr(fd, dummy, attr):
pass
def tcgetattr(fd):
return 0
else:
ok = True;