U
    fw%ZŸ  ã                   @   s8   d dl Z e jdkrdd„ Zndd„ ZG dd„ deƒZdS )é    N)é   c                 C   s   |   ¡ S ©N)Úencode©Ús© r   ú0/usr/lib/python3/dist-packages/ttystatus/area.pyÚxfrm   s    r	   c                 C   s   | S r   r   r   r   r   r   r	      s    c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚAreaManagerz8Manage the area on the terminal for displaying messages.c                 C   s
   d | _ d S r   ©Ú	_terminal)Úselfr   r   r   Ú__init__    s    zAreaManager.__init__c                 C   s
   || _ d S r   r   )r   Zterminalr   r   r   Úset_terminal#   s    zAreaManager.set_terminalc                 C   s   | j  ¡ \}}|d S )Né   )r   Zget_size)r   ÚwidthÚ_r   r   r   Úget_max_line_length&   s    zAreaManager.get_max_line_lengthc                 C   s>   t | d¡ƒ}| j ¡ }| j ¡ }| j ||d  | ¡ dS )aC  Prepare to overwrite currently displayed message.

        This is like clear, but it only moves the cursor to the top of
        the message, but does not clear all the lines. This is helpful
        for avoiding flickering.

        The cursor is assumed to be on the last line of the displayed
        message.

        Ú
r   N)ÚlenÚsplitr   Úget_up_sequenceÚget_carriage_return_sequenceÚwrite)r   Zdisplayed_messageÚ	num_linesÚupÚcrr   r   r   Úprepare_to_overwrite*   s    

z AreaManager.prepare_to_overwritec                 C   s   | j  d|d  ¡ dS )a0  Make space for a message needing a given number of lines.

        If the cursor is near the bottom of the terminal, scroll
        things up so that there's space. Otherwise, effectively
        nothing happens, except that the cursor is left at the last
        line reserved for the message.

        r   r   N)r   r   )r   r   r   r   r   Ú
make_space;   s    
zAreaManager.make_spacec                 C   s´   |   ¡ }| j ¡ }| j ¡ }| j ¡ }| j ¡ }| d¡}|t|ƒd  g}t|ƒD ]D\}	}
|	dkrn| 	|¡ | 	|¡ | 	t
|
d|… ƒ¡ | 	|¡ qTd |¡}| j |¡ dS )z½Display a message, which may be on multiple lines.

        The cursor is assumed to be at the last line of the message
        area. Long lines are chopped at terminal width - 1.

        r   r   r   Nó    )r   r   r   Zget_down_sequencer   Úget_erase_line_sequencer   r   Ú	enumerateÚappendr	   Újoinr   )r   ÚmessageZ	max_charsr   Zdownr   ÚeraseÚlinesÚpartsÚiÚlineÚoutputr   r   r   ÚdisplayG   s    







zAreaManager.displayc                 C   sF   | j  ¡ }| j  ¡ }| j  ¡ }| j  || | |d  | | ¡ dS )z´Clear area reserved for message needing a given number of lines.

        The cursor is assumed to be at the last line of the message
        area and is left at the top.

        r   N)r   r   r   r    r   )r   r   r   r   r%   r   r   r   Ú
clear_areaa   s    


zAreaManager.clear_areaN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r+   r,   r   r   r   r   r
      s   r
   )ÚsysÚversion_infor	   Úobjectr
   r   r   r   r   Ú<module>   s   

