B
    't\`                 @   s@  d Z ddlmZ ddlmZ ddl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 dd
lmZmZmZ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m Z m!Z!m"Z"m#Z#m$Z$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l0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? ddl@mAZA ddddddd d!d"d#d$gZBG d%d& d&ZCG d'd deDZEG d(d deDZFG d)d deDZGG d*d deDZHG d+d deDZIG d,d deDZJG d-d# d#eDZKG d.d" d"eDZLG d/d  d eDZMG d0d! d!eDZNG d1d$ d$eDZOdS )2a  
Collection of reusable components for building full screen applications.

All of these widgets implement the ``__pt_container__`` method, which makes
them usable in any situation where we are expecting a `prompt_toolkit`
container object.

.. warning::

    At this point, the API for these widgets is considered unstable, and can
    potentially change between minor releases (we try not too, but no
    guarantees are made yet). The public API in
    `prompt_toolkit.shortcuts.dialogs` on the other hand is considered stable.
    )unicode_literals)partialN)get_app)DynamicAutoSuggest)Buffer)DynamicCompleter)Document)	to_filter	Conditionis_true	has_focusis_done)to_formatted_textTemplateis_formatted_text)fragment_list_to_text)KeyBindings)	WindowVSplitHSplitFloatContainerFloatWindowAlignis_containerConditionalContainerDynamicContainer)BufferControlFormattedTextControl)	Dimension)is_dimensionto_dimension)ScrollbarMarginNumberedMargin)PasswordProcessorConditionalProcessorBeforeInputAppendAutoSuggestion)DynamicLexer)MouseEventType)
get_cwidth)Keys   )SearchToolbarTextAreaLabelButtonFrameShadowBoxVerticalLineHorizontalLine	RadioListCheckboxProgressBarc               @   s(   e Zd ZdZdZdZdZdZdZdZ	dS )	Borderz  Box drawing characters. (Thin) u   ─u   │u   ┌u   ┐u   └u   ┘N)
__name__
__module____qualname____doc__
HORIZONTALVERTICALTOP_LEFT	TOP_RIGHTBOTTOM_LEFTBOTTOM_RIGHT rC   rC   :lib/python3.7/site-packages/prompt_toolkit/widgets/base.pyr8   :   s   r8   c               @   sp   e Zd ZdZdddZedd	 Zejd
d	 Zedd Zejdd Zedd Z	e	jdd Z	dd Z
dS )r-   a	  
    A simple input field.

    This is a higher level abstraction on top of several other classes with
    sane defaults.

    This widget does have the most common options, but it does not intend to
    cover every single use case. For more configurations options, you can
    always build a text area manually, using a
    :class:`~prompt_toolkit.buffer.Buffer`,
    :class:`~prompt_toolkit.layout.BufferControl` and
    :class:`~prompt_toolkit.layout.Window`.

    Buffer attributes:

    :param text: The initial text.
    :param multiline: If True, allow multiline input.
    :param completer: :class:`~prompt_toolkit.completion.Completer` instance
        for auto completion.
    :param complete_while_typing: Boolean.
    :param accept_handler: Called when `Enter` is pressed (This should be a
        callable that takes a buffer as input).
    :param history: :class:`~prompt_toolkit.history.History` instance.
    :param auto_suggest: :class:`~prompt_toolkit.auto_suggest.AutoSuggest`
        instance for input suggestions.

    BufferControl attributes:

    :param password: When `True`, display using asterisks.
    :param focusable: When `True`, allow this widget to receive the focus.
    :param focus_on_click: When `True`, focus after mouse click.
    :param input_processors: `None` or a list of
        :class:`~prompt_toolkit.layout.Processor` objects.

    Window attributes:

    :param lexer: :class:`~prompt_toolkit.lexers.Lexer` instance for syntax
        highlighting.
    :param wrap_lines: When `True`, don't scroll horizontally, but wrap lines.
    :param width: Window width. (:class:`~prompt_toolkit.layout.Dimension` object.)
    :param height: Window height. (:class:`~prompt_toolkit.layout.Dimension` object.)
    :param scrollbar: When `True`, display a scroll bar.
    :param style: A style string.
    :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 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.

    Other attributes:

    :param search_field: An optional `SearchToolbar` object.
     TFNc                s  t |tjst|d ks&t |ts&t|d kr4d }nt |trD|j}|d krPg }| _| _| _| _	| _
