B
    't\c                 @   s$  d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZ dd
lmZmZmZ ddlmZ ddlmZ ddlm Z  ddl!Z!ddl"Z"ddgZ#dddZ$G dd de%Z&G dd de'Z(G dd de)Z*G dd de)Z+dddZ,dS )z_
Renders the command line on the console.
(Redraws parts of the input line that were changed.)
    )unicode_literals)FutureFromensure_futureget_event_loop)	to_filter)to_formatted_text)Input)MouseHandlers)PointScreenWritePosition)Output
ColorDepth)	BaseStyleDummyStyleTransformationStyleTransformation)
is_windows)deque)rangeNRendererprint_formatted_textFc                s  |
j |
j 
}g|j|j}|j|j|j|j|j |	  fdd	 	
fdd}fdd}|s	  |r|s|
  |s|r|
kr|tddd	  |  t }t|j|}tt|j|j|}d}x@t|D ]2}|j| }|j| }|j| }t
d	 |r<t| nd}t
d	 |r\t| nd}d}x||d	 k r|| }|| }|jpd	}|j|jks|j|jkr|t||d||kr|||  || tj| jd||7 }qhW | r||k  r|t|d	 |d	  |   qW ||jkrV|td|d	 d|rv|td|d|  n||| jj|s|s|  	  |js|r|  d fS )
a  
    Render the diff between this screen and the previous screen.

    This takes two `Screen` instances. The one that represents the output like
    it was during the last rendering and one that represents the current
    output raster. Looking at these two `Screen` instances, this function will
    render the difference by calling the appropriate methods of the `Output`
    object that only paint the changes to the terminal.

    This is some performance-critical code which is heavily optimized.
    Don't change things without profiling first.

    :param current_pos: Current cursor position.
    :param last_style: The style string, used for drawing the last drawn
        character.  (Color/attributes.)
    :param attrs_for_style_string: :class:`._StyleStringToAttrsCache` instance.
    :param width: The width of the terminal.
    :param previous_width: The width of the terminal during the last rendering.
    c                  s      dd< dS )z) Wrapper around Output.reset_attributes. Nr    r   )_output_reset_attributes
last_styler   6lib/python3.7/site-packages/prompt_toolkit/renderer.pyreset_attributesG   s    z-_output_screen_diff.<locals>.reset_attributesc                s   j j }}| j|krB  d| j|   d}| j  | S | j|k rZ|| j  |d krzd | j  n>| j |k s|d kr || j   n| j |kr| j |  | S )z; Move cursor to this `new` point. Returns the given Point. z
r      )xy)newZ	current_xZ	current_y)_output_cursor_backward_output_cursor_forward_output_cursor_upcurrent_posr   widthwriter   r   move_cursorL   s"    



z(_output_screen_diff.<locals>.move_cursorc                sZ   d }|| j kr| j n8| j  }|r8|| krB | | j | j d< dS )z5
        Write the output of this character.
        r   N)stylechar)r*   Zthe_last_styleZ	new_attrs)_output_set_attributesattrs_for_style_stringcolor_depthr   r'   r   r   output_charg   s    



z(_output_screen_diff.<locals>.output_charr   )r   r    r   ) columnsrowsr'   	write_rawset_attributesr   Zcursor_forward	cursor_upcursor_backwardZhide_cursorZdisable_autowrapr   
erase_downr   minheightmaxr   Zdata_bufferZzero_width_escapeskeysr&   r*   r)   r   r    Zerase_end_of_lineZget_cursor_positionlayoutZcurrent_windowenable_autowrapshow_cursor)appoutputscreenr%   r-   Zprevious_screenr   is_donefull_screenr,   sizeprevious_widthr7   r1   r(   r.   Zcurrent_heightZ	row_countcr    Znew_rowZprevious_rowZzero_width_escapes_rowZnew_max_line_lenZprevious_max_line_lenZnew_charZold_charZ
char_widthr   )r"   r#   r$   r   r+   r,   r-   r%   r   r   r&   r'   r   _output_screen_diff   sx    


  

rE   c               @   s   e Zd ZdZdS )HeightIsUnknownErrorz@ Information unavailable. Did not yet receive the CPR response. N)__name__
__module____qualname____doc__r   r   r   r   rF      s   rF   c               @   s    e Zd ZdZdd Zdd ZdS )_StyleStringToAttrsCachezk
    A cache structure that maps style strings to :class:`.Attr`.
    (This is an important speed up.)
    c             C   s*   t |stt|tst|| _|| _d S )N)callableAssertionError
