
    ^iJ(                         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             Z G d de      Zy)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                   *    e Zd ZU dZeed<   dZeed<   y)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     a/mnt/e/genesis-system/.venvs/browser-army/lib/python3.12/site-packages/InquirerPy/base/complex.pyr   r      s     IOSr!   r   c            %       \    e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-d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Zd.dZdddeeef   dedeegef   f fdZd.dZdee   ddfdZdeddfdZdeeeef      fdZd ee   ddfd!Zdeeeef      f fd"Zdefd#Zd.d$Zedefd%       Zej@                  d&eddfd'       Zede!fd(       Z"ede!fd)       Z#ede!fd*       Z$ede!fd+       Z%ede!fd,       Z& xZ'S )/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`
    NT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                      t            |||||||	|
|||||||       | _        d _        d _        d _          | _        | _        d _         j                  r xj                  dz  c_         j                  r xj                  dz  c_         j                  sdnd _	         j                  r xj                   j                  z  c_	        t         fd       _        t         fd       _        t         fd       _        y )	N)r%   r&   r(   r)   r*   r+   r-   r.   r0   r/   r1   r2   r3   r4   r5   F      r   c                  >     j                   t        j                  k(  S N)_editing_moder   VIselfs   r"   <lambda>z,BaseComplexPrompt.__init__.<locals>.<lambda>l   s    d.@.@KNN.R r!   c                       j                   S r;   _invalidr>   s   r"   r@   z,BaseComplexPrompt.__init__.<locals>.<lambda>m   s    T]] r!   c                  "     j                   dk7  S )N )_long_instructionr>   s   r"   r@   z,BaseComplexPrompt.__init__.<locals>.<lambda>o   s    D**b0 r!   )super__init___invalid_message	_renderedrC   _loadingrF   _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__s   `                 r"   rH   zBaseComplexPrompt.__init__5   s   ( 	##+!%=/) 	 	
" !0!1<<1$!!1$9=9O9OUV-11661 &&RS$%:;/800
,r!   c                 8    | j                   j                          y)zRedraw the application UI.N)_application
invalidater>   s    r"   _redrawzBaseComplexPrompt._redrawr   s    $$&r!   )r.   keysc                P     t           |d|idt        dt        f fd}|S )zDecorate keybinding registration function.

        Ensure that the `invalid` state is cleared on next keybinding entered.
        r.   funcr6   c                        fd       }|S )Nc                 <    j                   rd_          |        y )NFrB   )eventr[   r?   s    r"   
executablezDBaseComplexPrompt.register_kb.<locals>.decorator.<locals>.executable   s    ==$)DMUr!   r    )r[   r_   kb_decr?   s   ` r"   	decoratorz0BaseComplexPrompt.register_kb.<locals>.decorator   s     
 r!   )rG   register_kbr   )r?   r.   rY   ra   r`   rT   s   `   @r"   rb   zBaseComplexPrompt.register_kbv   s6     $d:6:	. 	3E 	 r!   c                     d| j                   d<   t        | j                   d<   d| j                   d<   | j                  j                  |d          y)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)rg   N)_statusr   rV   exit)r?   _contexts      r"   _exception_handlerz$BaseComplexPrompt._exception_handler   sJ     $(Z !>X"&Y)=>r!   appc                 n    | j                   s)d| _         | j                          | j                  |       yy)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)rJ   _keybinding_factory_on_rendered)r?   rm   s     r"   _after_renderzBaseComplexPrompt._after_render   s1     ~~!DN$$&c"	 r!   c                      || _         d| _        y)zoSet error message and set invalid state.

        Args:
            message: Error message to display.
        TN)rI   rC   )r?   r%   s     r"   
_set_errorzBaseComplexPrompt._set_error   s     !(r!   c                      d| j                   fgS )zsObtain the error message dynamically.

        Returns:
            FormattedText in list of tuple format.
        zclass:validation-toolbar)rI   r>   s    r"   _get_error_messagez$BaseComplexPrompt._get_error_message   s     +%%
 	
