a
    lc                    @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZmZmZ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mZmZmZ dd
lmZmZm Z  ddl!m"Z"m#Z# ddl$m%Z% ddl&m'Z'm(Z( ddl)m*Z*m+Z+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZA ddlBmCZC e	r~ddlDmEZEmFZF ddlGmHZH g dZIG dd dedZJe	rG dd deEZKeeJdf ZLddd d!ZMG d"d# d#eZNG d$d% d%eZOG d&d' d'eJZPG d(d) d)ePZQG d*d+ d+ePZRG d,d- d-eJZSG d.d/ d/ZTG d0d1 d1ZUG d2d3 d3ZVG d4d5 d5ZWeeB ZXG d6d7 d7eZYG d8d deJZZG d9d: d:eJZ[G d;d< d<eJZ\eLeJd=d>d?Z]eLeZd=d@dAZ^e_dBdCdDdEZ`dFS )Gza
Container for the layout.
(Containers can contain other containers or user interface controls.)
    )ABCMetaabstractmethod)Enum)partial)	TYPE_CHECKINGCallableDictListOptionalSequenceTupleUnioncast)get_app)SimpleCache)Point)FilterOrBoolemacs_insert_mode	to_filtervi_insert_mode)AnyFormattedTextStyleAndTextTuplesto_formatted_text)fragment_list_to_textfragment_list_width)KeyBindingsBase)
MouseEventMouseEventType)
get_cwidthtake_using_weightsto_intto_str   )DummyControlFormattedTextControlGetLinePrefixCallable	UIContent	UIControl)AnyDimension	Dimensionmax_layout_dimensionssum_layout_dimensionsto_dimension)Margin)MouseHandlers)_CHAR_CACHEScreenWritePosition)explode_text_fragments)Protocol	TypeGuard)NotImplementedOrNone)AnyContainer	ContainerHorizontalAlignVerticalAlignHSplitVSplitFloatContainerFloatWindowAlignWindowWindowRenderInfoConditionalContainerScrollOffsetsColorColumnto_container	to_windowis_containerDynamicContainerc                	   @   s   e Zd ZdZeddddZeeedddZeeeed	d
dZ	ee
eeeeee ddddZedddZee dddZeed  dddZdS )r7   z/
    Base class for user interface layout.
    Nreturnc                 C   s   dS )zu
        Reset the state of this container and all the children.
        (E.g. reset scroll offsets, etc...)
        N selfrJ   rJ   \/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/layout/containers.pyreset^   s    zContainer.resetmax_available_widthrI   c                 C   s   dS )z
        Return a :class:`~prompt_toolkit.layout.Dimension` that represents the
        desired width for this container.
        NrJ   rL   rP   rJ   rJ   rM   preferred_widthe   s    zContainer.preferred_widthwidthmax_available_heightrI   c                 C   s   dS )z
        Return a :class:`~prompt_toolkit.layout.Dimension` that represents the
        desired height for this container.
        NrJ   rL   rT   rU   rJ   rJ   rM   preferred_heightl   s    zContainer.preferred_heightscreenmouse_handlerswrite_positionparent_styleerase_bgz_indexrI   c                 C   s   dS )a=  
        Write the actual content to the screen.

        :param screen: :class:`~prompt_toolkit.layout.screen.Screen`
        :param mouse_handlers: :class:`~prompt_toolkit.layout.mouse_handlers.MouseHandlers`.
        :param parent_style: Style string to pass to the :class:`.Window`
            object. This will be applied to all content of the windows.
            :class:`.VSplit` and :class:`.HSplit` can use it to pass their
            style down to the windows that they contain.
        :param z_index: Used for propagating z_index from parent to child.
        NrJ   rL   rY   rZ   r[   r\   r]   r^   rJ   rJ   rM   write_to_screens   s    
zContainer.write_to_screenc                 C   s   dS )z
        When this container is modal, key bindings from parent containers are
        not taken into account if a user control in this container is focused.
        FrJ   rK   rJ   rJ   rM   is_modal   s    zContainer.is_modalc                 C   s   dS )z
        Returns a :class:`.KeyBindings` object. These bindings become active when any
        user control in this container has the focus, except if any containers
        between this container and the focused user control is modal.
        NrJ   rK   rJ   rJ   rM   get_key_bindings   s    zContainer.get_key_bindingsc                 C   s   g S )zG
        Return the list of child :class:`.Container` objects.
        rJ   rK   rJ   rJ   rM   get_children   s    zContainer.get_children)__name__
