Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion of Segments to Windows Console API calls on legacy Windows platform #1993

Merged
merged 27 commits into from
Mar 9, 2022
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1a11ea3
Conversion of Segments to legacy Windows API calls
darrenburns Mar 1, 2022
fe57132
Add None windll definition for mypy
darrenburns Mar 1, 2022
ffc5813
Attempting to appease mypy on non-Windows platforms
darrenburns Mar 1, 2022
fb227b8
Use type of Any for windll
darrenburns Mar 1, 2022
e384106
On legacy Windows, we sometimes still need ANSI output...
darrenburns Mar 1, 2022
78fc8d9
Only calling Windows legacy console API if Console.file is stdout
darrenburns Mar 1, 2022
1d38b45
Handling utf-8 error in check_buffer consistently across Windows & Unix
darrenburns Mar 1, 2022
6779e51
Don't default to outputting ANSI just because Console.record=True
darrenburns Mar 1, 2022
bb9ab3c
Merge branch 'master' of https://github.com/Textualize/rich into ansi…
darrenburns Mar 1, 2022
501dcbd
Add tests for LegacyWindowsTerm
darrenburns Mar 1, 2022
97d05f5
Fixing test module on non-Windows platforms
darrenburns Mar 1, 2022
7d08e0a
Fixing test module on non-Windows platforms
darrenburns Mar 1, 2022
151276b
Run legacy Windows tests on Windows only
darrenburns Mar 2, 2022
862416a
Fix typing issues
darrenburns Mar 2, 2022
7525377
Use Python 3.6 & 3.7 compatible means of acceessing mock call args/kw…
darrenburns Mar 2, 2022
80912a5
Update CHANGELOG.md
darrenburns Mar 2, 2022
28786c7
Merge branch 'ansi-to-win32' of https://github.com/Textualize/rich in…
darrenburns Mar 2, 2022
ceef724
Use Windows Console API to write text
darrenburns Mar 3, 2022
1122501
Use WriteConsoleW instead of file.write(...) in LegacyWindowsTerm
darrenburns Mar 3, 2022
d87498b
Use bitwise operators in LegacyWindowsTerm, fix formatting
darrenburns Mar 3, 2022
5a051f9
Make GetConsoleMode Windows Console wrapper more Pythonic
darrenburns Mar 3, 2022
8f738c7
Support reverse, bold (bright), and dim
darrenburns Mar 3, 2022
ca3e966
Handle legacy windows error
darrenburns Mar 3, 2022
81c4dc4
Add docstrings to Windows console wrapper functions
darrenburns Mar 3, 2022
9b76da2
Merge pull request #2019 from Textualize/ansi-to-win32_pull-request-f…
darrenburns Mar 7, 2022
8fe170a
Merge branch 'master' into ansi-to-win32
willmcgugan Mar 9, 2022
91e0146
check win32
willmcgugan Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add docstrings to Windows console wrapper functions
darrenburns committed Mar 3, 2022

Verified

This commit was signed with the committer’s verified signature.
darrenburns Darren Burns
commit 81c4dc411523f15d61c999262271d3feb3c2b25e
109 changes: 107 additions & 2 deletions rich/_win32_console.py
Original file line number Diff line number Diff line change
@@ -40,6 +40,15 @@ class WindowsCoordinates(NamedTuple):

@classmethod
def from_param(cls, value: "WindowsCoordinates") -> COORD:
"""Converts a WindowsCoordinates into a wintypes _COORD structure.
This classmethod is internally called by ctypes to perform the conversion.
Args:
value (WindowsCoordinates): The input coordinates to convert.
Returns:
wintypes._COORD: The converted coordinates struct.
"""
return COORD(value.col, value.row)


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


def GetStdHandle(handle: int = STDOUT) -> wintypes.HANDLE:
"""Retrieves a handle to the specified standard device (standard input, standard output, or standard error).
Args:
handle (int): Integer identifier for the handle. Defaults to -11 (stdout).
Returns:
wintypes.HANDLE: The handle
"""
return cast(wintypes.HANDLE, _GetStdHandle(handle))


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


def GetConsoleMode(std_handle: wintypes.HANDLE) -> int:
"""Retrieves the current input mode of a console's input buffer
or the current output mode of a console screen buffer.
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
Raises:
LegacyWindowsError: If any error occurs while calling the Windows console API.
Returns:
int: Value representing the current console mode as documented at
https://docs.microsoft.com/en-us/windows/console/getconsolemode#parameters
"""

