
    tni4                        d Z ddlZddlZddlmZmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZ ddlmZ ddlmZ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! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' erddl(m)Z)  G d de          Z*dS )z3Contains the base class :class:`.BaseSimplePrompt`.    N)ABCabstractmethod)	TYPE_CHECKINGAnyCallableDictListOptionalTupleUnioncast)EditingMode)	ConditionFilterOrBool)KeyBindingsKeyHandlerCallable)Keys)Style)	Validator)INQUIRERPY_KEYBOARD_INTERRUPT)RequiredKeyNotFound)InquirerPyMessageInquirerPySessionResultInquirerPyStyleInquirerPyValidate	get_style)KeyPressEventc            #          e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d4d	ed
ee         dededededee	         dedee
egef                  dee
egef                  dedededededee         ddf"dZd Zed	eddfd            Zded         ddfdZded         ddfd Zeded         ddfd!            Zedeeef         fd"            Zej        d5d#            Zdd$d%eeef         dede
egef         fd&Zed'eeef         d(eeef         deeeef                  fd)            Zedefd*            Z edefd+            Z!d6dee         defd,Z"d5d-Z#edefd.            Z$edeeef         fd/            Z%e%j        d0eeef         ddfd1            Z%edeeef         fd2            Z&e&j        d0eeef         ddfd3            Z&dS )7BaseSimplePrompta  The base class to create a simple terminal input prompt.

    Note:
        No actual :class:`~prompt_toolkit.application.Application` is created by this class.
        This class only creates some common interface and attributes that can be easily used
        by `prompt_toolkit`.

        To have a functional prompt, you'll at least have to implement the :meth:`.BaseSimplePrompt._run`
        and :meth:`.BaseSimplePrompt._get_prompt_message`.

    See Also:
        :class:`~InquirerPy.prompts.input.InputPrompt`
    NF? Invalid inputTMandatory promptmessagestylevi_modeqmarkamarkinstructionvalidateinvalid_messagetransformerfilterdefault
wrap_linesraise_keyboard_interrupt	mandatorymandatory_messagesession_resultreturnc                 *    | _         | _        |pi  _        t          |t                    s|n" t          t          |           j                   _        | _        t          |t                    s|n | j                   _        t          j
        |r|j        nt                      j                   _        | _        | _        dd dd _        t#                       _        d _        |	 _        |
 _        | _        |s"t/          t1          j        dd                    rt4          j        nt4          j         _        t          |t<                    r| _        n!t=          j         |r|nd |d           _        t1          j        d|             _!        tE           fd	           _#        d
tH          j%        igd j#        dd j#         dgd
did j#         dgd _&        d j'        igd j(        igd j)        igd _*        d S )NF)answeredresultskippedzclass:inputINQUIRERPY_VI_MODEc                     dS NT )_s    S/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/InquirerPy/base/simple.py<lambda>z+BaseSimplePrompt.__init__.<locals>.<lambda>f   s    D     T)move_cursor_to_endINQUIRERPY_NO_RAISE_KBIc                       j         S N)
_raise_kbiselfs   r>   r?   z+BaseSimplePrompt.__init__.<locals>.<lambda>m   s	     r@   keyzc-c)rH   r-   zc-dzc-z)answer	interruptskipfunc)+
_mandatory_mandatory_message_result
isinstancer   r   _message_instruction_defaultr   	from_dictdictr   _style_qmark_amark_statusr   _kb_lexer_transformer_filter_wrap_linesboolosgetenvr   VIEMACS_editing_moder   
_validatorfrom_callablerE   r   _is_rasing_kbir   Enter_kb_maps_handle_enter_handle_interrupt_handle_skip_kb_func_lookup)rG   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   s   `                r>   __init__zBaseSimplePrompt.__init__5   sZ   & $"3%+ gx007GG(h((66 	
 (%gx88SGGggdl>S>S 	 oE&Oejjy{{?OPP$)TeLL==#'% #ry)=uEEFF#KNN" 	
 h	** 	&DOO'5$8..#'  DO
 !i%+C'C
 
 
 ((?(?(?(?@@ tz*+)<==$*=)=>> U^Ut?R>R%S%ST
 
  234!4#9:;d/01 
  
r@   c                       fd} j                                         D ]W\  }}t          |t                    s|g}|D ]7} ||d         |                    dt          d                     |           8XdS )a  Register all keybindings in `self._kb_maps`.

        It's required to call this function at the end of prompt constructor if
        it inherits from :class:`~InquirerPy.base.simple.BaseSimplePrompt` or
        :class:`~InquirerPy.base.complex.BaseComplexPrompt`.
        c                     j         vrt          d d          t          | t                    s| g}  j        | d|ifd            }d S )Nzkeybinding action z
 not foundr-   c                 n    j                  D ]%} |d         | g|                    dg           R   &d S )NrL   args)kb_func_lookupget)eventmethodactionrG   s     r>   r=   zABaseSimplePrompt._keybinding_factory.<locals>._factory.<locals>._   sU    "1&9 C CF"F6N5B6::fb+A+ABBBBBC Cr@   )rs   r   rP   listregister_kb)keysr-   rw   r=   rG   s     ` r>   _factoryz6BaseSimplePrompt._keybinding_factory.<locals>._factory   s    T000)*Qv*Q*Q*QRRRdD)) vTt3F33C C C C C 43C C Cr@   rH   r-   c                      dS r;   r<   r<   r@   r>   r?   z6BaseSimplePrompt._keybinding_factory.<locals>.<lambda>   s    t r@   N)kb_mapsitemsrP   rx   rt   r   )rG   r{   rH   itemkbs   `    r>   _keybinding_factoryz$BaseSimplePrompt._keybinding_factory}   s    		C 		C 		C 		C 		C ++-- 	T 	TICdD)) v T TEBFF8Y||5L5L$M$MsSSSST	T 	Tr@   c                     dS )zhSet the error message for the prompt.

        Args:
            message: Error message to set.
        Nr<   )rG   r$   s     r>   
_set_errorzBaseSimplePrompt._set_error   	     	r@   ru   r   c                     | j         s?d| j        d<   d| j        d<   d| j        d<   |r|j                            d           dS dS |                     | j                   dS )zHandle the event when attempting to skip a prompt.

        Skip the prompt if the `_mandatory` field is False, otherwise
        show an error message that the prompt cannot be skipped.
        Tr6   r8   Nr7   r7   )r$   )rM   statusappexitr   rN   rG   ru   s     r>   rl   zBaseSimplePrompt._handle_skip   s      	=&*DK
#%)DK	"$(DK! ,	d+++++, , OOD$;O<<<<<r@   c                     d| j         d<   t          | j         d<   d| j         d<   |r"|j                            t                     dS dS )z9Handle the event when a KeyboardInterrupt signal is sent.Tr6   r7   r8   r   N)r   r   r   r   r   s     r>   rk   z"BaseSimplePrompt._handle_interrupt   sX    "&J =H!%I 	AINN"?N@@@@@	A 	Ar@   c                     dS )z:Handle the event when user attempt to answer the question.Nr<   r   s     r>   rj   zBaseSimplePrompt._handle_enter   s	     	r@   c                     | j         S )a  Dict[str, Any]: Get current prompt status.

        The status contains 3 keys: "answered" and "result".
            answered: If the current prompt is answered.
            result: The result of the user answer.
            skipped: If the prompt is skipped.
        rY   rF   s    r>   r   zBaseSimplePrompt.status   s     |r@   c                     || _         d S rD   r   rG   values     r>   r   zBaseSimplePrompt.status   s    r@   )r-   rz   c                h     t          j        d          dt          dt          f fd}|S )a  Keybinding registration decorator.

        This decorator wraps around the :meth:`prompt_toolkit.key_binding.KeyBindings.add` with
        added feature to process `alt` realted keybindings.

        By default, `prompt_toolkit` doesn't process `alt` related keybindings,
        it requires `alt-ANY` to `escape` + `ANY`.

        Args:
            keys: The keys to bind that can trigger the function.
            filter: :class:`~prompt_toolkit.filter.Condition` to indicate if this keybinding should be active.

        Returns:
            A decorator that should be applied to the function thats intended to be active when the keys
            are pressed.

        Examples:
            >>> @self.register_kb("alt-j")
            ... def test(event):
            ...     pass
        z	^alt-(.*)rL   r4   c                 ,    g }D ]l}                     |          }|r>|                    d           |                    |                    d                     W|                    |           m 	j        j        |did fd            }|S )Nescape   r-   r4   c                      |            d S rD   r<   )ru   rL   s    r>   
executablezCBaseSimplePrompt.register_kb.<locals>.decorator.<locals>.executable   s    Ur@   r4   N)matchappendgrouprZ   add)
rL   formatted_keysrH   r   r   alt_patternr-   rz   kwargsrG   s
   `    r>   	decoratorz/BaseSimplePrompt.register_kb.<locals>.decorator   s    N / /#))#.. /"))(333"))%++a..9999"))#....TX\>C&CFCC     DC r@   )recompiler   )rG   r-   rz   r   r   r   s   ```` @r>   ry   zBaseSimplePrompt.register_kb   sb    0 j..	. 	3E 	 	 	 	 	 	 	 	 	 	  r@   
pre_answerpost_answerc                    g }| j         d         rG|                    d| j        f           |                    d| j        rdnd| j        df           n| j         d         r|                    d| j        f           |                    d| j        rdnd| j        f           |                    | j        s|n$dd	|                     | j         d
                   z  f           nZ|                    d| j        f           |                    d| j        rdnd| j        f           |                    |           |S )a  Get the question message in formatted text form to display in the prompt.

        This function is mainly used to render the question message dynamically based
        on the current status (answered or not answered) of the prompt.

        Note:
            The function requires implementation when inheriting :class:`.BaseSimplePrompt`.
            You should call `super()._get_prompt_message(pre_answer, post_answer)` in
            the implemented `_get_prompt_message`.

        Args:
            pre_answer: The message to display before the question is answered.
            post_answer: The information to display after the question is answered.

        Returns:
            Formatted text in list of tuple format.
        r8   zclass:skipped r!   r6   zclass:answermarkzclass:answered_questionzclass:answerz %sr7   zclass:questionmarkzclass:question)r   r   rW   rQ   rX   r\   )rG   r   r   display_messages       r>   _get_prompt_messagez$BaseSimplePrompt._get_prompt_message   s   * ;y! 	/""OT[#ABBB"" DK-GSSR-G"WX    [$ 	/""$6#DEEE""-%)[8ccb8$--H   ""( #D--dk(.CDDD    ""$8$+#FGGG""$%)[8ccb8$--H   "":...r@   c                     dS )zAbstractmethod to enforce a run function is implemented.

        All prompt instance requires a `_run` call to initialise and run an instance of
        `PromptSession` or `Application`.
        Nr<   rF   s    r>   _runzBaseSimplePrompt._run,  r   r@   c                 
   K   dS )zAbstractmethod to enforce a run function is implemented.

        All prompt instance requires a `_run_async` call to initialise and run an instance of
        `PromptSession` or `Application`.
        Nr<   rF   s    r>   
_run_asynczBaseSimplePrompt._run_async5  s       	r@   c                     |                                  }|t          j        d|            | _        |t          k    rt
          | j        s|S |                     |          S )ax  Run the prompt and get the result.

        Args:
            raise_keyboard_interrupt: **Deprecated**. Set this parameter on the prompt initialisation instead.

        Returns:
            Value of the user answer. Types varies depending on the prompt.

        Raises:
            KeyboardInterrupt: When `ctrl-c` is pressed and `raise_keyboard_interrupt` is True.
        NrB   )r   r`   ra   rE   r   KeyboardInterruptr]   )rG   r0   r7   s      r>   executezBaseSimplePrompt.execute>  sm     #/"$))/G+G# # DO 222##| 	M||F###r@   c                    K   |                                   d{V }|t          k    rt          | j        s|S |                     |          S )a	  Run the prompt asynchronously and get the result.

        Returns:
            Value of the user answer. Types varies depending on the prompt.

        Raises:
            KeyboardInterrupt: When `ctrl-c` is pressed and `raise_keyboard_interrupt` is True.
        N)r   r   r   r]   )rG   r7   s     r>   execute_asynczBaseSimplePrompt.execute_asyncU  s\       ((((((((222##| 	M||F###r@   c                     | j         S )z-str: Instruction to display next to question.)rR   rF   s    r>   r)   zBaseSimplePrompt.instructione  s       r@   c                     | j         S )z$Dict[str, Any]: Keybinding mappings.ri   rF   s    r>   r}   zBaseSimplePrompt.kb_mapsj  s     }r@   r   c                 &    i | j         || _         d S rD   r   r   s     r>   r}   zBaseSimplePrompt.kb_mapso  s    24=2E2r@   c                     | j         S )z5Dict[str, Any]: Keybinding function lookup mappings..rm   rF   s    r>   rs   zBaseSimplePrompt.kb_func_lookups  s     ##r@   c                 &    i | j         || _         d S rD   r   r   s     r>   rs   zBaseSimplePrompt.kb_func_lookupx  s    @$"6@%@r@   )NFr    r    r!   Nr"   NNr!   TTTr#   Nr   rD   )'__name__
__module____qualname____doc__r   r
   r   r_   strr   r   r   r   rn   r   r   r   rl   rk   rj   propertyr   r   setterr   r   r   r   ry   r   r	   r   r   r   r   r   r)   r}   rs   r<   r@   r>   r   r   &   s        " ,015.6:15)-!3<@#F
 F
"F
 (F
 	F

 F
 F
 F
 -.F
 F
 hucz23F
 3%*-.F
 F
 F
 #'F
 F
  !F
" !!89#F
$ 
%F
 F
 F
 F
PT T T2 # $    ^=(?"; = = = = =Ax'@ AT A A A A 8O#<     ^ S#X    X ]   ] ?C* * *49%*/;*	%&(::	;* * * *X 3S/38=c3h3	eCHo	3 3 3 ^3j c    ^ #    ^$ $ $# $ $ $ $.$ $ $ $  !S ! ! ! X! c3h    X ^3T#s(^ 3 3 3 3 ^3 $S#X $ $ $ X$ ADcN At A A A A A Ar@   r   )+r   r`   r   abcr   r   typingr   r   r   r   r	   r
   r   r   r   prompt_toolkit.enumsr   prompt_toolkit.filters.baser   r   'prompt_toolkit.key_binding.key_bindingsr   r   prompt_toolkit.keysr   prompt_toolkit.styles.styler   prompt_toolkit.validationr   InquirerPy.enumr   InquirerPy.exceptionsr   InquirerPy.utilsr   r   r   r   r   (prompt_toolkit.key_binding.key_processorr   r   r<   r@   r>   <module>r      s   9 9 				 				 # # # # # # # #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 - , , , , , ? ? ? ? ? ? ? ? S S S S S S S S $ $ $ $ $ $ - - - - - - / / / / / / 9 9 9 9 9 9 5 5 5 5 5 5               GFFFFFFTA TA TA TA TAs TA TA TA TA TAr@   