__module____qualname____doc__r   rN   intr)   rR   rW   r0   r.   r1   strboolr
   r`   ra   r   rb   r	   rc   rJ   rJ   rJ   rM   r7   Y   s(   r7   )	metaclassc                   @   s   e Zd ZdZddddZdS )MagicContainerzY
        Any object that implements ``__pt_container__`` represents a container.
        r6   rH   c                 C   s   d S NrJ   rK   rJ   rJ   rM   __pt_container__   s    zMagicContainer.__pt_container__N)rd   re   rf   rg   rn   rJ   rJ   rJ   rM   rl      s   rl   r?   rH   c                   C   s   t tdgdS )z<Create a `Window` that displays the 'Window too small' text.)zclass:window-too-smallz Window too small... )text)r?   r$   rJ   rJ   rJ   rM   _window_too_small   s    
rp   c                   @   s    e Zd ZdZdZdZdZdZdS )r9   zAlignment for `HSplit`.TOPCENTERBOTTOMJUSTIFYN)rd   re   rf   rg   rq   rr   rs   rt   rJ   rJ   rJ   rM   r9      s
   r9   c                   @   s    e Zd ZdZdZdZdZdZdS )r8   zAlignment for `VSplit`.LEFTrr   RIGHTrt   N)rd   re   rf   rg   ru   rr   rv   rt   rJ   rJ   rJ   rM   r8      s
   r8   c                   @   s   e Zd ZdZdedddddddddf
ee ee	 e
ee ee
e
ee eee eeeg ef f ddddZed	d
dZee d	ddZee	 d	ddZdS )_Splitz4
    The common parts of `VSplit` and `HSplit`.
    Nr    F)childrenwindow_too_smallpaddingpadding_charpadding_stylerT   heightr^   modalkey_bindingsstylerI   c                 C   sV   dd |D | _ |pt | _|| _|| _|| _|| _|| _|| _|	| _	|
| _
|| _d S )Nc                 S   s   g | ]}t |qS rJ   )rD   .0crJ   rJ   rM   
<listcomp>       z#_Split.__init__.<locals>.<listcomp>)ry   rp   rz   r{   r|   r}   rT   r~   r^   r   r   r   )rL   ry   rz   r{   r|   r}   rT   r~   r^   r   r   r   rJ   rJ   rM   __init__   s    z_Split.__init__rH   c                 C   s   | j S rm   r   rK   rJ   rJ   rM   ra      s    z_Split.is_modalc                 C   s   | j S rm   r   rK   rJ   rJ   rM   rb      s    z_Split.get_key_bindingsc                 C   s   | j S rm   )ry   rK   rJ   rJ   rM   rc      s    z_Split.get_children)rd   re   rf   rg   r)   exactr   r6   r
   r7   r(   ri   rh   rj   r   r   r   r   ra   rb   r	   rc   rJ   rJ   rJ   rM   rw      s8   rw   c                       s   e Zd ZdZdejdddddddddfee ee	 ee
ee ee
e
ee eee eeeg ef f dd fddZeed	d
dZeeedddZddddZeee	 dddZeeeeeee ddddZeeee  dddZ  ZS )r:   a  
    Several layouts, one stacked above/under the other. ::

        +--------------------+
        |                    |
        +--------------------+
        |                    |
        +--------------------+

    By default, this doesn't display a horizontal line between the children,
    but if this is something you need, then create a HSplit as follows::

        HSplit(children=[ ... ], padding_char='-',
               padding=1, padding_style='#ffff00')

    :param children: List of child :class:`.Container` objects.
    :param window_too_small: A :class:`.Container` object that is displayed if
        there is not enough space for all the children. By default, this is a
        "Window too small" message.
    :param align: `VerticalAlign` value.
    :param width: When given, use this width instead of looking at the children.
    :param height: When given, use this height instead of looking at the children.
    :param z_index: (int or None) When specified, this can be used to bring
        element in front of floating elements.  `None` means: inherit from parent.
    :param style: A style string.
    :param modal: ``True`` or ``False``.
    :param key_bindings: ``None`` or a :class:`.KeyBindings` object.

    :param padding: (`Dimension` or int), size to be used for the padding.
    :param padding_char: Character to be used for filling in the padding.
    :param padding_style: Style to applied to the padding.
    Nr   rx   Fry   rz   alignr{   r|   r}   rT   r~   r^   r   r   r   rI   c                    s@   t  j||||||||	|
||d || _tdd| _t | _d S N)ry   rz   r{   r|   r}   rT   r~   r^   r   r   r   r"   maxsizesuperr   r   r   _children_cacher?   _remaining_space_windowrL   ry   rz   r   r{   r|   r}   rT   r~   r^   r   r   r   	__class__rJ   rM   r     s"    zHSplit.__init__rO   c                    s@   | j d urt| j S | jr6 fdd| jD }t|S t S d S )Nc                    s   g | ]}|  qS rJ   rR   r   rP   rJ   rM   r   =  r   z*HSplit.preferred_width.<locals>.<listcomp>)rT   r,   ry   r*   r)   rL   rP   
dimensionsrJ   r   rM   rR   8  s    

zHSplit.preferred_widthrS   c                    s2   | j d urt| j S  fdd| jD }t|S )Nc                    s   g | ]}|  qS rJ   rW   r   rU   rT   rJ   rM   r   F  s   z+HSplit.preferred_height.<locals>.<listcomp>)r~   r,   _all_childrenr+   )rL   rT   rU   r   rJ   r   rM   rW   B  s    

zHSplit.preferred_heightrH   c                 C   s   | j D ]}|  qd S rm   ry   rN   rL   r   rJ   rJ   rM   rN   K  s    
zHSplit.resetc                    s*   t t d fdd} jt j|S );
        List of child objects, including padding.
        rH   c                     s   g }  j tjtjfv r,| ttddd  jD ](}| | | t j j	 j
d q2| rh|    j tjtjfv r| ttddd | S )Nr   	preferredrT   )r~   charr   )r   r9   rr   rs   appendr?   r)   ry   r{   r|   r}   poprq   resultchildrK   rJ   rM   getU  s"    

z!HSplit._all_children.<locals>.getr	   r7   r   r   tuplery   rL   r   rJ   rK   rM   r   O  s    zHSplit._all_childrenrX   c              
   C   s   |  |}|d t| j }| jdu r*|n| j}|du rP| j|||||| n|j}	|j}
|j}t	|| j
D ].\}}|||t|
|	||||| |	|7 }	qn|j|j |	 }|dkr| j||t|
|	||||| dS )
        Render the prompt to a `Screen` instance.

        :param screen: The :class:`~prompt_toolkit.layout.screen.Screen` class
            to which the output has to be written.
         Nr   )_divide_heightsr!   r   r^   rz   r`   yposxposrT   zipr   r1   r~   r   )rL   rY   rZ   r[   r\   r]   r^   sizesr   r   r   rT   sr   Zremaining_heightrJ   rJ   rM   r`   q  s<    

zHSplit.write_to_screen)r[   rI   c                    s,  | j s
g S |j|j  fdd| jD }t|}|j krBdS dd |D }tttt	|dd |D d}t
|}t |j}dd |D }t||k r|| || k r||  d7  < t
|}qt js(t |j}	d	d |D }
t||	k r(|| |
| k r||  d7  < t
|}q|S )
zb
        Return the heights for all rows.
        Or None when there is not enough space.
        c                    s   g | ]}|  qS rJ   r   r   r~   rT   rJ   rM   r     r   z*HSplit._divide_heights.<locals>.<listcomp>Nc                 S   s   g | ]
}|j qS rJ   minr   drJ   rJ   rM   r     r   c                 S   s   g | ]
}|j qS rJ   weightr   rJ   rJ   rM   r     r   itemsweightsc                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rM   r     r   r"   c                 S   s   g | ]
}|j qS rJ   maxr   rJ   rJ   rM   r     r   )ry   rT   r~   r   r+   r   r   listrangelennextr   sumr   is_doner   )rL   r[   r   sum_dimensionsr   child_generatoripreferred_stoppreferred_dimensionsmax_stopmax_dimensionsrJ   r   rM   r     s6    



zHSplit._divide_heights)rd   re   rf   rg   r9   rt   r   r6   r
   r7   r(   ri   rh   rj   r   r   r   r   r)   rR   rW   rN   propertyr	   r   r0   r.   r1   r`   r   __classcell__rJ   rJ   r   rM   r:      sR   $%
	#9r:   c                       s   e Zd ZdZdejdddddddddfee ee	 ee
ee ee
e
ee eee eeeg ef f dd fddZeed	d
dZeeedddZddddZeee	 dddZeeee  dddZeeeeeee ddddZ  ZS )r;   a  
    Several layouts, one stacked left/right of the other. ::

        +---------+----------+
        |         |          |
        |         |          |
        +---------+----------+

    By default, this doesn't display a vertical line between the children, but
    if this is something you need, then create a HSplit as follows::

        VSplit(children=[ ... ], padding_char='|',
               padding=1, padding_style='#ffff00')

    :param children: List of child :class:`.Container` objects.
    :param window_too_small: A :class:`.Container` object that is displayed if
        there is not enough space for all the children. By default, this is a
        "Window too small" message.
    :param align: `HorizontalAlign` value.
    :param width: When given, use this width instead of looking at the children.
    :param height: When given, use this height instead of looking at the children.
    :param z_index: (int or None) When specified, this can be used to bring
        element in front of floating elements.  `None` means: inherit from parent.
    :param style: A style string.
    :param modal: ``True`` or ``False``.
    :param key_bindings: ``None`` or a :class:`.KeyBindings` object.

    :param padding: (`Dimension` or int), size to be used for the padding.
    :param padding_char: Character to be used for filling in the padding.
    :param padding_style: Style to applied to the padding.
    Nr   rx   Fr   c                    s@   t  j||||||||	|
||d || _tdd| _t | _d S r   r   r   r   rJ   rM   r     s"    zVSplit.__init__rO   c                    s0   | j d urt| j S  fdd| jD }t|S )Nc                    s   g | ]}|  qS rJ   r   r   r   rJ   rM   r   *  s   z*VSplit.preferred_width.<locals>.<listcomp>)rT   r,   r   r+   r   rJ   r   rM   rR   &  s    


zVSplit.preferred_widthrS   c                    sV   | j d urt| j S | |}| j}|d u r2t S  fddt||D }t|S d S )Nc                    s   g | ]\}}| | qS rJ   r   )r   r   r   rU   rJ   rM   r   B  s   z+VSplit.preferred_height.<locals>.<listcomp>)r~   r,   _divide_widthsr   r)   r   r*   )rL   rT   rU   r   ry   r   rJ   r   rM   rW   0  s    




zVSplit.preferred_heightrH   c                 C   s   | j D ]}|  qd S rm   r   r   rJ   rJ   rM   rN   H  s    
zVSplit.resetc                    s*   t t d fdd} jt j|S )r   rH   c                     s   g }  j tjtjfv r,| ttddd  jD ](}| | | t j j	 j
d q2| rh|    j tjtjfv r| ttddd | S )Nr   r   r   )rT   r   r   )r   r8   rr   rv   r   r?   r)   ry   r{   r|   r}   r   ru   r   rK   rJ   rM   r   R  s"    

z!VSplit._all_children.<locals>.getr   r   rJ   rK   rM   r   L  s    zVSplit._all_children)rT   rI   c                    s  | j }|sg S  fdd|D }dd |D }t|}|j krDdS dd |D }tttt|dd |D d}t|}t |j}	t	||	k r|| || k r||  d7  < t|}qd	d |D }
t |j
}t	||k r|| |
| k r||  d7  < t|}q|S )
zd
        Return the widths for all columns.
        Or None when there is not enough space.
        c                    s   g | ]}|  qS rJ   r   r   r   rJ   rM   r   y  r   z)VSplit._divide_widths.<locals>.<listcomp>c                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rM   r   z  r   Nc                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rM   r     r   c                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rM   r     r   r   r"   c                 S   s   g | ]
}|j qS rJ   r   r   rJ   rJ   rM   r     r   )r   r+   r   r   r   r   r   r   r   r   r   )rL   rT   ry   r   r   r   r   r   r   r   r   r   rJ   r   rM   r   n  s2    


