Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/colorama/win32.py: 12%

95 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1# Copyright Jonathan Hartley 2013. BSD 3-Clause license, see LICENSE file. 

2 

3# from winbase.h 

4STDOUT = -11 

5STDERR = -12 

6 

7ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004 

8 

9try: 

10 import ctypes 

11 from ctypes import LibraryLoader 

12 windll = LibraryLoader(ctypes.WinDLL) 

13 from ctypes import wintypes 

14except (AttributeError, ImportError): 

15 windll = None 

16 SetConsoleTextAttribute = lambda *_: None 

17 winapi_test = lambda *_: None 

18else: 

19 from ctypes import byref, Structure, c_char, POINTER 

20 

21 COORD = wintypes._COORD 

22 

23 class CONSOLE_SCREEN_BUFFER_INFO(Structure): 

24 """struct in wincon.h.""" 

25 _fields_ = [ 

26 ("dwSize", COORD), 

27 ("dwCursorPosition", COORD), 

28 ("wAttributes", wintypes.WORD), 

29 ("srWindow", wintypes.SMALL_RECT), 

30 ("dwMaximumWindowSize", COORD), 

31 ] 

32 def __str__(self): 

33 return '(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d)' % ( 

34 self.dwSize.Y, self.dwSize.X 

35 , self.dwCursorPosition.Y, self.dwCursorPosition.X 

36 , self.wAttributes 

37 , self.srWindow.Top, self.srWindow.Left, self.srWindow.Bottom, self.srWindow.Right 

38 , self.dwMaximumWindowSize.Y, self.dwMaximumWindowSize.X 

39 ) 

40 

41 _GetStdHandle = windll.kernel32.GetStdHandle 

42 _GetStdHandle.argtypes = [ 

43 wintypes.DWORD, 

44 ] 

45 _GetStdHandle.restype = wintypes.HANDLE 

46 

47 _GetConsoleScreenBufferInfo = windll.kernel32.GetConsoleScreenBufferInfo 

48 _GetConsoleScreenBufferInfo.argtypes = [ 

49 wintypes.HANDLE, 

50 POINTER(CONSOLE_SCREEN_BUFFER_INFO), 

51 ] 

52 _GetConsoleScreenBufferInfo.restype = wintypes.BOOL 

53 

54 _SetConsoleTextAttribute = windll.kernel32.SetConsoleTextAttribute 

55 _SetConsoleTextAttribute.argtypes = [ 

56 wintypes.HANDLE, 

57 wintypes.WORD, 

58 ] 

59 _SetConsoleTextAttribute.restype = wintypes.BOOL 

60 

61 _SetConsoleCursorPosition = windll.kernel32.SetConsoleCursorPosition 

62 _SetConsoleCursorPosition.argtypes = [ 

63 wintypes.HANDLE, 

64 COORD, 

65 ] 

66 _SetConsoleCursorPosition.restype = wintypes.BOOL 

67 

68 _FillConsoleOutputCharacterA = windll.kernel32.FillConsoleOutputCharacterA 

69 _FillConsoleOutputCharacterA.argtypes = [ 

70 wintypes.HANDLE, 

71 c_char, 

72 wintypes.DWORD, 

73 COORD, 

74 POINTER(wintypes.DWORD), 

75 ] 

76 _FillConsoleOutputCharacterA.restype = wintypes.BOOL 

77 

78 _FillConsoleOutputAttribute = windll.kernel32.FillConsoleOutputAttribute 

79 _FillConsoleOutputAttribute.argtypes = [ 

80 wintypes.HANDLE, 

81 wintypes.WORD, 

82 wintypes.DWORD, 

83 COORD, 

84 POINTER(wintypes.DWORD), 

85 ] 

86 _FillConsoleOutputAttribute.restype = wintypes.BOOL 

87 

88 _SetConsoleTitleW = windll.kernel32.SetConsoleTitleW 

89 _SetConsoleTitleW.argtypes = [ 

90 wintypes.LPCWSTR 

91 ] 

