o
    iJ(                     @   s   d Z ddlZddlmZ ddl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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 eG dd dZG dd deZdS )zContains the interface class :class:`.BaseComplexPrompt` for more complex prompts and the mocked document class :class:`.FakeDocument`.    N)	dataclass)AnyCallableListOptionalTupleUnion)Application)EditingMode)	ConditionFilterOrBool)KeyHandlerCallable)Keys)BaseSimplePrompt)INQUIRERPY_KEYBOARD_INTERRUPT)InquirerPySessionResultInquirerPyStyleInquirerPyValidatec                   @   s&   e Zd ZU dZeed< dZeed< dS )FakeDocumenta  A fake `prompt_toolkit` document class.

    Work around to allow non-buffer type :class:`~prompt_toolkit.layout.UIControl` to use
    :class:`~prompt_toolkit.validation.Validator`.

    Args:
        text: Content to be validated.
        cursor_position: Fake cursor position.
    textr   cursor_positionN)__name__
__module____qualname____doc__str__annotations__r   int r   r   T/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/InquirerPy/base/complex.pyr      s   
 
r   c                %       s  e Zd ZdZ																dCd	eeeegef f d
ee	 de
de
dededededeeegef  deeegef  dee dede
de
de
dedee ddf$ fddZdDddZddd eeef dedeegef f fd!d"ZdDd#d$Zd%ee ddfd&d'Zd	eddfd(d)Zdeeeef  fd*d+Zd,ee ddfd-d.Zdeeeef  f fd/d0Zdefd1d2ZdDd3d4Zedefd5d6Zej d7eddfd8d6Zede!fd9d:Z"ede!fd;d<Z#ede!fd=d>Z$ede!fd?d@Z%ede!fdAdBZ&  Z'S )EBaseComplexPrompta  A base class to create a more complex prompt that will involve :class:`~prompt_toolkit.application.Application`.

    Note:
        This class does not create :class:`~prompt_toolkit.layout.Layout` nor :class:`~prompt_toolkit.application.Application`,
        it only contains the necessary attributes and helper functions to be consumed.

    Note:
        Use :class:`~InquirerPy.base.BaseListPrompt` to create a complex list prompt which involves multiple choices. It has
        more methods and helper function implemented.

    See Also:
        :class:`~InquirerPy.base.BaseListPrompt`
        :class:`~InquirerPy.prompts.fuzzy.FuzzyPrompt`
    NF? Invalid inputTMandatory promptmessagestylebordervi_modeqmarkamarkinstructionlong_instructiontransformerfiltervalidateinvalid_message
wrap_linesraise_keyboard_interrupt	mandatorymandatory_messagesession_resultreturnc                    s   t  j|||||||	|
|||||||d | _d _d _d _  | _| _d _ jr6  jd7  _ jr@  jd7  _ jsEdnd _	 j
rS  j	 j7  _	t fdd _t fdd _t fd	d _d S )
N)r%   r&   r(   r)   r*   r+   r-   r.   r0   r/   r1   r2   r3   r4   r5   F      r   c                      s    j tjkS N)_editing_moder
   VIr   selfr   r   <lambda>l   s    z,BaseComplexPrompt.__init__.<locals>.<lambda>c                      s    j S r9   _invalidr   r<   r   r   r>   m   s    c                      s
    j dkS )Nr"   )_long_instructionr   r<   r   r   r>   o   s   
 )super__init___invalid_message	_renderedr@   _loadingrA   _border_height_offset _validation_window_bottom_offset_wrap_lines!extra_long_instruction_line_countr   _is_vim_edit_is_invalid_is_displaying_long_instruction)r=   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   	__class__r<   r   rC   5   sN   

zBaseComplexPrompt.__init__c                 C   s   | j   dS )zRedraw the application UI.N)_application
invalidater<   r   r   r   _redrawr   s   zBaseComplexPrompt._redraw)r.   keysc                   s.   t  j|d|i dtdtf fdd}|S )zDecorate keybinding registration function.

        Ensure that the `invalid` state is cleared on next keybinding entered.
        r.   funcr6   c                    s    fdd}|S )Nc                    s   j rd_  |  d S )NFr?   )event)rU   r=   r   r   
executable   s   zDBaseComplexPrompt.register_kb.<locals>.decorator.<locals>.executabler   )rU   rW   kb_decr=   )rU   r   	decorator   s   z0BaseComplexPrompt.register_kb.<locals>.decorator)rB   register_kbr   )r=   r.   rT   rZ   rO   rX   r   r[   v   s   	zBaseComplexPrompt.register_kbc                 C   s4   d| j d< t| j d< d| j d< | jj|d d dS )zSet exception handler for the event loop.

        Skip the question and raise exception.

        Args:
            loop: Current event loop.
            context: Exception context.
        Tansweredresultskipped	exception)r_   N)_statusr   rQ   exit)r=   _contextr   r   r   _exception_handler   s   
	

