o
    ip*                  #   @   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eeddddddddddfdedeeeeeeef f  deeeeeeef f  dedee dee dedededed ed!ed"ed#ed$ee d%ed&ef"d'd(Z dS ))    N)Any)Dict)Optional)Sequence)Union)Application)KeyBindings)Keys)Style)utils)DEFAULT_QUESTION_PREFIX)DEFAULT_SELECTED_POINTER)common)Choice)InquirerControl)	Separator)Question)merge_styles_defaultFTmessagechoicesdefaultqmarkpointerstyleuse_shortcutsuse_arrow_keysuse_indicatoruse_jk_keysuse_emacs_keysuse_search_filtershow_selectedshow_descriptioninstructionkwargsreturnc                    s  ss|	s|
st d|	rrt dr%|	r%tdd |D r%t d|du s/t|dkr3t drPtd	d |D }|ttjkrPt d
ttj|t|g}t|||||||d	fdd}tj	|fi |}t
   jtjdd jtjdddd }rtjD ]1\}}|jdu r|jsstd|jt|ts|jdu s|jrq fdd}|||j qfdd}fdd}rfdd}tjD ]} j|dd| q߈ jtjdd| r jtjdd|  jtjdd| |	r! jddd|  jddd| |
r8 jtjdd|  jtjdd|  jtjddfdd} tjdd  }t t!d"| |d!t"#|t!j$S )#u  A list of items to select **one** option from.

    The user can pick one option and confirm it (if you want to allow
    the user to select multiple options, use :meth:`questionary.checkbox` instead).

    Example:
        >>> import questionary
        >>> questionary.select(
        ...     "What do you want to do?",
        ...     choices=[
        ...         "Order a pizza",
        ...         "Make a reservation",
        ...         "Ask for opening hours"
        ...     ]).ask()
        ? What do you want to do? Order a pizza
        'Order a pizza'

    .. image:: ../images/select.gif

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


    Args:
        message: Question text

        choices: Items shown in the selection, this can contain :class:`Choice` or
                 or :class:`Separator` objects or simple items as strings. Passing
                 :class:`Choice` objects, allows you to configure the item more
                 (e.g. preselecting it or disabling it).

        default: A value corresponding to a selectable item in the choices,
                 to initially set the pointer position to.

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

        pointer: Pointer symbol in front of the currently highlighted element.
                 By default this is a ``»``.
                 Use ``None`` to disable it.

        instruction: A hint on how to navigate the menu.
                     It's ``(Use shortcuts)`` if only ``use_shortcuts`` is set
                     to True, ``(Use arrow keys or shortcuts)`` if ``use_arrow_keys``
                     & ``use_shortcuts`` are set and ``(Use arrow keys)`` by default.

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

        use_indicator: Flag to enable the small indicator in front of the
                       list highlighting the current location of the selection
                       cursor.

        use_shortcuts: Allow the user to select items from the list using
                       shortcuts. The shortcuts will be displayed in front of
                       the list items. Arrow keys, j/k keys and shortcuts are
                       not mutually exclusive.

        use_arrow_keys: Allow the user to select items from the list using
                        arrow keys. Arrow keys, j/k keys and shortcuts are not
                        mutually exclusive.

        use_jk_keys: Allow the user to select items from the list using
                     `j` (down) and `k` (up) keys. Arrow keys, j/k keys and
                     shortcuts are not mutually exclusive.

        use_emacs_keys: Allow the user to select items from the list using
                        `Ctrl+N` (down) and `Ctrl+P` (up) keys. Arrow keys, j/k keys,
                        emacs keys and shortcuts are not mutually exclusive.

        use_search_filter: Flag to enable search filtering. Typing some string will
                           filter the choices to keep only the ones that contain the
                           search string.
                           Note that activating this option disables "vi-like"
                           navigation as "j" and "k" can be part of a prefix and
                           therefore cannot be used for navigation

        show_selected: Display current selection choice at the bottom of list.

        show_description: Display description of current selection if available.

    Returns:
        :class:`Question`: Question instance, ready to be prompted (using ``.ask()``).
    z^Some option to move the selection is required. Arrow keys, j/k keys, emacs keys, or shortcuts.zSCannot use j/k keys with prefix filter search, since j/k can be part of the prefix.c                 s   s     | ]}t |d ddv V  qdS )shortcut_key )jkN)getattr.0c r-   N/home/ubuntu/.local/lib/python3.10/site-packages/questionary/prompts/select.py	<genexpr>   s    zselect.<locals>.<genexpr>zqA choice is trying to register j/k as a shortcut key when they are in use as arrow keys disable one or the other.Nr   z'A list of choices needs to be provided.c                 s   s    | ]
}t |tsd V  qdS )   N)
isinstancer   r*   r-   r-   r.   r/      s    zA list with shortcuts supports a maximum of {} choices as this is the maximum number of keyboard shortcuts that are available. You provided {} choices!)r   r   r   r    r!   r   initial_choicec                     s   dfdd fg}  jr6t  jtr*| dddd   jD f | S | d  jf | S rA| df | S rPrPd	rJd
nd d}nr_s_drYd
nd d}n
drdd
nd d}| d|f | S )Nzclass:qmarkzclass:questionz {} zclass:answerr&   c                 S   s   g | ]}|d  qS )r0   r-   )r+   tokenr-   r-   r.   
<listcomp>   s    z5select.<locals>.get_prompt_tokens.<locals>.<listcomp>zclass:instructionz(Use shortcuts or arrow keysz, type to filter)z(Use shortcutsz(Use arrow keys)formatis_answeredr1   get_pointed_attitlelistappendjoin)tokensinstruction_msg)icr"   r   r   r   r   r   r-   r.   get_prompt_tokens   s*   
z!select.<locals>.get_prompt_tokensTeagerc                 S   s   | j jtdd d S )Nzclass:aborting)	exceptionr   )appexitKeyboardInterrupteventr-   r-   r.   _   s   zselect.<locals>._zc{} does not have a shortcut and arrow keys for movement are disabled. This choice is not reachable.c                    s    j |dd fdd}d S )NTrA   c                    s
    _ d S N)
