o
    i&                     @   s  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 deZ%deddddddej&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(d)Z)dS )*    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                       s   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dZ
deded
ee f fddZ  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.
    FNr   only_directories	get_pathsfile_filtermin_input_len
expanduserreturnc                    sN   |pdd }| D ]}t j|std| dq	t j|||||d dS )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                   S   s   dgS )N. r    r    r    L/home/ubuntu/.local/lib/python3.10/site-packages/questionary/prompts/path.py<lambda>A   s    z/GreatUXPathCompleter.__init__.<locals>.<lambda>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__r    r!   r(   $   s"   

zGreatUXPathCompleter.__init__documentcomplete_eventc                 #   s~    t t| ||}|D ]0}|jd }|d d dkr9|d dd tjj }|d |f|jd< | jtjj7  _|V  qdS )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)   r-   r.   completions
completionstyled_displaydisplay_textr+   r    r!   r2   T   s   
	
z$GreatUXPathCompleter.get_completions)FNNr   F)__name__
__module____qualname____doc__boolr   r   r   strintr(   r   r   r   r	   r2   __classcell__r    r    r+   r!   r      s6    	0r    Fmessagedefaultqmarkvalidate	completerstyler   r   r   complete_stylekwargsr   c
              	      s   t |g}dttttf  f fdd}t|}|p"t|||dd}t }|jtj	dddt
fdd	}|jtjjdddt
fd
d}t|ftd||||	|d|
}|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                      s   dfdd  fgS )Nzclass:qmarkzclass:questionz {} )formatr    rC   rE   r    r!   get_prompt_tokens   s   zpath.<locals>.get_prompt_tokensT)r   r   r   r   )eagereventc                 S   sp   | j jd urd | j _d S | jj jddr6| jj jj}|tjj	r'|d d }| jj
|d | jj   d S d S )NT)
set_cursorr0   )result)current_buffercomplete_stateapprF   r-   r5   endswithr#   r$   r4   exitappend_to_history)rO   result_pathr    r    r!   
set_answer   s   zpath.<locals>.set_answerc                 S   sH   | j j}|jr
d |_|jj}|tjjs|	tjj |j
dd d S )NF)select_first)rT   rR   rS   r-   r5   rU   r#   r$   r4   insert_textstart_completion)rO   br*   r    r    r!   next_segment   s   zpath.<locals>.next_segmentzclass:answer)lexerrH   rG   	validatorrI   key_bindings)r   r   r   r?   r   r   r   addr   ControlMr   r#   r$   r4   r   r   default_bufferresetr   r   rT   )rC   rD   rE   rF   rG   rH   r   r   r   rI   rJ   merged_stylerM   r`   bindingsrY   r^   pr    rL   r!   r$   t   s8   
I 

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_COLUMNr?   r>   r$   r    r    r    r!   <module>   st    Z	