zVSplit._divide_widthsrX   c              
      s  | j s
dS | j}|  j}|d t| j }	| jdu r<|n| j}|du rd| j|| |	|| dS  fddt	||D }
t
 jt jt
|
} j} j}t	||D ].\}}|||t|||||	|| ||7 }q j j | }|dkr| j||t|||||	|| dS )r   Nr   c                    s    g | ]\}}| | jjqS rJ   )rW   r~   r   )r   rT   r   r[   rJ   rM   r     s   z*VSplit.write_to_screen.<locals>.<listcomp>r   )ry   r   r   rT   r!   r   r^   rz   r`   r   r   r~   r   r   r   r1   r   )rL   rY   rZ   r[   r\   r]   r^   ry   r   r   Zheightsr~   r   r   r   r   Zremaining_widthrJ   r   rM   r`     sJ    


zVSplit.write_to_screen)rd   re   rf   rg   r8   rt   r   r6   r
   r7   r(   ri   rh   rj   r   r   r   r   r)   rR   rW   rN   r   r	   r   r   r0   r.   r1   r`   r   rJ   rJ   r   rM   r;     sR   #%
!5r;   c                
   @   s   e Zd ZdZd!eed eee e	e
eg e
f f ee ddddZdd	d
dZeedddZeeedddZeeee
eee ddddZdeeee
eee ddddZeeedddZed	ddZee d	ddZee d	dd ZdS )"r<   a  
    Container which can contain another container for the background, as well
    as a list of floating containers on top of it.

    Example Usage::

        FloatContainer(content=Window(...),
                       floats=[
                           Float(xcursor=True,
                                ycursor=True,
                                content=CompletionsMenu(...))
                       ])

    :param z_index: (int or None) When specified, this can be used to bring
        element in front of floating elements.  `None` means: inherit from parent.
        This is the z_index for the whole `Float` container as a whole.
    FNrx   r=   )contentfloatsr   r   r   r^   rI   c                 C   s,   t || _|| _|| _|| _|| _|| _d S rm   )rD   r   r   r   r   r   r^   )rL   r   r   r   r   r   r^   rJ   rJ   rM   r     s    

zFloatContainer.__init__rH   c                 C   s$   | j   | jD ]}|j   qd S rm   )r   rN   r   )rL   frJ   rJ   rM   rN     s    

zFloatContainer.resetrO   c                 C   s   | j |S rm   )r   rR   rQ   rJ   rJ   rM   rR     s    zFloatContainer.preferred_widthrS   c                 C   s   | j ||S )z
        Return the preferred height of the float container.
        (We don't care about the height of the floats, they should always fit
        into the dimensions provided by the container.)
        )r   rW   rV   rJ   rJ   rM   rW     s    zFloatContainer.preferred_heightrX   c                 C   s   |d t | j }| jd u r |n| j}| j|||||| t| jD ]\}}	|pTd|	j }
|d t | j }|	jd up|	jd u}|r|d }
|j	|
t
| j|	||||||
d qF| |	||||||
 qFd S )Nr   r   i r^   	draw_func)r!   r   r^   r   r`   	enumerater   xcursorycursordraw_with_z_indexr   _draw_float)rL   rY   rZ   r[   r\   r]   r^   r   numberflZnew_z_indexZpostponerJ   rJ   rM   r`     sD    	zFloatContainer.write_to_screen)r   rY   rZ   r[   r   r]   r^   rI   c                 C   s  | |jpt jj}t|j|j |j|j	 d}	|
 }
| }|jdurb|
durb|j}|
}n@|jdur|jdur|j}|j|j |j }n|
dur|jdur|j|j |
 }|
}n|jr|
du r|j|jj}t|j|}n|
}|	j}|| |jkrtd|j| }n|
r2t|j|
 d }|
}np|j|jj}|jdurV|j}n<|jdurztd|j| |j }ntdt|j| d }t||j| }|jdur|dur|j}|}nz|jdur|jdur|j}|j|j |j }nF|dur(|jdur(|j| |j }|}n|jr|	j|jr@dnd }|du rd|j||jj}n|}||j| kr@|j| d |kr|j| }nt||	j}|	j| }n|rt|j| d }|}nr|j||jj}|jdur|j}n<|jdurtd|j| |j }ntdt|j| d }t||j| }|dkr|dkrt||j ||j	 ||d}|jr| ||r|jj|||||   |d dS )zDraw a single Float.xyNr      r"   r   r   rT   r~   )r]   r^   )!Zget_menu_positionattach_to_windowr   layoutZcurrent_windowr   r   r   r   r   	get_width
get_heightleftrightrT   r   r   rR   r   r   r   rh   topbottomr~   r   allow_cover_cursorrW   r1   hide_when_covering_content_area_is_emptyr`   transparent)rL   r   rY   rZ   r[   r   r]   r^   cposcursor_positionZfl_widthZ	fl_heightr   rT   r   r~   wprJ   rJ   rM   r   W  s    
zFloatContainer._draw_float)rY   r[   rI   c                 C   sj   |}t |j|j|j D ]L}||jv r|j| }t |j|j|j D ]}|| }|jdkrD  dS qDqdS )z
        Return True when the area below the write position is still empty.
        (For floats that should not hide content underneath.)
        r   FT)r   r   r~   data_bufferr   rT   r   )rL   rY   r[   r   r   rowr   r   rJ   rJ   rM   r     s    


zFloatContainer._area_is_emptyc                 C   s   | j S rm   r   rK   rJ   rJ   rM   ra     s    zFloatContainer.is_modalc                 C   s   | j S rm   r   rK   rJ   rJ   rM   rb     s    zFloatContainer.get_key_bindingsc                 C   s"   | j g}|dd | jD  |S )Nc                 s   s   | ]}|j V  qd S rm   r   )r   r   rJ   rJ   rM   	<genexpr>  r   z.FloatContainer.get_children.<locals>.<genexpr>)r   extendr   )rL   ry   rJ   rJ   rM   rc     s    zFloatContainer.get_children)FNrx   N)rd   re   rf   rg   r6   r	   rj   r
   r   r   ri   r   rh   r   rN   r)   rR   rW   r0   r.   r1   r`   r   r   ra   rb   r7   rc   rJ   rJ   rJ   rM   r<     sN       
; r<   c                   @   s   e Zd ZdZdeee ee ee ee eeeeg ef f  eeeeg ef f  e	e	ee e	e	ee	ddddZ
ee dd	d
Zee dddZedddZdS )r=   a  
    Float for use in a :class:`.FloatContainer`.
    Except for the `content` parameter, all other options are optional.

    :param content: :class:`.Container` instance.

    :param width: :class:`.Dimension` or callable which returns a :class:`.Dimension`.
    :param height: :class:`.Dimension` or callable which returns a :class:`.Dimension`.

    :param left: Distance to the left edge of the :class:`.FloatContainer`.
    :param right: Distance to the right edge of the :class:`.FloatContainer`.
    :param top: Distance to the top of the :class:`.FloatContainer`.
    :param bottom: Distance to the bottom of the :class:`.FloatContainer`.

    :param attach_to_window: Attach to the cursor from this window, instead of
        the current window.
    :param hide_when_covering_content: Hide the float when it covers content underneath.
    :param allow_cover_cursor: When `False`, make sure to display the float
        below the cursor. Not on top of the indicated position.
    :param z_index: Z-index position. For a Float, this needs to be at least
        one. It is relative to the z_index of the parent container.
    :param transparent: :class:`.Filter` indicating whether this float needs to be
        drawn transparently.
    NFr"   )r   r   r   r   r   rT   r~   r   r   r   r   r   r^   r   rI   c                 C   sx   |dksJ || _ || _|| _|| _|| _|| _|| _|	| _|
rHt|
nd | _	t
|| _|| _|| _|| _t|| _d S )Nr"   )r   r   r   r   rT   r~   r   r   rE   r   rD   r   r   r   r^   r   r   )rL   r   r   r   r   r   rT   r~   r   r   r   r   r   r^   r   rJ   rJ   rM   r     s     
zFloat.__init__rH   c                 C   s   t | jr|  S | jS rm   )callablerT   rK   rJ   rJ   rM   r   H  s    
zFloat.get_widthc                 C   s   t | jr|  S | jS rm   )r   r~   rK   rJ   rJ   rM   r   M  s    
zFloat.get_heightc                 C   s
   d| j  S )NzFloat(content=%r)r   rK   rJ   rJ   rM   __repr__R  s    zFloat.__repr__)NNNNNNFFNFFr"   F)rd   re   rf   rg   r6   r
   rh   r   r   rj   r   r   r   ri   r   rJ   rJ   rJ   rM   r=     sD                )r=   c                   @   sn  e Zd ZdZdeeeeedeeeeef f eeeef eeef f eeeddddZ	e
eeef dd	d
Ze
edddZe
ddddZe
ee dddZe
eeef dddZd*eedddZd+eedddZd,eeedddZe
edddZe
eddd Ze
edd!d"Ze
edd#d$Ze
edd%d&Zeed'd(d)ZdS )-r@   a{  
    Render information for the last render time of this control.
    It stores mapping information between the input buffers (in case of a
    :class:`~prompt_toolkit.layout.controls.BufferControl`) and the actual
    render position on the output screen.

    (Could be used for implementation of the Vi 'H' and 'L' key bindings as
    well as implementing mouse support.)

    :param ui_content: The original :class:`.UIContent` instance that contains
        the whole input, without clipping. (ui_content)
    :param horizontal_scroll: The horizontal scroll of the :class:`.Window` instance.
    :param vertical_scroll: The vertical scroll of the :class:`.Window` instance.
    :param window_width: The width of the window that displays the content,
        without the margins.
    :param window_height: The height of the window that displays the content.
    :param configured_scroll_offsets: The scroll offsets as configured for the
        :class:`Window` instance.
    :param visible_line_to_row_col: Mapping that maps the row numbers on the
        displayed screen (starting from zero for the first visible line) to
        (row, col) tuples pointing to the row and column of the :class:`.UIContent`.
    :param rowcol_to_yx: Mapping that maps (row, column) tuples representing
        coordinates of the :class:`UIContent` to (y, x) absolute coordinates at
        the rendered screen.
    r?   rB   N)window
ui_contenthorizontal_scrollvertical_scrollwindow_widthwindow_heightconfigured_scroll_offsetsvisible_line_to_row_colrowcol_to_yxx_offsety_offset
wrap_linesrI   c                 C   sF   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	|| _
d S rm   )r   r   r   r   r   r  r  r  _rowcol_to_yx	_x_offset	_y_offset)rL   r   r   r   r   r   r   r  r  r  r  r  r  rJ   rJ   rM   r   q  s    zWindowRenderInfo.__init__rH   c                 C   s   dd | j  D S )Nc                 S   s   i | ]\}}||d  qS )r   rJ   )r   Zvisible_lineZrowcolrJ   rJ   rM   
<dictcomp>  s   z?WindowRenderInfo.visible_line_to_input_line.<locals>.<dictcomp>)r  r   rK   rJ   rJ   rM   visible_line_to_input_line  s    z+WindowRenderInfo.visible_line_to_input_linec                 C   s^   | j j}z| j|j|jf \}}W n ty@   tddd Y S 0 t|| j || j dS dS )zy
        Return the cursor position coordinates, relative to the left/top corner
        of the rendered screen.
        r   r   N)	r   r   r  r   r   KeyErrorr   r  r	  )rL   r   r   r   rJ   rJ   rM   r     s    z WindowRenderInfo.cursor_positionc                 C   s^   | j d dkrd}n| j| jjj }t|| jj}t|t| jj	| j d  d | jj
dddS )a  
        Return a :class:`.ScrollOffsets` instance that indicates the actual
        offset. This can be less than or equal to what's configured. E.g, when
        the cursor is completely at the top, the top offset will be zero rather
        than what's configured.
        r   r"   )r   r   r   r   )displayed_linesinput_line_to_visible_liner   r   r   r   r  r   rB   
line_countr   )rL   r   r   rJ   rJ   rM   applied_scroll_offsets  s    z'WindowRenderInfo.applied_scroll_offsetsc                 C   s   t dd | j D S )z
        List of all the visible rows. (Line numbers of the input buffer.)
        The last line may not be entirely visible.
        c                 s   s   | ]\}}|V  qd S rm   rJ   )r   r   colrJ   rJ   rM   r     r   z3WindowRenderInfo.displayed_lines.<locals>.<genexpr>)sortedr  valuesrK   rJ   rJ   rM   r    s    z WindowRenderInfo.displayed_linesc                 C   s@   i }| j  D ],\}}||v r2t|| |||< q|||< q|S )z
        Return the dictionary mapping the line numbers of the input buffer to
        the lines of the screen. When a line spans several rows at the screen,
        the first row appears in the dictionary.
        )r  r   r   )rL   r   kvrJ   rJ   rM   r    s    
z+WindowRenderInfo.input_line_to_visible_lineF)after_scroll_offsetrI   c                 C   s    |r| j | jj S | j d S dS )z~
        Return the line number (0 based) of the input document that corresponds
        with the first visible line.
        r   N)r  r  r   )rL   r  rJ   rJ   rM   first_visible_line  s    z#WindowRenderInfo.first_visible_line)before_scroll_offsetrI   c                 C   s$   |r| j d| jj  S | j d S dS )zK
        Like `first_visible_line`, but for the last visible line.
        r  N)r  r  r   )rL   r  rJ   rJ   rM   last_visible_line  s    z"WindowRenderInfo.last_visible_line)r  r  rI   c                 C   s"   |  || ||  | d  S )zM
        Like `first_visible_line`, but for the center visible line.
        r   )r  r  )rL   r  r  rJ   rJ   rM   center_visible_line  s    z$WindowRenderInfo.center_visible_linec                 C   s   | j jS )z6
        The full height of the user control.
        )r   r  rK   rJ   rJ   rM   content_height  s    zWindowRenderInfo.content_heightc                 C   s   | j dko|  | jkS )zU
        True when the full height is visible (There is no vertical scroll.)
        r   )r   r  r  rK   rJ   rJ   rM   full_height_visible  s    
z$WindowRenderInfo.full_height_visiblec                 C   s
   | j dkS )z=
        True when the top of the buffer is visible.
        r   )r   rK   rJ   rJ   rM   top_visible  s    zWindowRenderInfo.top_visiblec                 C   s   |   | jd kS )z@
        True when the bottom of the buffer is visible.
        r"   )r  r  rK   rJ   rJ   rM   bottom_visible  s    zWindowRenderInfo.bottom_visiblec                 C   s   | j r
dS d| j | j S dS )z{
        Vertical scroll as a percentage. (0 means: the top is visible,
        100 means: the bottom is visible.)
        d   N)r  r   r  rK   rJ   rJ   rM   vertical_scroll_percentage  s    z+WindowRenderInfo.vertical_scroll_percentagelinenorI   c                 C   s$   | j r| j|| j| jjS dS dS )z|
        Return the height of the given line.
        (The height that it would take, if this line became visible.)
        r"   N)r  r   get_height_for_liner   r   get_line_prefix)rL   r#  rJ   rJ   rM   r$  &  s
    z$WindowRenderInfo.get_height_for_line)F)F)FF)rd   re   rf   rg   r&   rh   r   r   rj   r   r   r  r   r   r  r	   r  r  r  r  r  r  r  r  r  r!  r$  rJ   rJ   rJ   rM   r@   V  sV   

 	
r@   c                	   @   s   e Zd ZdZdeeeg ef f eeeg ef f eeeg ef f eeeg ef f ddddZeeddd	Z	eedd
dZ
eedddZeedddZedddZdS )rB   z
    Scroll offsets for the :class:`.Window` class.

    Note that left/right offsets only make sense if line wrapping is disabled.
    r   N)r   r   r   r   rI   c                 C   s   || _ || _|| _|| _d S rm   )_top_bottom_left_right)rL   r   r   r   r   rJ   rJ   rM   r   :  s    zScrollOffsets.__init__rH   c                 C   s
   t | jS rm   )r    r&  rK   rJ   rJ   rM   r   G  s    zScrollOffsets.topc                 C   s
   t | jS rm   )r    r'  rK   rJ   rJ   rM   r   K  s    zScrollOffsets.bottomc                 C   s
   t | jS rm   )r    r(  rK   rJ   rJ   rM   r   O  s    zScrollOffsets.leftc                 C   s
   t | jS rm   )r    r)  rK   rJ   rJ   rM   r   S  s    zScrollOffsets.rightc                 C   s   d | j| j| j| jS )Nz;ScrollOffsets(top={!r}, bottom={!r}, left={!r}, right={!r}))formatr&  r'  r(  r)  rK   rJ   rJ   rM   r   W  s    zScrollOffsets.__repr__)r   r   r   r   )rd   re   rf   rg   r   rh   r   r   r   r   r   r   r   ri   r   rJ   rJ   rJ   rM   rB   3  s*       rB   c                   @   s$   e Zd ZdZdeeddddZdS )rC   z6
    Column for a :class:`.Window` to be colored.
    class:color-columnN)positionr   rI   c                 C   s   || _ || _d S rm   )r,  r   )rL   r,  r   rJ   rJ   rM   r   e  s    zColorColumn.__init__)r+  )rd   re   rf   rg   rh   ri   r   rJ   rJ   rJ   rM   rC   `  s   rC   c                   @   s   e Zd ZdZdZdZdZdS )r>   z
    Alignment of the Window content.

    Note that this is different from `HorizontalAlign` and `VerticalAlign`,
    which are used for the alignment of the child containers in respectively
    `VSplit` and `HSplit`.
    ru   rv   rr   N)rd   re   rf   rg   ru   rv   rr   rJ   rJ   rJ   rM   r>   m  s   r>   c                   @   sl  e Zd ZdZdddddddddddddddddddejdddfee eeee	 e
