U
    fw%Z                     @   s$   d dl Z d dlZG dd deZdS )    Nc                   @   s   e Zd ZdZd2ddZdd Zdd Zd	d
 Zdd Zdd Z	e
dd Zdd Zdd Zd3ddZdd Zdd Zdd Zdd Zdd  Zd!d" Zd4d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdS )5TerminalStatusa  Show status and progress information on a terminal.

    All output is provided via widgets of various kinds. Many widgets
    format data that TerminalStatus stores. TerminalStatus provides a
    dict interface for setting and retrieving data items. Unlike a real
    dict, getting a value for a key that has not been set does not
    result in a KeyError exception, but in the empty string being
    returned.

    Nc                 C   s    |pt j||d| _|   d S )N)period	_terminal)	ttystatusZMessager_mclear)selfr   Zmessagerr    r	   2/usr/lib/python3/dist-packages/ttystatus/status.py__init__#   s
     zTerminalStatus.__init__c                 C   s
   | j  S )zReturn terminal width, height.)r   get_terminal_sizer   r	   r	   r
   r   (   s    z TerminalStatus.get_terminal_sizec                 C   s4   | j sg g| _ | j d | | | |   dS )z'Add a new widget to the status display.N)_widget_rowsappend_register_interestsflush)r   widgetr	   r	   r
   add,   s
    
zTerminalStatus.addc                 C   sP   t |dd d kr| j| n.|jD ]&}| j|g }|| || j|< q$d S )Ninterested_in)getattr_unknown_interestr   r   _interested_inget)r   r   keywidgetsr	   r	   r
   r   4   s    

z"TerminalStatus._register_interestsc                 C   s&   | j sg g| _ | j g  |   dS )zStart a new line of widgets.N)r   r   r   r   r	   r	   r
   start_new_line=   s    zTerminalStatus.start_new_linec                 C   sN   t |dD ]2\}}|dkr&|   t|D ]}| | q0q|   dS )a)  Add new widgets based on format string.

        The format string is taken literally, except that ``%%`` is a
        literal percent character, and ``%Foo(a,b,c)`` is a widget
        of type ``Foo`` with parameters a, b, and c. For example:
        ``format("hello, %String(name)")``.

        
r   N)	enumeratesplitr   r   parser   r   )r   format_stringiliner   r	   r	   r
   formatD   s    
zTerminalStatus.formatc                 C   s   g }| j D ]}||7 }q
|S N)r   )r   resultrowr	   r	   r
   r   U   s    

zTerminalStatus.widgetsc                 C   s&   g | _ i | _i | _g | _| j  dS )zRemove all widgets.N)r   _valuesr   r   r   r   r   r	   r	   r
   r   \   s
    zTerminalStatus.clearc                 C   s   | j |dS )z*Return value for key, or the empty string. r(   r   )r   r   r	   r	   r
   __getitem__d   s    zTerminalStatus.__getitem__c                 C   s   | j ||S )zLike dict.get.r*   )r   r   defaultr	   r	   r
   r   h   s    zTerminalStatus.getc                 C   sZ   || j |< | j|g | jg}|D ]}|D ]}||  q*q"| jjrV| j rV|   dS )zSet value for key.N)	r(   r   r   r   updater   enabledZtime_to_write_write)r   r   valueZwidget_listsr   wr	   r	   r
   __setitem__l   s    
zTerminalStatus.__setitem__c                 C   s   | j   dS )aJ  Hide current progress report.

        Use .flush() to make it visible again. Hiding is useful if you
        want to write things to stdout/stderr that might get mixed
        with progress output. The .notify() and .error() methods get
        disabled if progress reporting gets disabled, but .hide()
        doesn't.

        N)r   r   r   r	   r	   r
   hidey   s    zTerminalStatus.hidec                 C   s   | j dd dS )zForce an update of current state to the screen.

        This happens even if it is not yet time to output the screen.

        TforceN)r/   r   r	   r	   r
   r      s    zTerminalStatus.flushc                    s   d  fdd jD S )z$Render current state of all widgets.r   c                 3   s   | ]}  |V  qd S r%   )_render_row).0r'   r   r	   r
   	<genexpr>   s     z)TerminalStatus._render.<locals>.<genexpr>)joinr   r   r	   r   r
   _render   s    zTerminalStatus._renderc                 C   s   | j  }|}d gt| }t|D ]2\}}|jr$| |d||< |t|| 8 }q$t|D ]2\}}|js`| ||||< |t|| 8 }q`d|d | S )Nr   r)   )r   Zget_max_line_lengthlenr   Zstatic_width
_make_safeZrenderr9   )r   Z
widget_rowZ	max_charsZ	remainingZtextsr"   r1   r	   r	   r
   r6      s    
zTerminalStatus._render_rowc                    s    d d  fdd| D S )z7Expand TABs, remove all other ASCII control characters.    r)   c                 3   s"   | ]}t | kr|nd V  qdS )r)   N)ord)r7   cZASCII_SPACEr	   r
   r8      s   z,TerminalStatus._make_safe.<locals>.<genexpr>)r9   
expandtabs)r   r#   r	   r@   r
   r<      s    zTerminalStatus._make_safeFc                 C   s   | j j|  |d dS )z/Render and output current state of all widgets.r4   N)r   writer:   )r   r5   r	   r	   r
   r/      s    zTerminalStatus._writec                 C   s   | | p
d| | |< dS )z+Increase value for a key by a given amount.r   Nr	   )r   r   Zdeltar	   r	   r
   increase   s    zTerminalStatus.increasec                 C   s   | j |tj dS )zShow a message.N)r   notifysysstdoutr   msgr	   r	   r
   rD      s    zTerminalStatus.notifyc                 C   s   | j j|tjdd dS )zWrite an error message.Tr4   N)r   rD   rE   stderrrG   r	   r	   r
   error   s    zTerminalStatus.errorc                 C   s   |    | j  dS )zFinish status display.N)r/   r   finishr   r	   r	   r
   rK      s    zTerminalStatus.finishc                 C   s   | j   dS )zDisable all output.N)r   disabler   r	   r	   r
   rL      s    zTerminalStatus.disablec                 C   s   | j   dS )z&Enable output if it has been disabled.N)r   enabler   r	   r	   r
   rM      s    zTerminalStatus.enable)NNN)N)F)__name__
__module____qualname____doc__r   r   r   r   r   r$   propertyr   r   r+   r   r2   r3   r   r:   r6   r<   r/   rC   rD   rJ   rK   rL   rM   r	   r	   r	   r
   r      s2   
	



r   )rE   r   objectr   r	   r	   r	   r
   <module>   s   