| _tt|d|t fddt fddt fddt fdd||	d _t jt fddtt t jt @ tt t|d	t|d
dg| |||
|d _|rR|r8tddg}ng }|rLt g}ng }ntd}g }g }d| }t|||| j|t fdd|||d
 _ d S )Nr   c                  s
   t  jS )N)r   	read_onlyrC   )selfrC   rD   <lambda>   s    z#TextArea.__init__.<locals>.<lambda>c                  s    j S )N)	completerrC   )rG   rC   rD   rH      s    c                  s
   t  jS )N)r   complete_while_typingrC   )rG   rC   rD   rH      s    c                  s    j S )N)auto_suggestrC   )rG   rC   rD   rH      s    )document	multilinerF   rI   rJ   rK   accept_handlerhistoryc                  s    j S )N)lexerrC   )rG   rC   rD   rH      s    )Z	processorfilterzclass:text-area.prompt)style)bufferrP   input_processorsZsearch_buffer_controlpreview_search	focusablefocus_on_clickT)display_arrowsr+   zclass:text-area c                  s
   t  jS )N)r   
wrap_linesrC   )rG   rC   rD   rH      s    )
heightwidthdont_extend_heightdont_extend_widthcontentrR   rY   left_marginsright_marginsget_line_prefix)!
isinstancesix	text_typeAssertionErrorr,   controlrI   rJ   rP   rK   rF   rY   r   r   r
   r   r   rS   r   r'   r$   r&   r   r   r#   r	   r%   r!   r"   Dexactr   window)rG   textrM   ZpasswordrP   rK   rI   rJ   rN   rO   rV   rW   rY   rF   r[   rZ   r\   r]   Zline_numbersra   Z	scrollbarrR   Zsearch_fieldrU   promptrT   Zsearch_controlr`   r_   rC   )rG   rD   __init__~   sz    	





zTextArea.__init__c             C   s   | j jS )z$
        The `Buffer` text.
        )rS   rj   )rG   rC   rC   rD   rj      s    zTextArea.textc             C   s   | j jt|ddd d S )Nr   T)Zbypass_readonly)rS   Zset_documentr   )rG   valuerC   rC   rD   rj      s    c             C   s   | j jS )zA
        The `Buffer` document (text + cursor position).
        )rS   rL   )rG   rC   rC   rD   rL      s    zTextArea.documentc             C   s   || j _d S )N)rS   rL   )rG   rm   rC   rC   rD   rL      s    c             C   s   | j jS )zM
        The accept handler. Called when the user accepts the input.
        )rS   rN   )rG   rC   rC   rD   rN      s    zTextArea.accept_handlerc             C   s   || j _d S )N)rS   rN   )rG   rm   rC   rC   rD   rN      s    c             C   s   | j S )N)ri   )rG   rC   rC   rD   __pt_container__   s    zTextArea.__pt_container__)rE   TFNNNTNNTFTFNNFFFNFrE   NTrE   N)r9   r:   r;   r<   rl   propertyrj   setterrL   rN   rn   rC   rC   rC   rD   r-   D   s"   9        
Mc               @   s"   e Zd ZdZd
ddZdd	 ZdS )r.   aG  
    Widget that displays the given text. It is not editable or focusable.

    :param text: The text to be displayed. (This can be multiline. This can be
        formatted text as well.)
    :param style: A style string.
    :param width: When given, use this width, rather than calculating it from
        the text size.
    rE   NTFc                sR   t |st| _ fdd}t fddd _t j|d| ||d _d S )Nc                 sR   d krJt  j} t| }|r6tdd | D }n