e
e
e
eee  eee  ee e
e
eed ge	f  eed ge	f  e
e
e
edee eg ee f f eeeg ef f eeeg ef f edeeg ef f ee ddddZedd	d
ZddddZee	dddZe	dddZe	edddZe	e	edddZedNee eg ee	 f eedddZe	e	edddZ ee dddZ!e"e#e$eeee	 dd d!d"Z%e"e#e$eedd#d$d%Z&d&d&ddd&ddejdf	ee"e$e	e	e	e	eee	eeeeee	e	ge'f  e(e)e	e(e	e	f f e)e(e	e	f e(e	e	f f f d'd(d)Z*e"e$edd*d+d,Z+e"e$edd-d.d/Z,e"dd0d1d2Z-e"dd0d3d4Z.e"e/e	e	e	e	dd5d6d7Z0ee"e$e	e	dd8d9d:Z1ee	e	dd;d<d=Z2ee	e	dd;d>d?Z3ee	e	dd;d@dAZ4e5dBdCdDdEZ6dddFdGZ7dddHdIZ8ee9 ddJdKZ:ee; ddLdMZ<dS )Or?   a^  
    Container that holds a control.

    :param content: :class:`.UIControl` instance.
    :param width: :class:`.Dimension` instance or callable.
    :param height: :class:`.Dimension` instance or callable.
    :param z_index: When specified, this can be used to bring element in front
        of floating elements.
    :param dont_extend_width: When `True`, don't take up more width then the
                              preferred width reported by the control.
    :param dont_extend_height: When `True`, don't take up more width then the
                               preferred height reported by the control.
    :param ignore_content_width: A `bool` or :class:`.Filter` instance. Ignore
        the :class:`.UIContent` width when calculating the dimensions.
    :param ignore_content_height: A `bool` or :class:`.Filter` instance. Ignore
        the :class:`.UIContent` height when calculating the dimensions.
    :param left_margins: A list of :class:`.Margin` instance to be displayed on
        the left. For instance: :class:`~prompt_toolkit.layout.NumberedMargin`
        can be one of them in order to show line numbers.
    :param right_margins: Like `left_margins`, but on the other side.
    :param scroll_offsets: :class:`.ScrollOffsets` instance, representing the
        preferred amount of lines/columns to be always visible before/after the
        cursor. When both top and bottom are a very high number, the cursor
        will be centered vertically most of the time.
    :param allow_scroll_beyond_bottom: A `bool` or
        :class:`.Filter` instance. When True, allow scrolling so far, that the
        top part of the content is not visible anymore, while there is still
        empty space available at the bottom of the window. In the Vi editor for
        instance, this is possible. You will see tildes while the top part of
        the body is hidden.
    :param wrap_lines: A `bool` or :class:`.Filter` instance. When True, don't
        scroll horizontally, but wrap lines instead.
    :param get_vertical_scroll: Callable that takes this window
        instance as input and returns a preferred vertical scroll.
        (When this is `None`, the scroll is only determined by the last and
        current cursor position.)
    :param get_horizontal_scroll: Callable that takes this window
        instance as input and returns a preferred vertical scroll.
    :param always_hide_cursor: A `bool` or
        :class:`.Filter` instance. When True, never display the cursor, even
        when the user control specifies a cursor position.
    :param cursorline: A `bool` or :class:`.Filter` instance. When True,
        display a cursorline.
    :param cursorcolumn: A `bool` or :class:`.Filter` instance. When True,
        display a cursorcolumn.
    :param colorcolumns: A list of :class:`.ColorColumn` instances that
        describe the columns to be highlighted, or a callable that returns such
        a list.
    :param align: :class:`.WindowAlign` value or callable that returns an
        :class:`.WindowAlign` value. alignment of content.
    :param style: A style string. Style to be applied to all the cells in this
        window. (This can be a callable that returns a string.)
    :param char: (string) Character to be used for filling the background. This
        can also be a callable that returns a character.
    :param get_line_prefix: None or a callable that returns formatted text to
        be inserted before a line. It takes a line number (int) and a
        wrap_count and returns formatted text. This can be used for
        implementation of line continuations, things like Vim "breakindent" and
        so on.
    NFrx   )r   rT   r~   r^   dont_extend_widthdont_extend_heightignore_content_widthignore_content_heightleft_marginsright_marginsscroll_offsetsallow_scroll_beyond_bottomr  get_vertical_scrollget_horizontal_scrollalways_hide_cursor
