o
    :Fd                     @   sh   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 Z
dd	 Zd
d Zdd Zdd ZdS )    )named_commands)key_binding)REQUIRED_PARAM_EXCEPTION)get_error_message)get_appc                 C   s   d|  dd  }| S )Nzoci -r   )splitstrip)textcmd r   <usr/lib/python3.10/site-packages/interactive/key_bindings.pyfind_command_name	   s   r   c                 C   s.   |   }g }|D ]}|dr|| q|S )Nr   )r   
startswithappend)r
   r   paramspr   r   r   %find_all_typed_params_on_command_line   s   

r   c           	      C   s^   g }t | }t}||v r|S t| }|D ]}d}|D ]}||v r"d}q|s,||d  q|S )NFTr   )r   r   r   r   )	r
   list_of_required_paramsmissing_required_paramr   	exceptionZtyped_paramsZ
param_optsfoundparamr   r   r   all_required_param_selected   s    r   c                 C   s,   g d}d}|D ]}|| v rd} |S q|S )N)	z"--generate-full-command-json-inputz--generate-param-json-inputz--helpz-hz-?z--latest-versionz-vz	--versionz--release-infoFTr   )buffer_listZglobal_paramr   r   r   r   r   global_param_check-   s   r   c                     sd   t  }|dddd}|dddd}|dd fd	d
}|dd fdd}|S )NZ	backspacereturnc                 S   s   t |  | jd d S )N )r   Zbackward_delete_charcurrent_bufferZinsert_texteventr   r   r   
_backspace<   s   
z(override_key_binding.<locals>._backspacezc-dc                 S   s   t    d S )N)r   exitr   r   r   r   _quit_interactiveC   s   z/override_key_binding.<locals>._quit_interactiveZenterc                    s   | j }|j }t|j d j}t|}|jr|js6|r|r&t|  d S  d j	t
dt|dd d S |j \}}|  ||_d S )NZ	completertoolbarZmissing_required_paramsTZis_error)r   r
   r   r   r   r   Zcomplete_stater   Zaccept_lineset_toolbar_textr   strZdocumentZcut_selectionreset)r    bufferr   r   Zis_global_paramZdocument_with_selection_kwargsr   r   
_enter_keyH   s*   


z(override_key_binding.<locals>._enter_keyZf7c              
      sx   | j }z|j}|  |  W n ty. } z d jtddd W Y d }~d S d }~ww  d jtddd d S )Nr$   Z	try_againTr%   Zhistory_clearF)r   historyZdelete_history_filer(   	Exceptionr&   r   )r    r)   Zcli_interactive_historyer+   r   r   _clear_historyc   s    
z,override_key_binding.<locals>._clear_history)r   N)r   ZKeyBindingsadd)r,   Zkbr!   r#   r-   r1   r   r+   r   override_key_binding9   s   r3   N)Z#prompt_toolkit.key_binding.bindingsr   Zprompt_toolkitr   Zinteractive.utilsr   Zinteractive.error_messagesr   Zprompt_toolkit.applicationr   r   r   r   r   r3   r   r   r   r   <module>   s    	