tddS t|dS S d S )Nc             s   s   | ]}t |V  qd S )N)r)   ).0linerC   rC   rD   	<genexpr>  s    z4Label.__init__.<locals>.get_width.<locals>.<genexpr>r   )	preferred)r   rj   r   max
splitlinesrg   )Ztext_fragmentsrj   Zlongest_line)rG   r[   rC   rD   	get_width  s    


z!Label.__init__.<locals>.get_widthc                  s    j S )N)rj   rC   )rG   rC   rD   rH     s    z Label.__init__.<locals>.<lambda>)rj   zclass:label )r^   r[   rR   r\   r]   )r   re   rj   r   Zformatted_text_controlr   ri   )rG   rj   rR   r[   r\   r]   rw   rC   )rG   r[   rD   rl     s    zLabel.__init__c             C   s   | j S )N)ri   )rG   rC   rC   rD   rn     s    zLabel.__pt_container__)rE   NTF)r9   r:   r;   r<   rl   rn   rC   rC   rC   rD   r.      s   	 
c               @   s2   e Zd ZdZdddZdd Zdd	 Zd
d ZdS )r/   z
    Clickable button.

    :param text: The caption for the button.
    :param handler: `None` or callable. Called when the button is clicked.
    :param width: Width of the button.
    N   c          	      s   t |tjst|d ks$t|s$tt |ts2t| _| _| _t	 j
  dd _ fdd}t jtjd||ddd _d S )NT)key_bindingsrV   c                  s   t  j rdS dS d S )Nzclass:button.focusedzclass:button)r   layoutr   rC   )rG   rC   rD   	get_style6  s    z"Button.__init__.<locals>.get_styler+   )ZalignrZ   r[   rR   r]   r\   )rb   rc   rd   re   callableintrj   handlerr[   r   _get_text_fragments_get_key_bindingsrf   r   r   ZCENTERri   )rG   rj   r~   r[   r{   rC   )rG   rD   rl   )  s&    
zButton.__init__c                s@   d j d   j} fdd}dd|fdd||fdd	|fgS )
Nz{:^%s}   c                s   | j tjkr   d S )N)
event_typer(   MOUSE_UPr~   )mouse_event)rG   rC   rD   r~   H  s    z+Button._get_text_fragments.<locals>.handlerzclass:button.arrow<)z[SetCursorPosition]rE   zclass:button.text>)r[   formatrj   )rG   rj   r~   rC   )rG   rD   r   E  s    zButton._get_text_fragmentsc                s*   t  }|d|d fdd}|S )z Key bindings for the Button.  enterc                s    j d k	r    d S )N)r~   )event)rG   rC   rD   _W  s    
z#Button._get_key_bindings.<locals>._)r   add)rG   kbr   rC   )rG   rD   r   S  s    zButton._get_key_bindingsc             C   s   | j S )N)ri   )rG   rC   rC   rD   rn   _  s    zButton.__pt_container__)Nrx   )r9   r:   r;   r<   rl   r   r   rn   rC   rC   rC   rD   r/   !  s
   