cursorlinecursorcolumncolorcolumnsr   r   r   r%  rI   c                 C   s   t || _t || _t || _t || _t || _|p:t | _t || _t || _	t || _
t || _|	plg | _|
pvg | _|pt | _|| _|| _|pg | _|| _|| _|| _|| _|| _|| _|| _tdd| _tdd| _|   d S )N   r   r"   )r   r4  r7  r  r8  r9  r#   r   r-  r.  r/  r0  r1  r2  rB   r3  r5  r6  r:  r   r   r   r%  rT   r~   r^   r   _ui_content_cache_margin_width_cacherN   )rL   r   rT   r~   r^   r-  r.  r/  r0  r1  r2  r3  r4  r  r5  r6  r7  r8  r9  r:  r   r   r   r%  rJ   rJ   rM   r     s:    











zWindow.__init__rH   c                 C   s
   d| j  S )NzWindow(content=%r)r   rK   rJ   rJ   rM   r     s    zWindow.__repr__c                 C   s&   | j   d| _d| _d| _d | _d S )Nr   )r   rN   r   r   vertical_scroll_2render_inforK   rJ   rJ   rM   rN     s
    
zWindow.reset)marginrI   c                    s@   t dfdd td fdd}t jf}j||S )zb
        Return the width for this margin.
        (Calculate only once per render time.)
        rH   c                      s    j dddS )Nr   rT   r~   )_get_ui_contentrJ   rK   rJ   rM   get_ui_content  s    z0Window._get_margin_width.<locals>.get_ui_contentc                      s
     S rm   )r   rJ   )rC  r@  rJ   rM   r     s    z+Window._get_margin_width.<locals>.get_width)r&   rh   r   render_counterr=  r   )rL   r@  r   keyrJ   )rC  r@  rL   rM   _get_margin_width  s    zWindow._get_margin_widthc                    s0   t  fdd jD t  fdd jD  S )zN
        Calculate and return the width of the margin (left + right).
        c                 3   s   | ]}  |V  qd S rm   rF  r   mrK   rJ   rM   r      r   z1Window._get_total_margin_width.<locals>.<genexpr>)r   r1  r2  rK   rJ   rK   rM   _get_total_margin_width  s    "zWindow._get_total_margin_widthrO   c                    s2   t t d fdd}jtj| dS )z@
        Calculate the preferred width for this window.
        rH   c                     s8     rdS  } j |  }|dur4|| 7 }|S )zYContent width: is only calculated if no exact width for the
            window was given.N)r/  rJ  r   rR   )total_margin_widthrR   rP   rL   rJ   rM   preferred_content_width)  s    z7Window.preferred_width.<locals>.preferred_content_width	dimensionget_preferreddont_extend)r
   rh   _merge_dimensionsr,   rT   r-  )rL   rP   rM  rJ   rL  rM   rR   $  s    zWindow.preferred_widthrS   c                    s4   t t d fdd}jtj| dS )zA
        Calculate the preferred height for this window.
        rH   c                     s4     rdS  }  }j|   |jS )z[Content height: is only calculated if no exact height for the
            window was given.N)r0  rJ  r  r   rW   r%  )rK  r  rU   rL   rT   rJ   rM   preferred_content_heightH  s    z9Window.preferred_height.<locals>.preferred_content_heightrN  )r
   rh   rR  r,   r~   r.  )rL   rT   rU   rT  rJ   rS  rM   rW   C  s    zWindow.preferred_height)rO  rP  rQ  rI   c                 C   s   | pt  } | jr| j}n| }|durJ| jr8t|| j}| jrJt|| j}|rd|durdt| j|}n| jrp| jnd}| jr| jnd}t |||| jdS )z
        Take the Dimension from this `Window` class and the received preferred
        size from the `UIControl` and return a `Dimension` to report to the
        parent container.
        N)r   r   r   r   )r)   Zpreferred_specifiedr   Zmax_specifiedr   r   Zmin_specifiedr   )rO  rP  rQ  r   Zmax_Zmin_rJ   rJ   rM   rR  ^  s     