console_mode = wintypes.DWORD()
success = bool(_GetConsoleMode(std_handle, console_mode))
if not success:
@@ -98,8 +129,17 @@ def FillConsoleOutputCharacter(
length: int,
start: WindowsCoordinates,
) -> int:
"""Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates."""
assert len(char) == 1
"""Writes a character to the console screen buffer a specified number of times, beginning at the specified coordinates.
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
char (str): The character to write. Must be a string of length 1.
length (int): The number of times to write the character.
start (WindowsCoordinates): The coordinates to start writing at.
Returns:
int: The number of characters written.
"""
character = ctypes.c_char(char.encode())
num_characters = wintypes.DWORD(length)
num_written = wintypes.DWORD(0)
@@ -130,6 +170,18 @@ def FillConsoleOutputAttribute(
length: int,
start: WindowsCoordinates,
) -> int:
"""Sets the character attributes for a specified number of character cells,
beginning at the specified coordinates in a screen buffer.
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
attributes (int): Integer value representing the foreground and background colours of the cells.
length (int): The number of cells to set the output attribute of.
start (WindowsCoordinates): The coordinates of the first cell whose attributes are to be set.
Returns:
int: The number of cells whose attributes were actually set.
"""
num_cells = wintypes.DWORD(length)
style_attrs = wintypes.WORD(attributes)
num_written = wintypes.DWORD(0)
@@ -150,6 +202,16 @@ def FillConsoleOutputAttribute(
def SetConsoleTextAttribute(
std_handle: wintypes.HANDLE, attributes: wintypes.WORD
) -> bool:
"""Set the colour attributes for all text written after this function is called.
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
attributes (int): Integer value representing the foreground and background colours.
Returns:
bool: True if the attribute was set successfully, otherwise False.
"""
return bool(_SetConsoleTextAttribute(std_handle, attributes))


@@ -164,6 +226,14 @@ def SetConsoleTextAttribute(
def GetConsoleScreenBufferInfo(
std_handle: wintypes.HANDLE,
) -> CONSOLE_SCREEN_BUFFER_INFO:
"""Retrieves information about the specified console screen buffer.
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
Returns:
CONSOLE_SCREEN_BUFFER_INFO: A CONSOLE_SCREEN_BUFFER_INFO ctype struct contain information about
screen size, cursor position, colour attributes, and more."""
console_screen_buffer_info = CONSOLE_SCREEN_BUFFER_INFO()
_GetConsoleScreenBufferInfo(std_handle, byref(console_screen_buffer_info))
return console_screen_buffer_info
@@ -180,6 +250,15 @@ def GetConsoleScreenBufferInfo(
def SetConsoleCursorPosition(
std_handle: wintypes.HANDLE, coords: WindowsCoordinates
) -> bool:
"""Set the position of the cursor in the console screen
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
coords (WindowsCoordinates): The coordinates to move the cursor to.
Returns:
bool: True if the function succeeds, otherwise False.
"""
return bool(_SetConsoleCursorPosition(std_handle, coords))


@@ -194,6 +273,15 @@ def SetConsoleCursorPosition(
def SetConsoleCursorInfo(
std_handle: wintypes.HANDLE, cursor_info: CONSOLE_CURSOR_INFO
) -> bool:
"""Set the cursor info - used for adjusting cursor visibility and width
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
cursor_info (CONSOLE_CURSOR_INFO): CONSOLE_CURSOR_INFO ctype struct containing the new cursor info.
Returns:
bool: True if the function succeeds, otherwise False.
"""
return bool(_SetConsoleCursorInfo(std_handle, byref(cursor_info)))


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


def SetConsoleTitle(title: str) -> bool:
"""Sets the title of the current console window
Args:
title (str): The new title of the console window.
Returns:
bool: True if the function succeeds, otherwise False.
"""
return bool(_SetConsoleTitle(title))


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


def WriteConsole(std_handle: wintypes.HANDLE, text: str) -> bool:
"""Write a string of text to the console, starting at the current cursor position
Args:
std_handle (wintypes.HANDLE): A handle to the console input buffer or the console screen buffer.
text (str): The text to write.
Returns:
bool: True if the function succeeds, otherwise False.
"""
buffer = wintypes.LPWSTR(text)
num_chars_written = wintypes.LPDWORD()
return bool(