U
    7U                     @   sP   d dl mZ d dlZd dlZd dlZd dlZd dlZdd ZG dd deZ	dS )    )absolute_importNc              	   C   sj   t |  sdS ztdt| tjd}W n6   zt j	d t j	d f}W n   Y Y dS X Y nX |S )z
    Return a tuple (rows,cols) representing the size of the TTY `fd`.

    The provided file descriptor should be the stdout stream of the TTY.

    If the TTY size cannot be determined, returns None.
    NZhhZhhhhZLINESZCOLUMNS)
osisattyfilenostructZunpackfcntlZioctltermiosZ
TIOCGWINSZenviron)fdZdims r   //usr/lib/python3/dist-packages/dockerpty/tty.pysize   s    	r   c                   @   sJ   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )Terminalz
    Terminal provides wrapper functionality to temporarily make the tty raw.

    This is useful when streaming data from a pseudo-terminal into the tty.

    Example:

        with Terminal(sys.stdin, raw=True):
            do_things_in_raw_mode()
    Tc                 C   s   || _ || _d| _dS )a  
        Initialize a terminal for the tty with stdin attached to `fd`.

        Initializing the Terminal has no immediate side effects. The `start()`
        method must be invoked, or `with raw_terminal:` used before the
        terminal is affected.
        N)r
   raworiginal_attributes)selfr
   r   r   r   r   __init__=   s    	zTerminal.__init__c                 C   s   |    | S )z?
        Invoked when a `with` block is first entered.
        )startr   r   r   r   	__enter__K   s    zTerminal.__enter__c                 G   s   |    dS )z:
        Invoked when a `with` block is finished.
        N)stop)r   _r   r   r   __exit__T   s    zTerminal.__exit__c                 C   s   | j S )zE
        Returns True if the TTY should operate in raw mode.
        )r   r   r   r   r   israw\   s    zTerminal.israwc                 C   s6   t | j r2|  r2t| j| _t	| j dS )z}
        Saves the current terminal attributes and makes the tty raw.

        This method returns None immediately.
        N)
r   r   r
   r   r   r   Z	tcgetattrr   ttyZsetrawr   r   r   r   r   d   s    zTerminal.startc                 C   s"   | j dk	rt| jtj| j  dS )z
        Restores the terminal attributes back to before setting raw mode.

        If the raw terminal was not started, does nothing.
        N)r   r   Z	tcsetattrr
   Z	TCSADRAINr   r   r   r   r   p   s    
zTerminal.stopc                 C   s   dj t| j| j| jdS )Nz{cls}({fd}, raw={raw}))clsr
   r   )formattype__name__r
   r   r   r   r   r   __repr__~   s
    zTerminal.__repr__N)T)r   
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   1   s   
	r   )
Z
__future__r   r   r   r   r   r   r   objectr   r   r   r   r   <module>   s   