c               @   s"   e Zd ZdZd	ddZdd ZdS )
r0   a  
    Draw a border around any container, optionally with a title text.

    Changing the title and body of the frame is possible at runtime by
    assigning to the `body` and `title` attributes of this class.

    :param body: Another container object.
    :param title: Text to be displayed in the top of the frame (can be formatted text).
    :param style: Style string to be applied to this widget.
    rE   NFc                s  t |stt|stt|tjs(tt|s4tt|s@t|d ksVt|tsVtt|tsdt| _	| _
ttdd}d| }t|ddtjd|tjd|ddddt fdd	d
dd|dddd|tjd|ddtjdgdd}	t|ddtjd|tjd|ddtjdgdd}
t fdd}tt|	|dt|
| dt|dtjdt fdd	|dtjdgddt|ddtjd|tjd|ddtjdgg|||||d _d S )Nzclass:frame.border)rR   zclass:frame r+   )r[   rZ   char)r   |c                  s   t d jS )Nz {} )r   r   titlerC   )rG   rC   rD   rH     s    z Frame.__init__.<locals>.<lambda>zclass:frame.labelT)rR   r]   )rZ   c                  s
   t  jS )N)boolr   rC   )rG   rC   rD   	has_title  s    z!Frame.__init__.<locals>.has_title)r^   rQ   )r[   r   c                  s    j S )N)bodyrC   )rG   rC   rD   rH     s    r   )padding)r[   rZ   rR   ry   modal)r   re   r   rb   rc   rd   r   r   r   r   r   r   r   r   r8   r?   r=   r.   r@   r
   r   r   r>   r   rA   rB   	container)rG   r   r   rR   r[   rZ   ry   r   ZfillZtop_row_with_titleZtop_row_without_titler   rC   )rG   rD   rl   n  s\    



zFrame.__init__c             C   s   | j S )N)r   )rG   rC   rC   rD   rn     s    zFrame.__pt_container__)rE   rE   NNNF)r9   r:   r;   r<   rl   rn   rC   rC   rC   rD   r0   c  s   
 
=c               @   s    e Zd ZdZdd Zdd ZdS )r1   z
    Draw a shadow underneath/behind this container.
    (This applies `class:shadow` the the cells under the shadow. The Style
    should define the colors for the shadow.)

    :param body: Another container object.
    c             C   sN   t |stt|tdddddtdddtdddddtdddgd| _d S )	Nr+   Tzclass:shadow)rR   )bottomrZ   leftrighttransparentr^   )r   topr[   r   r   r^   )r^   floats)r   re   r   r   r   r   )rG   r   rC   rC   rD   rl     s    