r!   rj   c                      y)zARun once after the UI is rendered. Acts like `ComponentDidMount`.Nr    )r?   rj   s     r"   rp   zBaseComplexPrompt._on_rendered   s    r!   c                     d| j                   rd| j                   z  ndf}dd| j                  d   z  f}t        |   ||      S )zqGet the prompt message to display.

        Returns:
            Formatted text in list of tuple format.
        zclass:instructionz %s  zclass:answerz %sre   )r+   statusrG   _get_prompt_message)r?   
pre_answerpost_answerrT   s      r"   rz   z%BaseComplexPrompt._get_prompt_message   sS      )-)9)9FT%%%s

 &ut{{8/D'DEw*:{CCr!   c                 6    | j                   j                         S )zRun the application.)applicationrunr>   s    r"   _runzBaseComplexPrompt._run   s    ##%%r!   c                 R   K   | j                   j                          d{   S 7 w)z#Run the application asynchronously.N)r~   	run_asyncr>   s    r"   
_run_asynczBaseComplexPrompt._run_async   s!     %%//1111s   '%'c                 >    | 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.
        )rV   NotImplementedErrorr>   s    r"   r~   zBaseComplexPrompt.application   s       %%   r!   valuec                     || _         y r;   )rV   )r?   r   s     r"   r~   zBaseComplexPrompt.application   s
    !r!   c                 d    | j                   s| j                  S | j                  | j                  z   S )zint: Height offset to apply.)rO   rM   extra_line_countr>   s    r"   height_offsetzBaseComplexPrompt.height_offset   s0     &&&$$t':':::r!   c                    d}| j                   r|t        | j                         z  }|dz  }|t        t        | j                              z  }|dz  }|t        t        | j                              z  }| j                  r|dz  }|S )z!int: Total length of the message.r   r9   )_qmarklenr   _message_instruction)r?   total_message_lengths     r"   r   z&BaseComplexPrompt.total_message_length   s      !;; C$44  A% C$6 77!C(9(9$: ;; A% ##r!   c                 T    t        j                         \  }}| j                  dz
  |z  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.
        r9   )shutilget_terminal_sizer   r?   
term_widthrj   s      r"   extra_message_line_countz*BaseComplexPrompt.extra_message_line_count   s,     002
A))A-*<<r!   c                     | j                   r2t        j                         \  }}t        | j                         dz
  |z  S y)zint: Get the extra lines created caused by line wrapping.

        See Also:
            :attr:`.BaseComplexPrompt.extra_message_line_count`
        r9   r   )rF   r   r   r   r   s      r"   rP   z3BaseComplexPrompt.extra_long_instruction_line_count  s=     !!"446MJ../!3
BBr!   c                 F    d}|| j                   z  }|| j                  z  }|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   rP   )r?   re   s     r"   r   z"BaseComplexPrompt.extra_line_count  s0      	$///$888r!   )NFF?r   rE   rE   NNNzInvalid inputTTTzMandatory promptN)r6   N)(r   r   r   r   r	   r   r   r   r   r   boolr   r   rH   rX   r   r   r   rb   rl   r
   rq   rs   r   r   ru   rp   rz   r   r   propertyr~   setterr   r   r   r   rP   r   __classcell__)rT   s   @r"   r$   r$   %   s   $ ,0 "6:1515.)-!3<@%;
sH&=%>%CDDE;
 (;
 	;

 ;
 ;
 ;
 ;
 ;
 hucz23;
 3%*-.;
 -.;
 ;
 ;
 #';
  !;
" #;
$ !!89%;
& 
';
z'
 ?C49%/;	%&(::	;(?#+!6 #4 ## $ 
DsCx$9 
h{3  DT%S/%: D&c &2 ![ ! ! " " " " ;s ; ; $c $ $ =# = = 
3 
 
 #  r!   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"   <module>r      s^    N  ! > > 2 , ? F $ 3 9    A( Ar!   