92 _SetConsoleTitleW.restype = wintypes.BOOL 

93 

94 _GetConsoleMode = windll.kernel32.GetConsoleMode 

95 _GetConsoleMode.argtypes = [ 

96 wintypes.HANDLE, 

97 POINTER(wintypes.DWORD) 

98 ] 

99 _GetConsoleMode.restype = wintypes.BOOL 

100 

101 _SetConsoleMode = windll.kernel32.SetConsoleMode 

102 _SetConsoleMode.argtypes = [ 

103 wintypes.HANDLE, 

104 wintypes.DWORD 

105 ] 

106 _SetConsoleMode.restype = wintypes.BOOL 

107 

108 def _winapi_test(handle): 

109 csbi = CONSOLE_SCREEN_BUFFER_INFO() 

110 success = _GetConsoleScreenBufferInfo( 

111 handle, byref(csbi)) 

112 return bool(success) 

113 

114 def winapi_test(): 

115 return any(_winapi_test(h) for h in 

116 (_GetStdHandle(STDOUT), _GetStdHandle(STDERR))) 

117 

118 def GetConsoleScreenBufferInfo(stream_id=STDOUT): 

119 handle = _GetStdHandle(stream_id) 

120 csbi = CONSOLE_SCREEN_BUFFER_INFO() 

121 success = _GetConsoleScreenBufferInfo( 

122 handle, byref(csbi)) 

123 return csbi 

124 

125 def SetConsoleTextAttribute(stream_id, attrs): 

126 handle = _GetStdHandle(stream_id) 

127 return _SetConsoleTextAttribute(handle, attrs) 

128 

129 def SetConsoleCursorPosition(stream_id, position, adjust=True): 

130 position = COORD(*position) 

131 # If the position is out of range, do nothing. 

132 if position.Y <= 0 or position.X <= 0: 

133 return 

134 # Adjust for Windows' SetConsoleCursorPosition: 

135 # 1. being 0-based, while ANSI is 1-based. 

136 # 2. expecting (x,y), while ANSI uses (y,x). 

137 adjusted_position = COORD(position.Y - 1, position.X - 1) 

138 if adjust: 

139 # Adjust for viewport's scroll position 

140 sr = GetConsoleScreenBufferInfo(STDOUT).srWindow 

141 adjusted_position.Y += sr.Top 

142 adjusted_position.X += sr.Left 

143 # Resume normal processing 

144 handle = _GetStdHandle(stream_id) 

145 return _SetConsoleCursorPosition(handle, adjusted_position) 

146 

147 def FillConsoleOutputCharacter(stream_id, char, length, start): 

148 handle = _GetStdHandle(stream_id) 

149 char = c_char(char.encode()) 

150 length = wintypes.DWORD(length) 

151 num_written = wintypes.DWORD(0) 

152 # Note that this is hard-coded for ANSI (vs wide) bytes. 

153 success = _FillConsoleOutputCharacterA( 

154 handle, char, length, start, byref(num_written)) 

155 return num_written.value 

156 

157 def FillConsoleOutputAttribute(stream_id, attr, length, start): 

158 ''' FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten )''' 

159 handle = _GetStdHandle(stream_id) 

160 attribute = wintypes.WORD(attr) 

161 length = wintypes.DWORD(length) 

162 num_written = wintypes.DWORD(0) 

163 # Note that this is hard-coded for ANSI (vs wide) bytes. 

164 return _FillConsoleOutputAttribute( 

165 handle, attribute, length, start, byref(num_written)) 

166 

167 def SetConsoleTitle(title): 

168 return _SetConsoleTitleW(title) 

169 

170 def GetConsoleMode(handle): 

171 mode = wintypes.DWORD() 

172 success = _GetConsoleMode(handle, byref(mode)) 

173 if not success: 

174 raise ctypes.WinError() 

175 return mode.value 

176 

177 def SetConsoleMode(handle, mode): 

178 success = _SetConsoleMode(handle, mode) 

179 if not success: 

180 raise ctypes.WinError()