zShadow.__init__c             C   s   | j S )N)r   )rG   rC   rC   rD   rn     s    zShadow.__pt_container__N)r9   r:   r;   r<   rl   rn   rC   rC   rC   rD   r1     s   c               @   s"   e Zd ZdZd	ddZdd ZdS )
r2   a@  
    Add padding around a container.

    This also makes sure that the parent can provide more space than required by
    the child. This is very useful when wrapping a small element with a fixed
    size into a ``VSplit`` or ``HSplit`` object. The ``HSplit`` and ``VSplit``
    try to make sure to adapt respectively the width and height, possibly
    shrinking other elements. Wrapping something in a ``Box`` makes it flexible.

    :param body: Another container object.
    :param padding: The margin to be used around the body. This can be
        overridden by `padding_left`, padding_right`, `padding_top` and
        `padding_bottom`.
    :param style: A style string.
    :param char: Character to be used for filling the space around the body.
        (This is supposed to be a character with a terminal width of 1.)
    NrE   Fc          	      s   t |st d krtdd  fdd}||| _||| _||| _||| _|| _tt	| j|
dt
t	| j|
d|t	| j|
dgt	| j|
dg|||	|d d| _d S )Nr   )rt   c                s   | d kr } t | S )N)r    )rm   )r   rC   rD   get  s    zBox.__init__.<locals>.get)rZ   r   )r[   r   )r[   rZ   rR   r   ry   )r   re   rg   padding_leftpadding_rightpadding_toppadding_bottomr   r   r   r   r   )rG   r   r   r   r   r   r   r[   rZ   rR   r   r   ry   r   rC   )r   rD   rl     s$    




zBox.__init__c             C   s   | j S )N)r   )rG   rC   rC   rD   rn     s    zBox.__pt_container__)NNNNNNNrE   NFN)r9   r:   r;   r<   rl   rn   rC   rC   rC   rD   r2     s       
c               @   s&   e Zd Zd	ddZdd Zdd ZdS )
r6   rE   c                s   t |std _t }|d|d fdd}t j|dd _td jdd	 _	t
 j	ttd
|dgdd _d S )NTr   r   c                s    j   _ d S )N)checked)r   )rG   rC   rD   r   
  s    zCheckbox.__init__.<locals>._)ry   rV      r+   )r[   r^   rZ   z {})rj   zclass:checkbox)rR   )r   re   r   r   r   r   r   rf   r   ri   r   r.   r   r   r   )rG   rj   r   r   rC   )rG   rD   rl     s    
zCheckbox.__init__c             C   s:   dg}| d | jr"| d n
| d | d |S )N)rE   [)z[SetCursorPosition]rE   )rE   *)rE   r   )rE   ])appendr   )rG   resultrC   rC   rD   r     s    


zCheckbox._get_text_fragmentsc             C   s   | j S )N)r   )rG   rC   rC   rD   rn   )  s    zCheckbox.__pt_container__N)rE   )r9   r:   r;   rl   r   rn   rC   rC   rC   rD   r6     s   
c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r5   z}
    List of radio buttons. Only one can be checked at the same time.

    :param values: List of (value, label) tuples.
    c                s  t |tstt|dksttdd |D s4t| _|d d  _d _t }|	d fdd}|	d fdd}|	d	 fd
d}|	d fdd}|	d|	d fdd}|	t
j fdd}t j|dd _t jdtddgdd _d S )Nr   c             s   s$   | ]}t |tot|d kV  qdS )r   N)rb   tuplelen)rq   irC   rC   rD   rs   6  s   z%RadioList.__init__.<locals>.<genexpr>Zupc                s   t d jd  _d S )Nr   r+   )ru   _selected_index)r   )rG   rC   rD   r   @  s    zRadioList.__init__.<locals>._Zdownc                s    t t jd  jd  _d S )Nr+   )minr   valuesr   )r   )rG   rC   rD   r   D  s    Zpageupc                s(   | j jj}td jt|jj  _d S )Nr   )apprz   current_windowru   r   r   render_infodisplayed_lines)r   w)rG   rC   rD   r   I  s    
Zpagedownc                s2   | j jj}tt jd  jt|jj  _d S )Nr+   )	r   rz   r   r   r   r   r   r   r   )r   r   )rG   rC   rD   r   Q  s    
r   r   c                s    j  j d  _d S )Nr   )r   r   current_value)r   )rG   rC   rD   r   Y  s    c                sJ   xD j  jd d   j  D ]&}|d | jr j | _d S qW d S )Nr+   )r   r   
startswithdataindex)r   rm   )rG   rC   rD   r   ^  s     T)ry   rV   zclass:radio-list)rX   )r^   rR   r`   r\   )rb   listre   r   allr   r   r   r   r   r*   ZAnyr   r   rf   r   r!   ri   )rG   r   r   r   rC   )rG   rD   rl   3  s0    	

zRadioList.__init__c                s   fdd}g }xt  jD ]\}}|d  jk}| jk}d}|rL|d7 }|rX|d7 }||df |rt|d |r||d	f n||d
f ||df |d |t|d dd |d qW x2tt|D ]"}|| d || d |f||< qW |	  |S )Nc                s,   | j tjkr(| jj _ j j d  _dS )z
            Set `_selected_index` and `current_value` according to the y
            position of the mouse click event.
            r   N)r   r(   r   Zpositionyr   r   r   )r   )rG   rC   rD   mouse_handleru  s    
z4RadioList._get_text_fragments.<locals>.mouse_handlerr   rE   z class:radio-checkedz class:radio-selected()z[SetCursorPosition]rE   r   r   ))zclass:radior   r+   zclass:radio)rR   )rE   
)
	enumerater   r   r   r   extendr   ranger   pop)rG   r   r   r   rm   r   ZselectedrR   rC   )rG   rD   r   t  s0    	