zWindow._merge_dimensions)rT   r~   rI   c                    s2   t d fdd}t j f}j||S )z0
        Create a `UIContent` instance.
        rH   c                      s   j j dS )NrA  )r   create_contentrJ   r~   rL   rT   rJ   rM   get_content  s    z+Window._get_ui_content.<locals>.get_content)r&   r   rD  r<  r   )rL   rT   r~   rW  rE  rJ   rV  rM   rB    s    zWindow._get_ui_contentc                 C   s0   t  }|jrdS |jjr,|jjr(|jjS dS dS )z1Return `False`, or the Digraph symbol to be used.^?N)r   Zquoted_insertZvi_stateZwaiting_for_digraphZdigraph_symbol1)rL   apprJ   rJ   rM   _get_digraph_char  s    zWindow._get_digraph_charrX   c                 C   s   t |j|j|j|jd}|  r8t|j| |jj|_| 	 r\t|j| 
|j|jj|_| jdu rj|n| j}t| j|||||}|du s|dkr|  n|j||d dS )z
        Write window to screen. This renders the user control, the margins and
        copies everything over to the absolute position at the given screen.
        r   Nr   r   )r1   r   r   rT   r~   r-  r   rR   r   r.  rW   r^   r   _write_to_screen_at_indexr   )rL   rY   rZ   r[   r\   r]   r^   r   rJ   rJ   rM   r`     s<    	zWindow.write_to_screen)rY   rZ   r[   r\   r]   rI   c                    s^  j dksjdkrd S fddjD }fddjD }t|| }jj| j }	t|	tspJ 	 }

|	j| j  || tjr nj}j|	|t|j| jj|
dj t jjjk|jd\jt| }j}t|	jjj| j j|||
d  _tdd	fd
d}|jjt| jj | jjj  |d d}tttd fdd}t j|D ]8\}}|dkr|||}!|||| ||7 }qjt| }t j|D ].\}}|||}!|||| ||7 }q"|| |j#< d S )Nr   c                    s   g | ]}  |qS rJ   rG  rH  rK   rJ   rM   r     r   z4Window._write_to_screen_at_index.<locals>.<listcomp>c                    s   g | ]}  |qS rJ   rG  rH  rK   rJ   rM   r     r   T)r  highlight_linesr>  r7  	has_focusr   r%  )r   r   r   r   r   r   r  r  r  r  r  r  r5   mouse_eventrI   c                    s   t  j vrtS dd   D }| jj}| jj}jt	 d }t
||}|dkrz|||f \}}W n ty   |d8 }Y qR0 jtt||d| j| j| jd}qqRjttddd| j| j| jd}|tkr| }|S )z
            Wrapper around the mouse_handler of the `UIControl` that turns
            screen coordinates into line coordinates.
            Returns `NotImplemented` if no UI invalidation should be done.
            c                 S   s   i | ]\}}||qS rJ   rJ   )r   r  r  rJ   rJ   rM   r
  5  r   zKWindow._write_to_screen_at_index.<locals>.mouse_handler.<locals>.<dictcomp>r"   r   r   )r,  
event_typebutton	modifiers)r   r   Zwalk_through_modal_areaNotImplementedr   r,  r   r   r   r   r   r  r   mouse_handlerr   r   ra  rb  rc  _mouse_handler)r`  Zyx_to_rowcolr   r   Zmax_yr   r  r   )r  rL   r  r[   rJ   rM   re  )  s@    




z7Window._write_to_screen_at_index.<locals>.mouse_handler)Zx_minZx_maxZy_minZy_maxhandler)rI  rT   rI   c                    s&   |   |j}t||d jS )zRender margin. Return `Screen`.r"   )Zcreate_marginr~   r$   rU  )rI  rT   	fragments)r?  r[   rJ   rM   render_marginp  s    
z7Window._write_to_screen_at_index.<locals>.render_margin)$r~   rT   r1  r2  r   r   rU  
isinstancer&   r  _scroll_fill_bgr   r   
_copy_bodyr   r   r>  r7  r   r   Zcurrent_controlr%  r   r   r@   r3  r?  r   Zset_mouse_handler_for_ranger-   rh   r   _copy_margin_apply_styleZ"visible_windows_to_write_positions)rL   rY   rZ   r[   r\   r]   Zleft_margin_widthsZright_margin_widthsrK  r   r  r   r  r  re  move_xri  rI  rT   margin_contentrJ   )r?  r  rL   r  r[   rM   r\    s    

<
	