isinstancer   get_attrs_for_style_strstyle_transformation)selfrO   rP   r   r   r   __init__   s    z!_StyleStringToAttrsCache.__init__c             C   s"   |  |}| j|}|| |< |S )N)rO   rP   Ztransform_attrs)rQ   	style_strattrsr   r   r   __missing__   s    
z$_StyleStringToAttrsCache.__missing__N)rG   rH   rI   rJ   rR   rU   r   r   r   r   rK      s   rK   c               @   s   e Zd ZdZdZdZdZdS )CPR_Supportz( Enum: whether or not CPR is supported. 	SUPPORTEDNOT_SUPPORTEDUNKNOWNN)rG   rH   rI   rJ   rW   rX   rY   r   r   r   r   rV      s   rV   c               @   s   e Zd ZdZdZdddZd dd	Zed
d Zedd Z	edd Z
dd Zdd Zedd Zd!ddZd"ddZd#ddZdd ZdS )$r   z
    Typical usage:

    ::

        output = Vt100_Output.from_pty(sys.stdout)
        r = Renderer(style, output)
        r.render(app, layout=...)
       FNc             C   s   t |tstt |tstt |ts*tt|s>|d ks>t|| _|| _|| _|| _	t
|| _|| _d| _d| _d| _t | _tj| _|jstj| _d | _d | _d | _d | _| jdd d S )NFT)_scroll)rN   r   rM   r   r	   rL   r)   r>   inputrA   r   mouse_supportcpr_not_supported_callback_in_alternate_screen_mouse_support_enabled_bracketed_paste_enabledr   _waiting_for_cpr_futuresrV   rY   cpr_supportZresponds_to_cprrX   _attrs_for_style_last_style_hash_last_transformation_hash_last_color_depthreset)rQ   r)   r>   r\   rA   r]   r^   r   r   r   rR     s,    
zRenderer.__init__Tc             C   s   t ddd| _d | _d | _d | _t | _d| _t rB|rB| j	
  | jr\|r\| j	  d| _| jrr| j	  d| _| jr| j	  d| _| j	  d S )Nr   )r   r    F)r   _cursor_pos_last_screen
_last_size_last_styler
   mouse_handlers_min_available_heightr   r>   Zscroll_buffer_to_promptr_   Zquit_alternate_screenr`   disable_mouse_supportra   Zdisable_bracketed_pasteflush)rQ   r[   leave_alternate_screenr   r   r   rh   .  s$    





zRenderer.resetc             C   s   | j S )zn
        The `Screen` class that was generated during the last rendering.
        This can be `None`.
        )rj   )rQ   r   r   r   last_rendered_screenX  s    zRenderer.last_rendered_screenc             C   s   | j p| jdkpt S )z
        True when the height from the cursor until the bottom of the terminal
        is known. (It's often nicer to draw bottom toolbars only if the height
        is known, in order to avoid flickering when the CPR response arrives.)
        r   )rA   rn   r   )rQ   r   r   r   height_is_known`  s    zRenderer.height_is_knownc             C   sN   | j r
dS | jdkrB| j j}| jr.| jjnd}|t| j| S tddS )zU
        Return the number of rows visible in the terminal above the layout.
        r   zRows above layout is unknown.N)	r_   rn   r>   get_sizer0   rj   r7   r8   rF   )rQ   
total_rowsZlast_screen_heightr   r   r   rows_above_layoutj  s    
zRenderer.rows_above_layoutc                s    j jdkst jr& j j _nt r: j	  _nn j
tjkrJdS  fdd} j
tjkrj|  n> j
tjkr js|   fdd}tj|d}d|_|  dS )	aS  
        Get current cursor position.

        We do this to calculate the minimum available height that we can
        consume for rendering the prompt. This is the available space below te
        cursor.

        For vt100: Do CPR request. (answer will arrive later.)
        For win32: Do API call. (Answer comes immediately.)
        r   Nc                  s    j t   j  d S )N)rb   appendr   r>   Zask_for_cprr   )rQ   r   r   do_cpr  s    z9Renderer.request_absolute_cursor_position.<locals>.do_cprc                  s8   t  j  jtjkr4tj _ jr4t 	 j d S )N)
timesleepCPR_TIMEOUTrc   rV   rY   rX   r^   r   Zcall_from_executorr   )rQ   r   r   timer  s
    z8Renderer.request_absolute_cursor_position.<locals>.timer)targetT)ri   r    rM   rA   r>   rt   r0   rn   r   Zget_rows_below_cursor_positionrc   rV   rX   rW   rY   waiting_for_cpr	threadingThreaddaemonstart)rQ   rx   r|   tr   )rQ   r    request_absolute_cursor_positionx  s     z)Renderer.request_absolute_cursor_positionc             C   sX   t j| _| j j}|| d }|| _y| j }W n t	k
rH   Y nX |
d dS )z
        To be called when we know the absolute cursor position.
        (As an answer of a "Cursor Position Request" response.)
        r   N)rV   rW   rc   r>   rt   r0   rn   rb   popleft
IndexError
set_result)rQ   rowru   Zrows_below_cursorfr   r   r   report_absolute_cursor_row  s    z#Renderer.report_absolute_cursor_rowc             C   s
   t | jS )zi
        Waiting for CPR flag. True when we send the request, but didn't got a
        response.
        )boolrb   )rQ   r   r   r   r~     s    zRenderer.waiting_for_cprr   c                sp   t j  rjtjkr$tdS t  fdd}t|  fdd}tj	|d}d|_