"zRadioList._get_text_fragmentsc             C   s   | j S )N)ri   )rG   rC   rC   rD   rn     s    zRadioList.__pt_container__N)r9   r:   r;   r<   rl   r   rn   rC   rC   rC   rD   r5   -  s   A+c               @   s    e Zd ZdZdd Zdd ZdS )r3   z3
    A simple vertical line with a width of 1.
    c             C   s   t tjddd| _d S )Nzclass:line,vertical-liner+   )r   rR   r[   )r   r8   r>   ri   )rG   rC   rC   rD   rl     s    zVerticalLine.__init__c             C   s   | j S )N)ri   )rG   rC   rC   rD   rn     s    zVerticalLine.__pt_container__N)r9   r:   r;   r<   rl   rn   rC   rC   rC   rD   r3     s   c               @   s    e Zd ZdZdd Zdd ZdS )r4   z6
    A simple horizontal line with a height of 1.
    c             C   s   t tjddd| _d S )Nzclass:line,horizontal-liner+   )r   rR   rZ   )r   r8   r=   ri   )rG   rC   rC   rD   rl     s    zHorizontalLine.__init__c             C   s   | j S )N)ri   )rG   rC   rC   rD   rn     s    zHorizontalLine.__pt_container__N)r9   r:   r;   r<   rl   rn   rC   rC   rC   rD   r4     s   c               @   s6   e Zd Zdd Zedd Zejdd Zdd ZdS )	r7   c                sn   d _ td _ttddt jdddtddddttd fdd	d
td fdd	d
gdgd _d S )N<   z60%r+   )rZ   r   )r^   r   r   zclass:progress-bar.usedc                  s   t t jdS )N)weight)rg   r}   _percentagerC   )rG   rC   rD   rH     s    z&ProgressBar.__init__.<locals>.<lambda>)rR   r[   zclass:progress-barc                  s   t td j dS )Nd   )r   )rg   r}   r   rC   )rG   rC   rD   rH     s    )r   r   r   r   r^   )r^   r   )r   r.   labelr   r   r   r   r   )rG   rC   )rG   rD   rl     s    
zProgressBar.__init__c             C   s   | j S )N)r   )rG   rC   rC   rD   
percentage  s    zProgressBar.percentagec             C   s&   t |tst|| _d|| j_d S )Nz{0}%)rb   r}   re   r   r   r   rj   )rG   rm   rC   rC   rD   r     s    c             C   s   | j S )N)r   )rG   rC   rC   rD   rn     s    zProgressBar.__pt_container__N)r9   r:   r;   rl   ro   r   rp   rn   rC   rC   rC   rD   r7     s   )Pr<   Z
__future__r   	functoolsr   rc   Z"prompt_toolkit.application.currentr   Zprompt_toolkit.auto_suggestr   Zprompt_toolkit.bufferr   Zprompt_toolkit.completionr   Zprompt_toolkit.documentr   Zprompt_toolkit.filtersr	   r
   r   r   r   Zprompt_toolkit.formatted_textr   r   r   Z#prompt_toolkit.formatted_text.utilsr   Z'prompt_toolkit.key_binding.key_bindingsr   Z prompt_toolkit.layout.containersr   r   r   r   r   r   r   r   r   Zprompt_toolkit.layout.controlsr   r   Zprompt_toolkit.layout.dimensionr   rg   r   r    Zprompt_toolkit.layout.marginsr!   r"   Z prompt_toolkit.layout.processorsr#   r$   r%   r&   Zprompt_toolkit.lexersr'   Zprompt_toolkit.mouse_eventsr(   Zprompt_toolkit.utilsr)   Zprompt_toolkit.keysr*   Ztoolbarsr,   __all__r8   objectr-   r.   r/   r0   r1   r2   r6   r5   r3   r4   r7   rC   rC   rC   rD   <module>   s^   ,
 5)BM7+v