
    tniJ(                         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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                   ,    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     T/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/InquirerPy/base/complex.pyr   r      s7           IIIOSr!   r   c            %           e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d2d	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d3dZdddeeef         dedeegef         f fdZd3d 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d3d)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d0            Z%ede!fd1            Z& xZ'S )4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                     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           _        d S )	N)r)   r*   r,   r-   r.   r/   r1   r2   r4   r3   r5   r6   r7   r8   r9   F      r   c                  .     j         t          j        k    S N)_editing_moder   VIselfs   r"   <lambda>z,BaseComplexPrompt.__init__.<locals>.<lambda>l   s    d.@KN.R r!   c                       j         S r?   _invalidrB   s   r"   rD   z,BaseComplexPrompt.__init__.<locals>.<lambda>m   s	    T] r!   c                       j         dk    S )Nr&   )_long_instructionrB   s   r"   rD   z,BaseComplexPrompt.__init__.<locals>.<lambda>o   s    D*b0 r!   )super__init___invalid_message	_renderedrG   _loadingrI   _border_height_offset _validation_window_bottom_offset_wrap_lines!extra_long_instruction_line_countr   _is_vim_edit_is_invalid_is_displaying_long_instruction)rC   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   	__class__s   `                 r"   rK   zBaseComplexPrompt.__init__5   sa   ( 	##+!%=/) 	 	
 	
 	
" !0&!1< 	%1$! 	%1$9=9O0VUV- 	11611 &&R&R&R&RSS$%:%:%:%:;;/800000
 0
,,,r!   c                 8    | j                                          dS )zRedraw the application UI.N)_application
invalidaterB   s    r"   _redrawzBaseComplexPrompt._redrawr   s    $$&&&&&r!   )r2   keysc                h      t                      j        |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.
        r2   funcr:   c                 &      fd            }|S )Nc                 :    j         rd_          |            d S )NFrF   )eventr^   rC   s    r"   
executablezDBaseComplexPrompt.register_kb.<locals>.decorator.<locals>.executable   s%    = *$)DMUr!   r    )r^   rb   kb_decrC   s   ` r"   	decoratorz0BaseComplexPrompt.register_kb.<locals>.decorator   s4         V
 r!   )rJ   register_kbr   )rC   r2   r\   rd   rc   rW   s   `   @r"   re   zBaseComplexPrompt.register_kbv   s]     %$d:6::	. 	3E 	 	 	 	 	 	 	 r!   c                     d| j         d<   t          | j         d<   d| j         d<   | j                            |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)rj   N)_statusr   rY   exit)rC   _contexts      r"   _exception_handlerz$BaseComplexPrompt._exception_handler   sM     $(Z !>X"&Y)=>>>>>r!   appc                 x    | j         s2d| _         |                                  |                     |           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)rM   _keybinding_factory_on_rendered)rC   rp   s     r"   _after_renderzBaseComplexPrompt._after_render   sK     ~ 	#!DN$$&&&c"""""		# 	#r!   c                 "    || _         d| _        dS )zoSet error message and set invalid state.

        Args:
            message: Error message to display.
        TN)rL   rG   )rC   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)rL   rB   s    r"   _get_error_messagez$BaseComplexPrompt._get_error_message   s     +%
 	
r!   rm   c                     dS )zARun once after the UI is rendered. Acts like `ComponentDidMount`.Nr    )rC   rm   s     r"   rs   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 %srh   )r/   statusrJ   _get_prompt_message)rC   
pre_answerpost_answerrW   s      r"   r}   z%BaseComplexPrompt._get_prompt_message   sY      )-)9BFT%%%s

 &ut{8/D'DEww**:{CCCr!   c                 4    | j                                         S )zRun the application.)applicationrunrB   s    r"   _runzBaseComplexPrompt._run   s    ##%%%r!   c                 D   K   | j                                          d{V S )z#Run the application asynchronously.N)r   	run_asyncrB   s    r"   
_run_asynczBaseComplexPrompt._run_async   s-      %//111111111r!   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.
        )rY   NotImplementedErrorrB   s    r"   r   zBaseComplexPrompt.application   s       	&%%  r!   valuec                     || _         d S r?   )rY   )rC   r   s     r"   r   zBaseComplexPrompt.application   s    !r!   c                 <    | j         s| j        S | j        | j        z   S )zint: Height offset to apply.)rR   rP   extra_line_countrB   s    r"   height_offsetzBaseComplexPrompt.height_offset   s)      	'&&$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   r=   )_qmarklenr   _message_instruction)rC   total_message_lengths     r"   r   z&BaseComplexPrompt.total_message_length   s      !; 	& C$4$44  A% C$6$6 7 77!C(9$:$: ; ;; 	& A% ##r!   c                 H    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.
        r=   )shutilget_terminal_sizer   rC   
term_widthrm   s      r"   extra_message_line_countz*BaseComplexPrompt.extra_message_line_count   s)     022
A)A-*<<r!   c                 t    | j         r0t          j                    \  }}t          | j                   dz
  |z  S dS )zint: Get the extra lines created caused by line wrapping.

        See Also:
            :attr:`.BaseComplexPrompt.extra_message_line_count`
        r=   r   )rI   r   r   r   r   s      r"   rS   z3BaseComplexPrompt.extra_long_instruction_line_count  sA     ! 	"466MJ.//!3
BB1r!   c                 2    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   rS   )rC   rh   s     r"   r   z"BaseComplexPrompt.extra_line_count  s*      	$//$88r!   )NFFr%   r%   r&   r&   NNNr'   TTTr(   N)r:   N)(r   r   r   r   r	   r   r   r   r   r   boolr   r   rK   r[   r   r   r   re   ro   r
   rt   rv   r   r   rx   rs   r}   r   r   propertyr   setterr   r   r   r   rS   r   __classcell__)rW   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 D D D D D&c & & & &2 2 2 2 ![ ! ! ! X! " " " " " " ;s ; ; ; X; $c $ $ $ X$ =# = = = X= 
3 
 
 
 X
 #    X    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  N  ! ! ! ! ! ! > > > > > > > > > > > > > > > > 2 2 2 2 2 2 , , , , , , ? ? ? ? ? ? ? ? F F F F F F $ $ $ $ $ $ 3 3 3 3 3 3 9 9 9 9 9 9                 A A A A A( A A A A Ar!   