Skip to content

Commit 81c4dc4

Browse files
committed
Add docstrings to Windows console wrapper functions
1 parent ca3e966 commit 81c4dc4

File tree

1 file changed

+107
-2
lines changed

1 file changed

+107
-2
lines changed

rich/_win32_console.py

Lines changed: 107 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ class WindowsCoordinates(NamedTuple):
4040

4141
@classmethod
4242
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+
"""
4352
return COORD(value.col, value.row)
4453

4554

@@ -65,6 +74,14 @@ class CONSOLE_CURSOR_INFO(ctypes.Structure):
6574

6675

6776
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+
"""
6885
return cast(wintypes.HANDLE, _GetStdHandle(handle))
6986

7087

@@ -74,6 +91,20 @@ def GetStdHandle(handle: int = STDOUT) -> wintypes.HANDLE:
7491

7592

7693
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+
77108
console_mode = wintypes.DWORD()
78109
success = bool(_GetConsoleMode(std_handle, console_mode))
79110
if not success:
@@ -98,8 +129,17 @@ def FillConsoleOutputCharacter(
98129
length: int,
99130
start: WindowsCoordinates,
100131
) -> 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+
"""
103143
character = ctypes.c_char(char.encode())
104144
num_characters = wintypes.DWORD(length)
105145
num_written = wintypes.DWORD(0)
@@ -130,6 +170,18 @@ def FillConsoleOutputAttribute(
130170
length: int,
131171
start: WindowsCoordinates,
132172
) -> 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+
"""
133185
num_cells = wintypes.DWORD(length)
134186
style_attrs = wintypes.WORD(attributes)
135187
num_written = wintypes.DWORD(0)
@@ -150,6 +202,16 @@ def FillConsoleOutputAttribute(
150202
def SetConsoleTextAttribute(
151203
std_handle: wintypes.HANDLE, attributes: wintypes.WORD
152204
) -> 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+
"""
153215
return bool(_SetConsoleTextAttribute(std_handle, attributes))
154216

155217

@@ -164,6 +226,14 @@ def SetConsoleTextAttribute(
164226
def GetConsoleScreenBufferInfo(
165227
std_handle: wintypes.HANDLE,
166228
) -> 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."""
167237
console_screen_buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
168238
_GetConsoleScreenBufferInfo(std_handle, byref(console_screen_buffer_info))
169239
return console_screen_buffer_info
@@ -180,6 +250,15 @@ def GetConsoleScreenBufferInfo(
180250
def SetConsoleCursorPosition(
181251
std_handle: wintypes.HANDLE, coords: WindowsCoordinates
182252
) -> 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+
"""
183262
return bool(_SetConsoleCursorPosition(std_handle, coords))
184263

185264

@@ -194,6 +273,15 @@ def SetConsoleCursorPosition(
194273
def SetConsoleCursorInfo(
195274
std_handle: wintypes.HANDLE, cursor_info: CONSOLE_CURSOR_INFO
196275
) -> 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+
"""
197285
return bool(_SetConsoleCursorInfo(std_handle, byref(cursor_info)))
198286

199287

@@ -203,6 +291,14 @@ def SetConsoleCursorInfo(
203291

204292

205293
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+
"""
206302
return bool(_SetConsoleTitle(title))
207303

208304

@@ -218,6 +314,15 @@ def SetConsoleTitle(title: str) -> bool:
218314

219315

220316
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+
"""
221326
buffer = wintypes.LPWSTR(text)
222327
num_chars_written = wintypes.LPDWORD()
223328
return bool(

0 commit comments

Comments
 (0)