@@ -40,6 +40,15 @@ class WindowsCoordinates(NamedTuple):
40
40
41
41
@classmethod
42
42
def from_param (cls , value : "WindowsCoordinates" ) -> COORD :
43
+ """Converts a WindowsCoordinates into a wintypes _COORD structure.
44
+ This classmethod is internally called by ctypes to perform the conversion.
45
+
46
+ Args:
47
+ value (WindowsCoordinates): The input coordinates to convert.
48
+
49
+ Returns:
50
+ wintypes._COORD: The converted coordinates struct.
51
+ """
43
52
return COORD (value .col , value .row )
44
53
45
54
@@ -65,6 +74,14 @@ class CONSOLE_CURSOR_INFO(ctypes.Structure):
65
74
66
75
67
76
def GetStdHandle (handle : int = STDOUT ) -> wintypes .HANDLE :
77
+ """Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
78
+
79
+ Args:
80
+ handle (int): Integer identifier for the handle. Defaults to -11 (stdout).
81
+
82
+ Returns:
83
+ wintypes.HANDLE: The handle
84
+ """
68
85
return cast (wintypes .HANDLE , _GetStdHandle (handle ))
69
86
70
87
@@ -74,6 +91,20 @@ def GetStdHandle(handle: int = STDOUT) -> wintypes.HANDLE:
74
91
75
92
76
93
def GetConsoleMode (std_handle : wintypes .HANDLE ) -> int :
94
+ """Retrieves the current input mode of a console's input buffer
95
+ or the current output mode of a console screen buffer.
96
+
97
+ Args:
98
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
99
+
100
+ Raises:
101
+ LegacyWindowsError: If any error occurs while calling the Windows console API.
102
+
103
+ Returns:
104
+ int: Value representing the current console mode as documented at
105
+ https://docs.microsoft.com/en-us/windows/console/getconsolemode#parameters
106
+ """
107
+
77
108
console_mode = wintypes .DWORD ()
78
109
success = bool (_GetConsoleMode (std_handle , console_mode ))
79
110
if not success :
@@ -98,8 +129,17 @@ def FillConsoleOutputCharacter(
98
129
length : int ,
99
130
start : WindowsCoordinates ,
100
131
) -> int :
101
- """Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates."""
102
- assert len (char ) == 1
132
+ """Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.
133
+
134
+ Args:
135
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
136
+ char (str): The character to write. Must be a string of length 1.
137
+ length (int): The number of times to write the character.
138
+ start (WindowsCoordinates): The coordinates to start writing at.
139
+
140
+ Returns:
141
+ int: The number of characters written.
142
+ """
103
143
character = ctypes .c_char (char .encode ())
104
144
num_characters = wintypes .DWORD (length )
105
145
num_written = wintypes .DWORD (0 )
@@ -130,6 +170,18 @@ def FillConsoleOutputAttribute(
130
170
length : int ,
131
171
start : WindowsCoordinates ,
132
172
) -> int :
173
+ """Sets the character attributes for a specified number of character cells,
174
+ beginning at the specified coordinates in a screen buffer.
175
+
176
+ Args:
177
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
178
+ attributes (int): Integer value representing the foreground and background colours of the cells.
179
+ length (int): The number of cells to set the output attribute of.
180
+ start (WindowsCoordinates): The coordinates of the first cell whose attributes are to be set.
181
+
182
+ Returns:
183
+ int: The number of cells whose attributes were actually set.
184
+ """
133
185
num_cells = wintypes .DWORD (length )
134
186
style_attrs = wintypes .WORD (attributes )
135
187
num_written = wintypes .DWORD (0 )
@@ -150,6 +202,16 @@ def FillConsoleOutputAttribute(
150
202
def SetConsoleTextAttribute (
151
203
std_handle : wintypes .HANDLE , attributes : wintypes .WORD
152
204
) -> bool :
205
+ """Set the colour attributes for all text written after this function is called.
206
+
207
+ Args:
208
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
209
+ attributes (int): Integer value representing the foreground and background colours.
210
+
211
+
212
+ Returns:
213
+ bool: True if the attribute was set successfully, otherwise False.
214
+ """
153
215
return bool (_SetConsoleTextAttribute (std_handle , attributes ))
154
216
155
217
@@ -164,6 +226,14 @@ def SetConsoleTextAttribute(
164
226
def GetConsoleScreenBufferInfo (
165
227
std_handle : wintypes .HANDLE ,
166
228
) -> CONSOLE_SCREEN_BUFFER_INFO :
229
+ """Retrieves information about the specified console screen buffer.
230
+
231
+ Args:
232
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
233
+
234
+ Returns:
235
+ CONSOLE_SCREEN_BUFFER_INFO: A CONSOLE_SCREEN_BUFFER_INFO ctype struct contain information about
236
+ screen size, cursor position, colour attributes, and more."""
167
237
console_screen_buffer_info = CONSOLE_SCREEN_BUFFER_INFO ()
168
238
_GetConsoleScreenBufferInfo (std_handle , byref (console_screen_buffer_info ))
169
239
return console_screen_buffer_info
@@ -180,6 +250,15 @@ def GetConsoleScreenBufferInfo(
180
250
def SetConsoleCursorPosition (
181
251
std_handle : wintypes .HANDLE , coords : WindowsCoordinates
182
252
) -> bool :
253
+ """Set the position of the cursor in the console screen
254
+
255
+ Args:
256
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
257
+ coords (WindowsCoordinates): The coordinates to move the cursor to.
258
+
259
+ Returns:
260
+ bool: True if the function succeeds, otherwise False.
261
+ """
183
262
return bool (_SetConsoleCursorPosition (std_handle , coords ))
184
263
185
264
@@ -194,6 +273,15 @@ def SetConsoleCursorPosition(
194
273
def SetConsoleCursorInfo (
195
274
std_handle : wintypes .HANDLE , cursor_info : CONSOLE_CURSOR_INFO
196
275
) -> bool :
276
+ """Set the cursor info - used for adjusting cursor visibility and width
277
+
278
+ Args:
279
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
280
+ cursor_info (CONSOLE_CURSOR_INFO): CONSOLE_CURSOR_INFO ctype struct containing the new cursor info.
281
+
282
+ Returns:
283
+ bool: True if the function succeeds, otherwise False.
284
+ """
197
285
return bool (_SetConsoleCursorInfo (std_handle , byref (cursor_info )))
198
286
199
287
@@ -203,6 +291,14 @@ def SetConsoleCursorInfo(
203
291
204
292
205
293
def SetConsoleTitle (title : str ) -> bool :
294
+ """Sets the title of the current console window
295
+
296
+ Args:
297
+ title (str): The new title of the console window.
298
+
299
+ Returns:
300
+ bool: True if the function succeeds, otherwise False.
301
+ """
206
302
return bool (_SetConsoleTitle (title ))
207
303
208
304
@@ -218,6 +314,15 @@ def SetConsoleTitle(title: str) -> bool:
218
314
219
315
220
316
def WriteConsole (std_handle : wintypes .HANDLE , text : str ) -> bool :
317
+ """Write a string of text to the console, starting at the current cursor position
318
+
319
+ Args:
320
+ std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
321
+ text (str): The text to write.
322
+
323
+ Returns:
324
+ bool: True if the function succeeds, otherwise False.
325
+ """
221
326
buffer = wintypes .LPWSTR (text )
222
327
num_chars_written = wintypes .LPDWORD ()
223
328
return bool (
0 commit comments