U
    fw%Z  ã                   @   s$   d dl Z d dlZG dd„ deƒZdS )é    Nc                   @   sn   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dd„Zdd„ Zddd„Zdd„ ZdS )ÚMessageran  Manages messages to the terminal.

    This includes disabling messages, allowing notifications, and
    becalming the flow of messages to avoid writing too fast. The
    speed is a performance thing: writing too much message text can
    slow an application down a lot (too much work for terminal
    emulators), and doesn't actually help the user in any way.

    Nc                 C   sŽ   |d krdn|| _ d| _d | _d | _d| _|p4t ¡ | _z| j ¡  W n t	k
r`   d| _Y nX | j 
¡ srd| _t ¡ | _| j | j¡ d S )Ng      ð?Tr   F)Ú_periodÚenabledÚ_cached_messageÚ_displayed_messageÚ_previous_write_atÚ	ttystatusZPhysicalTerminalÚ	_terminalZopen_ttyÚIOErrorZhas_capabilitiesZAreaManagerÚ_areaZset_terminal)ÚselfZperiodr	   © r   ú4/usr/lib/python3/dist-packages/ttystatus/messager.pyÚ__init__"   s    

zMessager.__init__c                 C   s
   d| _ dS )z(Disable all output except notifications.FN©r   ©r   r   r   r   Údisable7   s    zMessager.disablec                 C   s
   d| _ dS )zEnable output to happen.TNr   r   r   r   r   Úenable;   s    zMessager.enablec                 C   s   |   ¡ | j | jkS )zIs it time to write now?)Ú_nowr   r   r   r   r   r   Útime_to_write?   s    zMessager.time_to_writec                 C   s   t   ¡ S )zReturn current time.)Útimer   r   r   r   r   C   s    zMessager._nowc                 C   s
   | j  ¡ S )zReturn terminal width, height.)r	   Zget_sizer   r   r   r   Úget_terminal_sizeH   s    zMessager.get_terminal_sizec                 C   s
   | j  ¡ S )N)r   Úget_max_line_lengthr   r   r   r   r   L   s    zMessager.get_max_line_lengthFc                 C   sj   | j r`|s|  ¡ r`| jdk	r*| j | j¡ t| d¡ƒ}| j |¡ | j |¡ || _|  	¡ | _
|| _dS )zLWrite message to terminal.

        Message may be multiple lines.

        NÚ
)r   r   r   r   Zprepare_to_overwriteÚlenÚsplitZ
make_spaceZdisplayr   r   r   )r   ÚmessageÚforceÚ	num_linesr   r   r   ÚwriteO   s    

zMessager.writec                 C   s<   | j dk	r8t| j  d¡ƒ}| j |¡ d| _ d| _d| _dS )z9Remove currently displayed message from terminal, if any.Nr   r   )r   r   r   r   Z
clear_arear   r   )r   r   r   r   r   Úclear`   s    
zMessager.clearc                 C   sb   | j s
|r^|  ¡  z | |¡ | d¡ | ¡  W n tk
rF   Y nX | jdk	r^|  | j¡ dS )a­  Show a notification message string to the user.

        Notifications are meant for error messages and other things
        that do not belong in, say, progress bars. Whatever is currently
        on the terminal is wiped, then the notification message is shown,
        a new line is started, and the old message is restored.

        Notifications are written even when the output is not going
        to a terminal.

        r   N)r   r    r   Úflushr
   r   )r   r   Úfr   r   r   r   Únotifyj   s    



zMessager.notifyc                 C   s2   | j r.| jdk	r.|  | j¡ | jr.| j d¡ dS )zFinalize output.Nr   )r   r   r   r	   r   r   r   r   Úfinishƒ   s    zMessager.finish)NN)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r    r#   r$   r   r   r   r   r      s   




r   )r   r   Úobjectr   r   r   r   r   Ú<module>   s   