o
    bĎi                     @   sZ   d Z ddlZddlZddlZdZdZdZejdkZdZ	dZ
d	d
 ZdddZdddZdS )z9Get info about the current terminal window/screen buffer.    N   O   win32iic           	      C   sf   |t krtdtd}| ||st td|jdd \}}}}|| || }}||fS )a  Get information about this current console window (Windows only).

    https://github.com/Robpol86/colorclass/blob/ab42da59/colorclass/windows.py#L111

    :raise OSError: When handle is invalid or GetConsoleScreenBufferInfo API call fails.

    :param ctypes.windll.kernel32 kernel32: Loaded kernel32 instance.
    :param int handle: stderr or stdout handle.

    :return: Width (number of characters) and height (number of lines) of the terminal.
    :rtype: tuple
    zInvalid handle.   hhhhHhhhhhh   )	INVALID_HANDLE_VALUEOSErrorctypescreate_string_bufferGetConsoleScreenBufferInfoWinErrorstructunpackraw)	kernel32handlelpcsbilefttoprightbottomwidthheight r   V/home/jeff/fluffinator/venv/lib/python3.10/site-packages/terminaltables/terminal_io.pyget_console_info   s   
r   c                 C   s   t r4| ptjj} z	t| | tW S  ty3   zt| | tW  Y S  ty2   t	t
f Y  Y S w w ztddtdjd}W n tyO   t	t
f Y S w td|dd \}}||fS )a  Get the width and height of the terminal.

    http://code.activestate.com/recipes/440694-determine-size-of-console-window-on-windows/
    http://stackoverflow.com/questions/17993814/why-the-irrelevant-code-made-a-difference

    :param kernel32: Optional mock kernel32 object. For testing.

    :return: Width (number of characters) and height (number of lines) of the terminal.
    :rtype: tuple
    fcntlr   termiosz        hhhhN   )
IS_WINDOWSr   windllr   r   GetStdHandleSTD_ERROR_HANDLEr   STD_OUTPUT_HANDLEDEFAULT_WIDTHDEFAULT_HEIGHT
__import__ioctl
TIOCGWINSZIOErrorr   r   )r   devicer   r   r   r   r   terminal_size*   s$   r/   c                 C   s   z|  d}W n ty   | }Y nw trI|ptjj}ztdd | D }W n ty8   tdd | D }Y nw |rB||dkS |	| dkS t
jd| d  dS )	a  Set the terminal title.

    :param title: The title to set (string, unicode, bytes accepted).
    :param kernel32: Optional mock kernel32 object. For testing.

    :return: If title changed successfully (Windows only, always True on Linux/OSX).
    :rtype: bool
    zutf-8c                 s   s    | ]	}t |d k V  qdS    N)ord.0cr   r   r   	<genexpr>X   s    z%set_terminal_title.<locals>.<genexpr>c                 s   s    | ]}|d k V  qdS r0   r   r3   r   r   r   r6   Z   s    r   s   ]0;   T)encodeAttributeErrorr#   r   r$   r   all	TypeErrorSetConsoleTitleASetConsoleTitleWsysstdoutwrite)titler   title_bytesis_asciir   r   r   set_terminal_titleG   s"   	rD   )N)__doc__r   r   r>   r)   r(   r
   platformr#   r&   r'   r   r/   rD   r   r   r   r   <module>   s    