z$BaseComplexPrompt._exception_handlerappc                 C   s&   | j sd| _ |   | | dS dS )am  Run after the :class:`~prompt_toolkit.application.Application` is rendered/updated.

        Since this function is fired up on each render, adding a check on `self._rendered` to
        process logics that should only run once.

        Set event loop exception handler here, since its guaranteed that the event loop is running
        in `_after_render`.
        TN)rE   _keybinding_factory_on_rendered)r=   re   r   r   r   _after_render   s
   	zBaseComplexPrompt._after_renderc                 C   s   || _ d| _dS )zoSet error message and set invalid state.

        Args:
            message: Error message to display.
        TN)rD   r@   )r=   r%   r   r   r   
_set_error   s   
zBaseComplexPrompt._set_errorc                 C   s   d| j fgS )zsObtain the error message dynamically.

        Returns:
            FormattedText in list of tuple format.
        zclass:validation-toolbar)rD   r<   r   r   r   _get_error_message   s   z$BaseComplexPrompt._get_error_messagerb   c                 C   s   dS )zARun once after the UI is rendered. Acts like `ComponentDidMount`.Nr   )r=   rb   r   r   r   rg      s   zBaseComplexPrompt._on_renderedc                    s8   d| j r	d| j  ndf}dd| jd  f}t ||S )zqGet the prompt message to display.

        Returns:
            Formatted text in list of tuple format.
        zclass:instructionz %s  zclass:answerz %sr]   )r+   statusrB   _get_prompt_message)r=   
pre_answerpost_answerrO   r   r   rm      s
   z%BaseComplexPrompt._get_prompt_messagec                 C   s
   | j  S )zRun the application.)applicationrunr<   r   r   r   _run      
zBaseComplexPrompt._runc                    s   | j  I dH S )z#Run the application asynchronously.N)rp   	run_asyncr<   r   r   r   
_run_async   s   zBaseComplexPrompt._run_asyncc                 C   s   | j st| j S )ad  Get the application.

        :class:`.BaseComplexPrompt` requires :attr:`.BaseComplexPrompt._application` to be defined since this class
        doesn't implement :class:`~prompt_toolkit.layout.Layout` and :class:`~prompt_toolkit.application.Application`.

        Raises:
            NotImplementedError: When `self._application` is not defined.
        )rQ   NotImplementedErrorr<   r   r   r   rp      s   
zBaseComplexPrompt.applicationvaluec                 C   s
   || _ d S r9   )rQ   )r=   rw   r   r   r   rp      rs   c                 C   s   | j s| jS | j| j S )zint: Height offset to apply.)rJ   rH   extra_line_countr<   r   r   r   height_offset   s   zBaseComplexPrompt.height_offsetc                 C   s^   d}| j r|t| j 7 }|d7 }|tt| j7 }|d7 }|tt| j7 }| jr-|d7 }|S )z!int: Total length of the message.r   r8   )_qmarklenr   _message_instruction)r=   total_message_lengthr   r   r   r~      s   z&BaseComplexPrompt.total_message_lengthc                 C   s   t  \}}| jd | S )a  int: Get the extra lines created caused by line wrapping.

        Minus 1 on the totoal message length as we only want the extra line.
        24 // 24 will equal to 1 however we only want the value to be 1 when we have 25 char
        which will create an extra line.
        r8   )shutilget_terminal_sizer~   r=   
term_widthrb   r   r   r   extra_message_line_count   s   z*BaseComplexPrompt.extra_message_line_countc                 C   s(   | j rt \}}t| j d | S dS )zint: Get the extra lines created caused by line wrapping.

        See Also:
            :attr:`.BaseComplexPrompt.extra_message_line_count`
        r8   r   )rA   r   r   r{   r   r   r   r   rK     s   z3BaseComplexPrompt.extra_long_instruction_line_countc                 C   s   d}|| j 7 }|| j7 }|S )zGet the extra lines created caused by line wrapping.

        Used mainly to calculate how much additional offset should be applied when getting
        the height.

        Returns:
            Total extra lines created due to line wrapping.
        r   )r   rK   )r=   r]   r   r   r   rx     s   


z"BaseComplexPrompt.extra_line_count)NFFr!   r!   r"   r"   NNNr#   TTTr$   N)r6   N)(r   r   r   r   r   r   r   r   r   r   boolr   r   rC   rS   r   r   r   r[   rd   r	   rh   ri   r   r   rj   rg   rm   rr   ru   propertyrp   setterr   ry   r~   r   rK   rx   __classcell__r   r   rO   r   r    %   s    	

=

	

r    ) r   r   dataclassesr   typingr   r   r   r   r   r   prompt_toolkit.applicationr	   prompt_toolkit.enumsr
   prompt_toolkit.filters.baser   r   'prompt_toolkit.key_binding.key_bindingsr   prompt_toolkit.keysr   InquirerPy.base.simpler   InquirerPy.enumr   InquirerPy.utilsr   r   r   r   r    r   r   r   r   <module>   s     