z Window._write_to_screen_at_indexr   )r   
new_screenr[   rp  rT   r   r   r  r]  r>  r7  r^  r   r%  rI   c                    sb  j | j	jjtd i i dttttttttf d fddtd	
fdd}|  ttt	d	fd
d}	j
r|	j
j	j
j}|r| | |rd_n	j_|  |	r| |j |r$	j
r$|  	jrF| |	jj	jj tjj _fS )a  
        Copy the UIContent into the output screen.
        Return (visible_line_to_row_col, rowcol_to_yx) tuple.

        :param get_line_prefix: None or a callable that takes a line number
            (int) and a wrap_count (int) and returns formatted text.
        )rx   rx   F)liner#  r   r   is_inputrI   c              	      s  |r
}ni }|r:r:t |d}||||dd\}}d}r|r}t| } |dkr| r|t| d d 8 }|d7 }| dd= qR||8 } tjkrt| }	|		k r|	|	 d 7 }n& tjkrt| }	|		k r|	|	 7 }d}
d}| D ]^}}}|  }d|v r.j|  |   |7  < q|D ]}t||f }|j	}
r|| 	kr|| d | f|d < |d7 }|d7 }d}|rrt ||}||||dd\}}|  }|j
kr||f    S |dkr|dkr|	k r||| < |dkr>td|D ]}|| | < q$np|dkrdD ]`}|| dkrL|| |  j	|krL|| |  }t|j| |jf }||| | < qL| | f|||
| f< |
d7 }
||7 }q2q||fS )	z
            Copy over a single line to the output screen. This can wrap over
            multiple lines in the output. It will call the prefix (prompt)
            function before every line.
            r   Frt  r"   Nr   z[ZeroWidthEscape])r   r"   )r   r2   r   r>   rr   r   rv   Zzero_width_escapesr/   rT   r~   r   r   r   )rs  r#  r   r   rt  Zcurrent_rowcol_to_yxpromptZskippedZh_scroll
line_widthr  Z
wrap_countr   ro   _Znew_buffer_rowr   r   
char_widthr   pwZ	prev_charZchar2)r   	copy_line
empty_charr%  r   
new_bufferrr  r  r  rT   r  r[   r   r   rJ   rM   r{    s    






z$Window._copy_body.<locals>.copy_linerH   c                     sb    } }| j k r^|k r^|}|f| < d} |||| dd\}} |d7 }| d7 } q
| S )Nr   Tru  r"   )r~   get_line)r   r#  rs  r   )r{  r   r  r   r   r>  r  r[   rJ   rM   copy7  s    

zWindow._copy_body.<locals>.copy)r   r  rI   c                    sD   z | |f \}}W n t y2   tddd Y S 0 t||dS dS )z<Translate row/col from UIContent to real Screen coordinates.r   r   N)r  r   )r   r  r   r   )r  rJ   rM   cursor_pos_to_screen_posK  s
    z3Window._copy_body.<locals>.cursor_pos_to_screen_pos)F)r   r   r  r   r/   r   rh   rj   r   r   r   r   r   Zset_cursor_positionshow_cursor_highlight_digraph_highlight_cursorlinesr~   _show_key_processor_key_bufferZmenu_positionZset_menu_positionr   )rL   r   rr  r[   rp  rT   r   r   r  r]  r>  r7  r^  r   r%  r  r  Zscreen_cursor_positionrJ   )r   r{  r|  r%  r   r  r}  rr  r  r   r   r>  r  rT   r  r[   r   r   rM   rm    sb    
 
*} 


zWindow._copy_body)rY   r[   r]   rI   c           
      C   s   t | jr|  }n| j}|s"|r||}t|p.ddf }t|j|j|j D ]0}|j| }t|j|j|j D ]}	|||	< qlqJdS )zj
        Erase/fill the background.
        (Useful for floats and when a `char` has been given.)
        r   rx   N)	r   r   r/   r   r   r~   r   r   rT   )
rL   rY   r[   r]   r   r   Zchar_objr   r   r   rJ   rJ   rM   rl    s    


zWindow._fill_bg)rr  r[   r\   rI   c                 C   sT   |d t | j }|j||dd t|j|j|j d |jd}|j|ddd d S )Nr   F)r   afterr"   zclass:last-lineT)r  )r!   r   Z	fill_arear1   r   r   r~   rT   )rL   rr  r[   r\   r   r   rJ   rJ   rM   ro    s    zWindow._apply_style)rr  rI   c                 C   s4   |   }|r0|| }t|df |j|j |j< dS )zd
        When we are in Vi digraph mode, put a question mark underneath the
        cursor.
        zclass:digraphN)r[  get_cursor_positionr/   r   r   r   )rL   rr  Zdigraph_charr   rJ   rJ   rM   r    s    
zWindow._highlight_digraphc                 C   s\   t  }|jj}|rXt rX|jsX|d j}t|dkrX|| }t|df |j	|j
 |j< dS )ah  
        When the user is typing a key binding that consists of several keys,
        display the last pressed key if the user is in insert mode and the key
        is meaningful to be displayed.
        E.g. Some people want to bind 'jj' to escape in Vi insert mode. But the
             first 'j' needs to be displayed in order to get some feedback.
        r  r"   zclass:partial-key-bindingN)r   Zkey_processor
key_buffer_in_insert_moder   datar   r  r/   r   r   r   )rL   rr  rZ  r  r  r   rJ   rJ   rM   r    s    

z%Window._show_key_processor_key_buffer)rr  r   r   r   rT   r~   rI   c                 C   s,  d}d}|j }	|  rT|	|j }
t||| D ]$}|
| }t|j|j| f |
|< q.|  rt||| D ]0}|	| }
|
|j }t|j|j| f |
|j< qj| j	}t
|r| }|D ]r}t|tsJ |j}||| k rd|j }t||| D ]4}|	| }
|
||  }t|j|j| f |
|| < qqdS )z.
        Highlight cursor row/column.
        z class:cursor-line z class:cursor-column r   N)r   r8  r   r   r/   r   r   r9  r   r:  r   rj  rC   r,  )rL   rr  r   r   r   rT   r~   Zcursor_line_styleZcursor_column_styler   r   Zoriginal_chary2r:  cccolumnZcolor_column_stylerJ   rJ   rM   r    s>    



zWindow._highlight_cursorlines)rq  rr  r[   rp  rT   rI   c           	      C   s6   |j | }|j}t||||j}| |||d| dS )zL
        Copy characters from the margin screen to the real screen.
        r   N)r   r   r1   r~   rm  )	rL   rq  rr  r[   rp  rT   r   r   Zmargin_write_positionrJ   rJ   rM   rn  	  s    
zWindow._copy_margin)r   rT   r~   rI   c                 C   s&   |   r| j}n| j}|||| dS )zA
        Scroll body. Ensure that the cursor is visible.
        N)r  _scroll_when_linewrapping_scroll_without_linewrapping)rL   r   rT   r~   funcrJ   rJ   rM   rk  	  s    zWindow._scrollc           
         sJ  j jj jd_ttdfdd dkrJjj_d_dS  jj}| krj	jjj
jjd}jj_t|d | j_td| j_dS d_td fd	d
}td fdd}td fdd}| }	tjt|	| _tj| _ sFtj|	_dS )
        Scroll to make sure the cursor position is visible and that we maintain
        the requested scroll offset.

        Set `self.horizontal_scroll/vertical_scroll`.
        r   r"  c                    s    |  jS rm   )r$  r%  )r#  )rL   r   rT   rJ   rM   get_line_height.	  s    z9Window._scroll_when_linewrapping.<locals>.get_line_heightN)Z
slice_stopr"   rH   c                     sJ   d} j j}tj jddD ](}|  |7 } |  kr@|  S |}qdS )Nr   r  r   r   r   )used_heightprev_linenor#  )r  r~   scroll_offsets_bottomr   rJ   rM   get_min_vertical_scroll[	  s    zAWindow._scroll_when_linewrapping.<locals>.get_min_vertical_scrollc                     sJ   j j} d}tj jd ddD ]$}| |7 }|kr@|   S |} q | S )Nr   r"   r  r  r  r  r#  )r  scroll_offsets_topr   rJ   rM   get_max_vertical_scrollj	  s    zAWindow._scroll_when_linewrapping.<locals>.get_max_vertical_scrollc                     sJ   j d } d}tj d ddD ]$}| |7 }|kr@|   S |} q | S )z
            Calculate the upper most line that can be visible, while the bottom
            is still visible. We should not allow scroll more than this if
            `allow_scroll_beyond_bottom` is false.
            r"   r   r  )r  r   r  )r  r~   r   rJ   rM   get_topmost_visiblex	  s    
z=Window._scroll_when_linewrapping.<locals>.get_topmost_visible)r3  r   r   r   rh   r   r   r   r>  r$  r%  r   r   r   r4  )
rL   r   rT   r~   line_heightZtext_before_heightr  r  r  Ztopmost_visiblerJ   )r  r~   r  r  rL   r   rT   rM   r  	  sN    	


z Window._scroll_when_linewrappingc           	   
      s4  |j ptddd}d _|jdkr2d _d _dS t||j}t	t	t	t	t	t	t	d fdd} j
r 
  _t jt	sJ  jr   _t jt	sJ  j}| j|j|j|j j||jd _ jrtt |j jd}nd}| j|j|jt|d|j j || tt| j| d _dS )r  r   r   N)current_scrollscroll_offset_startscroll_offset_end
cursor_poswindow_sizecontent_sizerI   c                    s   t t||d |}t t||d |d | }| dk r<d}   s^| || kr^td|| } | || krxtd|| } | |d | | k r|d | | } | S )zEScrolling algorithm. Used for both horizontal and vertical scrolling.r   r"   r   )rh   r   r4  r   r  r  r  r  r  r  rK   rJ   rM   	do_scroll	  s$    
z6Window._scroll_without_linewrapping.<locals>.do_scrollr  )r   r   r>  r  r   r   r   r~  r   rh   r5  rj  r6  r3  r   r   r%  r   r   r   r   r   r   r   )	rL   r   rT   r~   r   Zcurrent_line_textr  offsetsZcurrent_line_prefix_widthrJ   rK   rM   r  	  s^    	

)	z#Window._scroll_without_linewrappingr5   r_  c                 C   s4   |j tjkr|   dS |j tjkr0|   dS tS )z
        Mouse handler. Called when the UI control doesn't handle this
        particular event.

        Return `NotImplemented` if nothing was done as a consequence of this
        key binding (no UI invalidate required in that case).
        N)ra  r   ZSCROLL_DOWN_scroll_downZ	SCROLL_UP
_scroll_uprd  )rL   r`  rJ   rJ   rM   rf  
  s    zWindow._mouse_handlerc                 C   sP   | j }|du rdS | j|j|j k rL|jj|jjkr>| j	  |  jd7  _dS )zScroll window down.Nr"   )
