U
    d                     @  s   d Z ddlmZ ddlZddlZddlmZmZ ddl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mZ ddlmZ erddlmZ ddlmZ ddgZeZdddddZdddddZddddddZ d!ddddd Z!dS )"z2
Key binding handlers for displaying completions.
    )annotationsN)TYPE_CHECKINGList)in_terminal)CompleteEvent
Completionget_common_complete_suffix)StyleAndTextTuples)KeyBindings)KeyPressEvent)Keys)
get_cwidth)ApplicationPromptSessiongenerate_completions!display_completions_like_readlineENoneeventreturnc                 C  s&   | j }|jr|  n|jdd dS )z{
    Tab-completion: where the first tab completes the common suffix and the
    second tab lists all the completions.
    T)Zinsert_common_partN)current_bufferZcomplete_stateZcomplete_nextZstart_completion)r   b r   R/tmp/pip-unpacked-wheel-4x_7prb2/prompt_toolkit/key_binding/bindings/completion.pyr   "   s    
c                 C  s   | j }|jdkrdS tdd}t|j|j|}t|j|}t|dkrn||d j	  |
|d j n |r~|
| n|rt| j| dS )a  
    Key binding handler for readline-style tab completion.
    This is meant to be as similar as possible to the way how readline displays
    completions.

    Generate the completions immediately (blocking) and display them above the
    prompt in columns.

    Usage::

        # Call this handler when 'Tab' has been pressed.
        key_bindings.add(Keys.ControlI)(display_completions_like_readline)
    NT)Zcompletion_requested   r   )r   Z	completerr   listZget_completionsdocumentr   lenZdelete_before_cursorZstart_positionZinsert_texttext"_display_completions_like_readlineapp)r   r   Zcomplete_eventcompletionsZcommon_suffixr   r   r   r   0   s    

zApplication[object]zlist[Completion]zasyncio.Task[None])r"   r#   r   c                   s   ddl m ddlm  j }|j}|j}t|t	dd D d t	d| |d  t
ttt ddd	 fd
ddd fdd} | S )z
    Display the list of completions in columns above the prompt.
    This will ask for a confirmation if there are too many completions to fit
    on a single page and provide a paginator to walk through them.
    r   )to_formatted_text)create_confirm_sessionc                 s  s   | ]}t |jV  qd S )N)r   display_text).0cr   r   r   	<genexpr>h   s     z5_display_completions_like_readline.<locals>.<genexpr>r   intr   )pager   c              
     s   |  | d    t tt t  fddtD }g }tD ]}tD ]n}zT|| | }d|jp~d }||j|d t	|j
 }||jd| f W qb tk
r   Y qbX qb|d qV|d	 d S )
Nr   c                   s$   g | ]} | |d    qS )r   r   )r'   iZpage_completionsZpage_row_countr   r   
<listcomp>v   s   zG_display_completions_like_readline.<locals>.display.<locals>.<listcomp>z+class:readline-like-completions.completion  )style )r/   
zclass:readline-like-completions)r*   mathceilr   floatranger0   extenddisplayr   r&   append
IndexErrorZ
print_text)r+   Zpage_columnsresultrr(   
completionr0   padding)r"   column_countr#   completions_per_pagemax_compl_widthr$   r-   r   r8   o   s.     
z3_display_completions_like_readline.<locals>.display)r   c               
     s   t dd4 I dH  tkrdt d I dH } | rtD ]D}| |d krJtd I dH }|sJ W 5 Q I dH R  dS qJq j  nd W 5 Q I dH R X dS )	z
Coroutine.T)Zrender_cli_doneNzDisplay all z possibilities?r   --MORE--r   )r   r   Zprompt_asyncr6   _create_more_sessionoutputflush)Zconfirmr+   Z	show_more)r"   r#   r@   r%   r8   
page_countr   r   	run_compl   s     z5_display_completions_like_readline.<locals>.run_compl)prompt_toolkit.formatted_textr$   Zprompt_toolkit.shortcuts.promptr%   rD   get_sizecolumnsZrowsminmaxr*   r3   r4   r   r5   Zcreate_background_task)r"   r#   Z	term_sizeZ
term_widthZterm_heightrG   r   )	r"   r?   r#   r@   r%   r8   rA   rF   r$   r   r!   T   s    
 "r!   rB   strzPromptSession[bool])messager   c              	   C  s   ddl m} t }|d|d|d|tj|tj|tjdddd	d
}|d|d|d|d|tjddddd}|tj	ddddd}|| |ddS )zH
    Create a `PromptSession` object for displaying the "--MORE--".
    r   r   r1   yYr   r   r   c                 S  s   | j jdd d S )NTr;   r"   exitr   r   r   r   _yes   s    z"_create_more_session.<locals>._yesnNqQc                 S  s   | j jdd d S )NFrQ   rR   rT   r   r   r   _no   s    z!_create_more_session.<locals>._noc                 S  s   dS )zDisable inserting of text.Nr   rT   r   r   r   _ignore   s    z%_create_more_session.<locals>._ignoreT)Zkey_bindingsZerase_when_done)
prompt_toolkit.shortcutsr   r
   addr   ZControlJZControlMZControlIZControlCAny)rN   r   ZbindingsrU   rZ   r[   r   r   r   rC      s$    




rC   )rB   )"__doc__
__future__r   Zasyncior3   typingr   r   Z*prompt_toolkit.application.run_in_terminalr   Zprompt_toolkit.completionr   r   r   rH   r	   Z'prompt_toolkit.key_binding.key_bindingsr
   Z(prompt_toolkit.key_binding.key_processorr   Zprompt_toolkit.keysr   Zprompt_toolkit.utilsr   Zprompt_toolkit.applicationr   r\   r   __all__r   r   r   r!   rC   r   r   r   r   <module>   s,   $\