original: https://github.com/sindresorhus/windows-terminal-size/blob/main/term-size.c
Created
April 23, 2025 12:07
-
-
Save aont/9302cdbc3d30a4a472985f3c02336181 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
# 定義 | |
# STD_OUTPUT_HANDLE = -11 | |
_GENERIC_READ = 0x80000000 | |
_CONSOLE_TEXTMODE_BUFFER = 1 | |
class _COORD(ctypes.Structure): | |
_fields_ = [("X", ctypes.c_short), | |
("Y", ctypes.c_short)] | |
class _SMALL_RECT(ctypes.Structure): | |
_fields_ = [("Left", ctypes.c_short), | |
("Top", ctypes.c_short), | |
("Right", ctypes.c_short), | |
("Bottom", ctypes.c_short)] | |
class _CONSOLE_SCREEN_BUFFER_INFO(ctypes.Structure): | |
_fields_ = [("dwSize", _COORD), | |
("dwCursorPosition", _COORD), | |
("wAttributes", ctypes.c_uint16), | |
("srWindow", _SMALL_RECT), | |
("dwMaximumWindowSize", _COORD)] | |
# 関数の準備 | |
_kernel32 = ctypes.windll.kernel32 | |
_CreateConsoleScreenBuffer = _kernel32.CreateConsoleScreenBuffer | |
_GetConsoleScreenBufferInfo = _kernel32.GetConsoleScreenBufferInfo | |
_CloseHandle = _kernel32.CloseHandle | |
def get_terminal_size(): | |
# 新しいコンソールバッファを作成 | |
tmpConsole = _CreateConsoleScreenBuffer(_GENERIC_READ, 0, None, _CONSOLE_TEXTMODE_BUFFER, None) | |
# コンソールのスクリーンバッファ情報を取得 | |
info = _CONSOLE_SCREEN_BUFFER_INFO() | |
_GetConsoleScreenBufferInfo(tmpConsole, ctypes.byref(info)) | |
# ハンドルを閉じる | |
_CloseHandle(tmpConsole) | |
# 最大ウィンドウサイズを取得 | |
columns = info.dwMaximumWindowSize.X | |
rows = info.dwMaximumWindowSize.Y | |
return columns, rows | |
# 結果を表示 | |
# print(columns) | |
# print(rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment