
    AHj&                        d dl 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 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 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 d dlmZ d dlm Z  d dl!m"Z" d dl#m$Z$  G d de      Z%deddddddejL                  f	de'de'de'ded ee   d!ee   d"e(d#eeg ee'   f      d$eee'ge(f      d%ed&ed'e"fd(Z)y))    N)Any)Callable)Iterable)List)Optional)Tuple)CompleteEvent)
Completion)PathCompleter)	Completer)Document)KeyBindings)KeyPressEvent)Keys)SimpleLexer)CompleteStyle)PromptSession)Style)DEFAULT_QUESTION_PREFIX)build_validator)Question)merge_styles_defaultc                        e Zd ZdZ	 	 	 	 	 ddedeeg ee   f      deeegef      de	deddf fd	Z
d
ededee   f fdZ xZS )GreatUXPathCompleterzWraps :class:`prompt_toolkit.completion.PathCompleter`.

    Makes sure completions for directories end with a path separator. Also make sure
    the right path separator is used. Checks if `get_paths` returns list of existing
    directories.
    Nonly_directories	get_pathsfile_filtermin_input_len
expanduserreturnc                     |xs d } |       D ]0  }t         j                  j                  |      r#t        d| d       t        |   |||||       y)a*  Adds validation of 'get_paths' to :class:`prompt_toolkit.completion.PathCompleter`.

        Args:
            only_directories (bool): If True, only directories will be
                returned, but no files. Defaults to False.
            get_paths (Callable[[], List[str]], optional): Callable which
                returns a list of directories to look into when the user enters a
                relative path. If None, set to (lambda: ["."]). Defaults to None.
            file_filter (Callable[[str], bool], optional): Callable which
                takes a filename and returns whether this file should show up in the
                completion. ``None`` when no filtering has to be done. Defaults to None.
            min_input_len (int): Don't do autocompletion when the input string
                is shorter. Defaults to 0.
            expanduser (bool): If True, tilde (~) is expanded. Defaults to
                False.

        Raises:
            ValueError: If any of the by `get_paths` returned directories does not
                exist.
        c                      dgS )N. r$       P/root/tools/cai/cai_env/lib/python3.12/site-packages/questionary/prompts/path.py<lambda>z/GreatUXPathCompleter.__init__.<locals>.<lambda>A   s    3% r%   zS
 Completer for file paths 'get_paths' must return only existing directories, but 'z' does not exist.)r   r   r   r   r   N)ospathisdir
ValueErrorsuper__init__)selfr   r   r   r   r   current_path	__class__s          r&   r-   zGreatUXPathCompleter.__init__$   sr    : 0-	%K 	L77==.)N*;=	 	-#'! 	 	
r%   documentcomplete_eventc              #   H  K   t         t        |   ||      }|D ]  }|j                  d   }|d   d   dk(  rd|d   dd t        j
                  j                  z   }|d   |f|j                  d<   |xj                  t        j
                  j                  z  c_        |  yw)zGet completions.

        Wraps :class:`prompt_toolkit.completion.PathCompleter`. Makes sure completions
        for directories end with a path separator. Also make sure the right path
        separator is used.
        r      /N)r,   r   get_completionsdisplayr(   r)   septext)r.   r1   r2   completions
completionstyled_displaydisplay_textr0   s          r&   r7   z$GreatUXPathCompleter.get_completionsT   s      0$Gn
 & 	J (//2N a $+-a0"5C)7):L(I
""1%
 277;;.!	s   BB")FNNr   F)__name__
__module____qualname____doc__boolr   r   r   strintr-   r   r	   r   r
   r7   __classcell__)r0   s   @r&   r   r      s     "'7;7; .
.
 HRc]34.
 hud{34	.

 .
 .
 
.
` 2?	*	 r%   r    Fmessagedefaultqmarkvalidate	completerstyler   r   r   complete_stylekwargsr    c
           
      &    t        |g      }dt        t        t        t        f      f fd}t	        |      }|xs t        |||d      }t               }|j                  t        j                  d      dt        fd       }|j                  t        j                  j                  d      dt        fd       }t        |ft        d	      ||||	|d
|
}|j                   j#                  t%        |             t'        |j(                        S )a
  A text input for a file or directory path with autocompletion enabled.

    Example:
        >>> import questionary
        >>> questionary.path(
        >>>    "What's the path to the projects version file?"
        >>> ).ask()
        ? What's the path to the projects version file? ./pyproject.toml
        './pyproject.toml'

    .. image:: ../images/path.gif

    This is just a really basic example, the prompt can be customized using the
    parameters.

    Args:
        message: Question text.

        default: Default return value (single value).

        qmark: Question prefix displayed in front of the question.
               By default this is a ``?``.

        complete_style: How autocomplete menu would be shown, it could be ``COLUMN``
                        ``MULTI_COLUMN`` or ``READLINE_LIKE`` from
                        :class:`prompt_toolkit.shortcuts.CompleteStyle`.

        validate: Require the entered value to pass a validation. The
                  value can not be submitted until the validator accepts
                  it (e.g. to check minimum password length).

                  This can either be a function accepting the input and
                  returning a boolean, or an class reference to a
                  subclass of the prompt toolkit Validator class.

        completer: A custom completer to use in the prompt. For more information,
                   see `this <https://python-prompt-toolkit.readthedocs.io/en/master/pages/asking_for_input.html#a-custom-completer>`_.

        style: A custom color and style for the question parts. You can
               configure colors as well as font types for different elements.

        only_directories: Only show directories in auto completion. This option
                          does not do anything if a custom ``completer`` is
                          passed.

        get_paths: Set a callable to generate paths to traverse for suggestions. This option
                   does not do anything if a custom ``completer`` is
                   passed.

        file_filter: Optional callable to filter suggested paths. Only paths
                     where the passed callable evaluates to ``True`` will show up in
                     the suggested paths. This does not validate the typed path, e.g.
                     it is still possible for the user to enter a path manually, even
                     though this filter evaluates to ``False``. If in addition to
                     filtering suggestions you also want to validate the result, use
                     ``validate`` in combination with the ``file_filter``.

    Returns:
        :class:`Question`: Question instance, ready to be prompted (using ``.ask()``).
    r    c                  2    dfddj                         fgS )Nzclass:qmarkzclass:questionz {} )format)rH   rJ   s   r&   get_prompt_tokenszpath.<locals>.get_prompt_tokens   s"    &)96==;Q(RSSr%   T)r   r   r   r   )eagereventc                    | j                   j                  d | j                   _        y | j                  j                   j                  d      r| j                  j                   j                  j
                  }|j                  t        j                  j                        r|d d }| j                  j                  |       | j                  j                   j                          y y )NT)
set_cursorr5   )result)current_buffercomplete_stateapprK   r1   r:   endswithr(   r)   r9   exitappend_to_history)rU   result_paths     r&   
set_answerzpath.<locals>.set_answer   s    ..:26E  /YY%%..$.?))22;;@@K##BGGKK0)#2.IINN+N.II$$668 @r%   c                 J   | j                   j                  }|j                  rd |_        |j                  j                  }|j                  t        j                  j                        s)|j                  t        j                  j                         |j                  d       y )NF)select_first)r[   rY   rZ   r1   r:   r\   r(   r)   r9   insert_textstart_completion)rU   br/   s      r&   next_segmentzpath.<locals>.next_segment   sj    II$$#Azz$$RWW[[1MM"''++&	.r%   zclass:answer)lexerrM   rL   	validatorrN   key_bindings)r   r   r   rD   r   r   r   addr   ControlMr   r(   r)   r9   r   r   default_bufferresetr   r   r[   )rH   rI   rJ   rK   rL   rM   r   r   r   rN   rO   merged_stylerS   rh   bindingsr`   rf   ps   ` `               r&   r)   r)   t   s   R (0LTtE#s(O4 T  )I 1)	I }H\\$--t\,
9- 
9 -
9 \\"''++T\*
/M 
/ +
/ %	.)%	 	A 8G,-AEE?r%   )*r(   typingr   r   r   r   r   r   prompt_toolkit.completionr	   r
   r   prompt_toolkit.completion.baser   prompt_toolkit.documentr   prompt_toolkit.key_bindingr   (prompt_toolkit.key_binding.key_processorr   prompt_toolkit.keysr   prompt_toolkit.lexersr   prompt_toolkit.shortcuts.promptr   r   prompt_toolkit.stylesr   questionary.constantsr   questionary.prompts.commonr   questionary.questionr   questionary.stylesr   r   MULTI_COLUMNrD   rC   r)   r$   r%   r&   <module>r      s-   	       3 0 3 4 , 2 B $ - 9 9 ' 9 6 ) 3U= Ut (%)!"3737$1$>$>  	
 	" E?  T#Y/0 (C5$;/0 "  r%   