pointed_atrG   )ir?   r-   r.   select_choice   s   
z3select.<locals>._reg_binding.<locals>.select_choice)add)rL   keysrM   )bindingsr?   )rL   r.   _reg_binding   s   zselect.<locals>._reg_bindingc                    (         s      rd S d S rJ   )select_nextis_selection_validrG   r?   r-   r.   move_cursor_down      z select.<locals>.move_cursor_downc                    rR   rJ   )select_previousrT   rG   rU   r-   r.   move_cursor_up   rW   zselect.<locals>.move_cursor_upc                    s     | jd j d S )Nr   )add_search_characterkey_sequencekeyrG   rU   r-   r.   search_filter   s   zselect.<locals>.search_filterr'   r(   c                    s   d _ | jj  jd d S )NT)result)r7   rD   rE   r8   valuerG   rU   r-   r.   
set_answer  s   zselect.<locals>.set_answerc                 S   s   dS )zDisallow inserting other text.Nr-   rG   r-   r-   r.   other  s    zselect.<locals>.other)layoutkey_bindingsr   r-   )%
ValueErroranylensumr   SHORTCUT_KEYSr6   r   r   create_inquirer_layoutr   rN   r	   ControlQControlC	enumerater   r%   disabledRuntimeErrorr9   r1   r   string	printable	BackspaceDownUpControlNControlPControlMr   r   r   r   used_kwargs__init__)r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   real_len_of_choicesmerged_styler@   rb   rI   rL   r,   rQ   rV   rY   r]   	characterr`   ra   r-   )rP   r?   r"   r   r   r   r   r   r.   select   s   f



r|   )!ro   typingr   r   r   r   r   prompt_toolkit.applicationr   prompt_toolkit.key_bindingr   prompt_toolkit.keysr	   prompt_toolkit.stylesr
   questionaryr   questionary.constantsr   r   questionary.promptsr   questionary.prompts.commonr   r   r   questionary.questionr   questionary.stylesr   strboolr|   r-   r-   r-   r.   <module>   s   	