|  S )z*
        Wait for a CPR response.
        Nc              3   s.   x D ]} t | V  qW  s*d  d S )N)r   doner   )Z
response_f)cpr_futuresr   r   r   wait_for_responses  s    
z;Renderer.wait_for_cpr_responses.<locals>.wait_for_responsesc                  s(   t    s$t _ d  d S )N)ry   rz   r   r   rb   r   r   )r   rQ   timeoutr   r   wait_for_timeout  s    
z9Renderer.wait_for_cpr_responses.<locals>.wait_for_timeout)r}   T)listrb   rc   rV   rX   r   Zsucceedr   r   r   r   r   )rQ   r   r   r   r   r   )r   r   rQ   r   r   wait_for_cpr_responses  s    


zRenderer.wait_for_cpr_responsesc             C   s6  | j }| jr | js d| _|  | js6| j   d| _|  }|rX| jsX|  d| _n|sp| jrp|	  d| _|
 }t }d|_t }| jr|j}	nJ|r|j|j|jj}	n0| jr| jjnd}
t| j|
|j|j|jj}	t|	|j}	| j|krd| _| j | jks0|j | jks0|j| jkr<d| _d| _ | j dkrZt!| jj"|j| _ | j | _|j | _|j| _|jj#||t$dd|j|	ddddd |%  |j&r|'|j& t(|||| j)|j| j| j*|| j| j || jr| jjndd\| _)| _*|| _|| _|| _+|,  |j-|j._-|r2| /  dS )	z
        Render the current interface to the output.

        :param is_done: When True, put the cursor at the end of the interface. We
                won't print any changes to this part.
        TFr   N)ZxposZyposr&   r7    )Zparent_styleZerase_bgZz_index)rA   r,   rB   rC   )0r>   rA   r_   Zenter_alternate_screenra   Zenable_bracketed_paster]   r`   Zenable_mouse_supportro   rt   r   r<   r
   r0   Z	containerZpreferred_heightr/   Z	preferredrj   r7   r8   rn   r6   rk   r)   Zinvalidation_hashre   rP   rf   r-   rg   rd   rK   rO   Zwrite_to_screenr   Zdraw_all_floatsZ
exit_styleZappend_style_to_contentrE   ri   rl   rm   rp   Zvisible_windowsr:   rh   )rQ   r=   r:   r@   r>   Zneeds_mouse_supportrB   r?   rm   r7   Zlast_heightr   r   r   render  s~    





 
zRenderer.renderc             C   sR   | j }|| jj || jj |  |  |  |	  | j
|d dS )aR  
        Hide all output and put the cursor back at the first line. This is for
        instance used for running a system command (while hiding the CLI) and
        later resuming the same CLI.)

        :param leave_alternate_screen: When True, and when inside an alternate
            screen buffer, quit the alternate screen.
        )rq   N)r>   r4   ri   r   r3   r    r5   r   r;   rp   rh   )rQ   rq   r>   r   r   r   erase[  s    	zRenderer.erasec             C   s6   |    | j}|  |dd |  |   dS )z,
        Clear screen and go to 0,0
        r   N)r   r>   Zerase_screenZcursor_gotorp   r   )rQ   r>   r   r   r   clearo  s    zRenderer.clear)FFN)FT)r   )F)T)rG   rH   rI   rJ   r{   rR   rh   propertyrr   rs   rv   r   r   r~   r   r   r   r   r   r   r   r   r     s   	 
 
*
9
#
h
c       
      C   s   t | tstt |tst|dks2t |ts2t|dksH|tjksHtt|}|pXt }|pdt	 }| 
  |   t|j|}xP|D ]H\}}|| }	|	r| |	| n| 
  d|kst| |dd qW | 
  |   dS )zT
    Print a list of (style_str, text) tuples in the given style to the output.
    Nr   
z
)rN   r   rM   r   r   r   Z_ALLr   r   defaultr   r;   rK   rO   r2   r'   replacerp   )
r>   Zformatted_textr)   rP   r-   Z	fragmentsr,   rS   textrT   r   r   r   r     s*    
)NNFFNNr   )NN)-rJ   Z
__future__r   Zprompt_toolkit.eventloopr   r   r   r   Zprompt_toolkit.filtersr   Zprompt_toolkit.formatted_textr   Zprompt_toolkit.input.baser	   Z$prompt_toolkit.layout.mouse_handlersr
   Zprompt_toolkit.layout.screenr   r   r   Zprompt_toolkit.outputr   r   Zprompt_toolkit.stylesr   r   r   Zprompt_toolkit.utilsr   collectionsr   Z	six.movesr   ry   r   __all__rE   	ExceptionrF   dictrK   objectrV   r   r   r   r   r   r   <module>   s:     
 D    