r?  r   r  r   r   r   r  r   r   Zmove_cursor_downrL   inforJ   rJ   rM   r  
  s    
zWindow._scroll_downc                 C   sR   | j }|du rdS |jdkrN|jj|jd |jj kr@| j  |  jd8  _dS )zScroll window up.Nr   r"   )	r?  r   r   r   r   r  r   r   Zmove_cursor_upr  rJ   rJ   rM   r  
  s    

zWindow._scroll_upc                 C   s
   | j  S rm   )r   rb   rK   rJ   rJ   rM   rb   0
  s    zWindow.get_key_bindingsc                 C   s   g S rm   rJ   rK   rJ   rJ   rM   rc   3
  s    zWindow.get_children)F)=rd   re   rf   rg   r>   ru   r
   r'   r(   rh   r   r   r-   rB   r   r   r	   rC   ri   r%   r   r   rN   rF  rJ  r)   rR   rW   staticmethodrj   rR  r&   rB  r[  r0   r.   r1   r`   r\  r   r   r   rm  rl  ro  r  r  r   r  rn  rk  r  r  r   rf  r  r  r   rb   r7   rc   rJ   rJ   rJ   rM   r?   {  s  ?

A 0; ?2 t2zic                   @   s   e Zd ZdZeeddddZedddZddd	d
Z	e
edddZe
e
edddZeeeeeee
 ddddZee dddZdS )rA   a  
    Wrapper around any other container that can change the visibility. The
    received `filter` determines whether the given container should be
    displayed or not.

    :param content: :class:`.Container` instance.
    :param filter: :class:`.Filter` instance.
    N)r   filterrI   c                 C   s   t || _t|| _d S rm   )rD   r   r   r  )rL   r   r  rJ   rJ   rM   r   A
  s    
zConditionalContainer.__init__rH   c                 C   s   d| j d| jdS )NzConditionalContainer(z	, filter=))r   r  rK   rJ   rJ   rM   r   E
  s    zConditionalContainer.__repr__c                 C   s   | j   d S rm   )r   rN   rK   rJ   rJ   rM   rN   H
  s    zConditionalContainer.resetrO   c                 C   s    |   r| j|S t S d S rm   )r  r   rR   r)   zerorQ   rJ   rJ   rM   rR   K
  s    z$ConditionalContainer.preferred_widthrS   c                 C   s"   |   r| j||S t S d S rm   )r  r   rW   r)   r  rV   rJ   rJ   rM   rW   Q
  s    z%ConditionalContainer.preferred_heightrX   c                 C   s"   |   r| j||||||S d S rm   )r  r   r`   r_   rJ   rJ   rM   r`   W
  s    	z$ConditionalContainer.write_to_screenc                 C   s   | j gS rm   r   rK   rJ   rJ   rM   rc   e
  s    z!ConditionalContainer.get_children)rd   re   rf   rg   r6   r   r   ri   r   rN   rh   r)   rR   rW   r0   r.   r1   rj   r
   r`   r	   r7   rc   rJ   rJ   rJ   rM   rA   7
  s   	rA   c                   @   s   e Zd ZdZeg ef ddddZedddZddd	d
Z	e
edddZe
e
edddZeeeeeee
 ddddZedddZee dddZee dddZdS )rG   z
    Container class that dynamically returns any Container.

    :param get_container: Callable that returns a :class:`.Container` instance
        or any widget with a ``__pt_container__`` method.
    N)get_containerrI   c                 C   s
   || _ d S rm   )r  )rL   r  rJ   rJ   rM   r   q
  s    zDynamicContainer.__init__rH   c                 C   s   |   }t|S )z
        Return the current container object.

        We call `to_container`, because `get_container` can also return a
        widget with a ``__pt_container__`` method.
        )r  rD   )rL   objrJ   rJ   rM   _get_containert
  s    zDynamicContainer._get_containerc                 C   s   |     d S rm   )r  rN   rK   rJ   rJ   rM   rN   ~
  s    zDynamicContainer.resetrO   c                 C   s   |   |S rm   )r  rR   rQ   rJ   rJ   rM   rR   
  s    z DynamicContainer.preferred_widthrS   c                 C   s   |   ||S rm   )r  rW   rV   rJ   rJ   rM   rW   
  s    z!DynamicContainer.preferred_heightrX   c                 C   s   |   |||||| d S rm   )r  r`   r_   rJ   rJ   rM   r`   
  s    	z DynamicContainer.write_to_screenc                 C   s   dS )NFrJ   rK   rJ   rJ   rM   ra   
  s    zDynamicContainer.is_modalc                 C   s   d S rm   rJ   rK   rJ   rJ   rM   rb   
  s    z!DynamicContainer.get_key_bindingsc                 C   s
   |   gS rm   )r  rK   rJ   rJ   rM   rc   
  s    zDynamicContainer.get_children)rd   re   rf   rg   r   r6   r   r7   r  rN   rh   r)   rR   rW   r0   r.   r1   ri   rj   r
   r`   ra   r   rb   r	   rc   rJ   rJ   rJ   rM   rG   i
  s"   
rG   )	containerrI   c                 C   s6   t | tr| S t| dr$t|  S td| dS )zC
    Make sure that the given object is a :class:`.Container`.
    rn   zNot a container object: N)rj  r7   hasattrrD   rn   
ValueErrorr  rJ   rJ   rM   rD   
  s
    

rD   c                 C   s>   t | tr| S t| dr*ttd|  S td| ddS )zB
    Make sure that the given argument is a :class:`.Window`.
    rn   rl   zNot a Window object: .N)rj  r?   r  rE   r   rn   r  r  rJ   rJ   rM   rE   
  s
    

rE   zTypeGuard[AnyContainer])valuerI   c                 C   s.   t | trdS t| dr*ttd|  S dS )zb
    Checks whether the given value is a container object
    (for use in assert statements).
    Trn   rl   F)rj  r7   r  rF   r   rn   )r  rJ   rJ   rM   rF   
  s
    

rF   N)arg   abcr   r   enumr   	functoolsr   typingr   r   r   r	   r
   r   r   r   r   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.cacher   Zprompt_toolkit.data_structuresr   Zprompt_toolkit.filtersr   r   r   r   Zprompt_toolkit.formatted_textr   r   r   Z#prompt_toolkit.formatted_text.utilsr   r   Zprompt_toolkit.key_bindingr   Zprompt_toolkit.mouse_eventsr   r   Zprompt_toolkit.utilsr   r   r    r!   Zcontrolsr#   r$   r%   r&   r'   rO  r(   r)   r*   r+   r,   Zmarginsr-   rZ   r.   rY   r/   r0   r1   utilsr2   Ztyping_extensionsr3   r4   Z'prompt_toolkit.key_binding.key_bindingsr5   __all__r7   rl   r6   rp   r9   r8   rw   r:   r;   r<   r=   r@   rB   rC   r  r>   r?   rA   rG   rD   rE   objectrF   rJ   rJ   rJ   rM   <module>   sz   ,G	, p  
  Q ^-
         E2;