o
    iT                  
   @   s4  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 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& e	e'eee'e'f  eee'e'eegdf f  df Z(G dd dZ)G d d! d!e)Z*G d"d# d#eZ+d$ed%ee  fd&d'Z,d(ed%dfd)d*Z-d+e+d,eg eee'e'f  f d-ed%efd.d/Z.d4d0e'd1ee' d-ed%dfd2d3Z/dS )5    N)Any)Callable)Dict)List)Optional)Sequence)Tuple)Union)PromptSession)Always)	Condition)IsDone)Keys)ConditionalContainer)FormattedTextControl)HSplit)Layout)Window)BufferControl)LayoutDimension)Style)ValidationError)	Validator)DEFAULT_SELECTED_POINTER)DEFAULT_STYLE)INDICATOR_SELECTED)INDICATOR_UNSELECTED)INVALID_INPUTc                   @   sf  e Zd ZU dZeed< 	 ee ed< 	 ee ed< 	 ee	 ed< 	 ee
ee	f  ed< ee ed< 	 					
	ddedee dee dee	 dee
ee	f  dee ddfddZede
ed eeef f dd fddZedee
ee	f  fddZejdee
ee	f  fddZejdd Zdd Zede	fddZejde	fddZejdd ZdS )Choicea  One choice in a :meth:`select`, :meth:`rawselect` or :meth:`checkbox`.

    Args:
        title: Text shown in the selection list.

        value: Value returned, when the choice is selected. If this argument
               is `None` or unset, then the value of `title` is used.

        disabled: If set, the choice can not be selected by the user. The
                  provided text is used to explain, why the selection is
                  disabled.

        checked: Preselect this choice when displaying the options.

        shortcut_key: Key shortcut used to select this item.

        description: Optional description of the item that can be displayed.
    titlevaluedisabledchecked_Choice__shortcut_keydescriptionNFTshortcut_keyreturnc                 C   sj   || _ || _|| _|d ur|nd| _|| _|d ur|| _d S t|tr0ddd |D | _d S || _d S )NF c                 S   s   g | ]}|d  qS )    ).0tokenr)   r)   N/home/ubuntu/.local/lib/python3.10/site-packages/questionary/prompts/common.py
<listcomp>c   s    z#Choice.__init__.<locals>.<listcomp>)	r!   r   r%   r"   r$   r    
isinstancelistjoin)selfr   r    r!   r"   r%   r$   r)   r)   r,   __init__P   s   	


zChoice.__init__cc              
   C   s\   t | tr| S t | trt| | S t| d| d| dd| d| d| ddS )aD  Create a choice object from different representations.

        Args:
            c: Either a :obj:`str`, :class:`Choice` or :obj:`dict` with
               ``name``, ``value``, ``disabled``, ``checked`` and
               ``key`` properties.

        Returns:
            An instance of the :class:`Choice` object.
        namer    r!   Nr"   keyr$   )r.   r   strget)r3   r)   r)   r,   buildg   s   




zChoice.buildc                 C      | j S )zA shortcut key for the choice)r#   r1   r)   r)   r,   r%      s   zChoice.shortcut_keyr5   c                 C   sF   |d urt |tr|| _d | _d S t|| _d| _d S d | _d| _d S )NFT)r.   bool_Choice__auto_shortcutr#   r6   )r1   r5   r)   r)   r,   r%      s   




c                 C   s   d | _ d| _d S )NT)r#   r<   r:   r)   r)   r,   r%      s   
c                 C   s   | j d u rdS d| j S )Nz-) z{}) )r%   formatr:   r)   r)   r,   get_shortcut_title   s   
zChoice.get_shortcut_titlec                 C   r9   )zWhether to assign a shortcut key to the choice

        Keys are assigned starting with numbers and proceeding
        through the ASCII alphabet.
        r<   r:   r)   r)   r,   auto_shortcut   s   zChoice.auto_shortcutshould_assignc                 C   s   || _ | j rd | _d S d S N)r<   r#   )r1   rA   r)   r)   r,   r@      s   
c                 C   s
   d| _ d S )NFr?   r:   r)   r)   r,   r@         
)NNFTN)__name__
__module____qualname____doc__FormattedText__annotations__r   r   r6   r;   r	   r2   staticmethodr   r8   propertyr%   setterdeleterr>   r@   r)   r)   r)   r,   r   +   s`   
 
&
r   c                       sJ   e Zd ZU dZdZeed< 	 eed< 	 d	dee ddf fddZ  Z	S )
	Separatorz%Used to space/separate choices group.z---------------default_separatorlineNr&   c                    s"   |p| j | _t | jdd dS )zCreate a separator in a list.

        Args:
            line: Text to be displayed in the list, by default uses ``---``.
        N-)rO   rP   superr2   )r1   rP   	__class__r)   r,   r2      s   zSeparator.__init__rB   )
rD   rE   rF   rG   rO   r6   rI   r   r2   __classcell__r)   r)   rS   r,   rN      s   
 $rN   c                       s  e Zd ZU g dZee ed< eee	ee
e	ef f  ed< ee ed< dZee	df ed< eed< eed< eed	< ee	 ed
< eed< eed< eed< deddddddfdeee	ee
e	ef f  deee	ee
e	ef f  d
ee	 dedededed	edeee	ee
e	ef f  def fddZdefddZdd Zdeee	ee
e	ef f  dee fddZedd Zedefdd Zd!d" Zdefd#d$Zdee	 fd%d&Zdefd'd(Zd8d)d*Zd8d+d,Zdefd-d.Zdee fd/d0Zd1e ddfd2d3Z!d8d4d5Z"d6d7 Z#  Z$S )9InquirerControl)$1234567890abr3   defghijklmnopqrstuvwxyzchoicesdefaultselected_optionsNsearch_filteruse_indicatoruse_shortcutsuse_arrow_keyspointer
pointed_atis_answeredshow_descriptionTFshow_selectedinitial_choicekwargsc
                    sD  || _ || _|| _|| _|| _|| _|| _t|tr|j	}dd |D }|d ur8||vr8||vr8t
d| d|	d u r?d }n.|	|v rI||	}n$|	|v ret|D ]\}}t|trc|j	|	krc|} nqQnt
d|	 dd| _g | _d| _d | _g | _d| _| || |   t j| jfi |
 |  st
d|	 d	d S )
Nc                 S   s   g | ]
}t |tr|jqS r)   )r.   r   r    )r*   choicer)   r)   r,   r-     s
    
z,InquirerControl.__init__.<locals>.<listcomp>z,Invalid `default` value passed. The value (`zl`) does not exist in the set of choices. Please make sure the default value is one of the available choices.z3Invalid `initial_choice` value passed. The value (`zl`) does not exist in the set of choices. Please make sure the initial value is one of the available choices.Fz!Invalid 'initial_choice' value ('z"'). It must be a selectable value.)r~   r   r   r   r   r{   r   r.   r   r    
ValueErrorindex	enumerater   rz   submission_attemptederror_messager|   found_in_search_init_choices_assign_shortcut_keysrR   r2   _get_choice_tokensis_selection_valid)r1   rz   r{   r   r~   r   r   r   r   r   r   choices_valuesr   rj   r   rS   r)   r,   r2      s`   




zInquirerControl.__init__r   c                 C   s8   t | jtr| j|k}n| j|jk}|jp|o| jd uS rB   )r.   r{   r   r    r"   )r1   r   compare_defaultr)   r)   r,   _is_selectedG  s   zInquirerControl._is_selectedc                 C   s   | j d d  }| jD ]}|jd ur'|j|v r||j q
td|j|jq
d}| jD ]}|jr>|js>|| |_|d7 }|t	|krG d S q-d S )NzInvalid shortcut '{}'for choice '{}'. Shortcuts should be single characters or numbers. Make sure that all your shortcuts are unique.r   r(   )
SHORTCUT_KEYSrz   r%   remover   r=   r   r@   r!   len)r1   available_shortcutsr3   shortcut_idxr)   r)   r,   r   N  s&   




z%InquirerControl._assign_shortcut_keysc                 C   sp   g | _ |d ur
|| _t|D ]'\}}t|}| |r#| j|j |d u r/|j	s/| | _}| j | qd S rB   )
rz   r   r   r   r8   r   r|   appendr    r!   )r1   rz   r   rh   r3   r   r)   r)   r,   r   h  s   


zInquirerControl._init_choicesc                    s>    j s jS  fdd jD }t|dk _ jr|S  jS )Nc                    s$   g | ]} j  |j v r|qS r)   )r}   lowerr   r*   r3   r:   r)   r,   r-     s    z4InquirerControl.filtered_choices.<locals>.<listcomp>r   )r}   rz   r   r   )r1   filteredr)   r:   r,   filtered_choices  s   
z InquirerControl.filtered_choicesr&   c                 C   s
   t | jS rB   )r   r   r:   r)   r)   r,   choice_count  rC   zInquirerControl.choice_countc                    s   g dt dtf fdd}t jD ]	\}}||| q  } jrI jr+| nd}|t|j	t
r7|j	n|j	d d 7 }dd	|f  joP|jd u}|r^dd
|jf  jsg|sg  S )Nr   r   c                    s  |j  jv }|  jkr' jd urdd jf nd d n jd ur1t jnd}ddd|  f t|trOd	d
|j	f n|j
rt|j	trj|r^dnddf |j	 n|rpdndd|j	f |rdndd
t|j
trdnd|j
f nn jr| nd}|r jrtd }nd}dd
|f n jrtd }nd}dd
|f t|j	trՈ|j	 n-|rdd||j	f n|  jkrdd||j	f ndd||j	f d d S )Nzclass:pointerz {} )
class:textz   )z[SetCursorPosition]r'   r(   r       zclass:separatorz{}zclass:selectedzclass:disabledz- z- {}r'   z ({})z{}{}zclass:highlightedr'   
)r    r|   r   r   r   r=   r   r.   rN   r   r!   r/   extendr;   r   r>   r~   r   r   )r   r   selectedpointer_lengthshortcut	indicatorr1   tokensr)   r,   r     sh   











z2InquirerControl._get_choice_tokens.<locals>.appendr'   r   r(   r   z  Answer: {}z  Description: {})intr   r   r   get_pointed_atr   r   r>   r.   r   r6   r   r=   r   r$   pop)r1   r   rh   r3   currentanswerr   r)   r   r,   r     s&   K
z"InquirerControl._get_choice_tokensc                 C   s   | j | j }t|tS rB   )rz   r   r.   rN   )r1   r   r)   r)   r,   is_selection_a_separator  s   
z(InquirerControl.is_selection_a_separatorc                 C   s   | j | j jS rB   )rz   r   r!   r:   r)   r)   r,   is_selection_disabled  s   z%InquirerControl.is_selection_disabledc                 C   s   |    o	|   S rB   )r   r   r:   r)   r)   r,   r     s   z"InquirerControl.is_selection_validc                 C   s   | j d | j | _ d S Nr(   r   r   r:   r)   r)   r,   select_previous     zInquirerControl.select_previousc                 C   s   | j d | j | _ d S r   r   r:   r)   r)   r,   select_next  r   zInquirerControl.select_nextc                 C   s   | j | j S rB   )r   r   r:   r)   r)   r,   r     s   zInquirerControl.get_pointed_atc                    s    fdd j D S )Nc                    s&   g | ]}t |ts|j jv r|qS r)   )r.   rN   r    r|   r   r:   r)   r,   r-   	  s    z7InquirerControl.get_selected_values.<locals>.<listcomp>)rz   r:   r)   r:   r,   get_selected_values  s   
z#InquirerControl.get_selected_valuescharc                 C   sF   |t jkr
|   n| jdu rt|| _n	|  jt|7  _d| _dS )z%Adds a character to the search filterNr   )r   	Backspaceremove_search_characterr}   r6   r   )r1   r   r)   r)   r,   add_search_character  s   



z$InquirerControl.add_search_characterc                 C   s2   | j rt| j dkr| j d d | _ d S d | _ d S )Nr(   )r}   r   r:   r)   r)   r,   r     s   
z'InquirerControl.remove_search_characterc                 C   s*   | j d u rd S dd| jrdnd| j fdgS )Nr   )class:question-markz/ zclass:search_successzclass:search_none)r   z...)r}   r   r:   r)   r)   r,   get_search_string_tokens"  s   
z(InquirerControl.get_search_string_tokens)r&   N)%rD   rE   rF   r   r   r   rI   r   r	   r6   r   r   r}   r;   r   r   r   r2   r   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r)   r)   rS   r,   rV      s   
 '	
L

	g


rV   validater&   c                    sR    r't  rt tr  S t tr S t r'G  fdddt}| S d S )Nc                       s   e Zd Z fddZdS )z(build_validator.<locals>._InputValidatorc                    s4    |j }|dur|du rt}t|t|j dd S )NTF)messagecursor_position)textr   r   r   )r1   documentverdictr   r)   r,   r   :  s   

z1build_validator.<locals>._InputValidator.validateN)rD   rE   rF   r   r)   r   r)   r,   _InputValidator9  s    r   )inspectisclass
issubclassr   r.   callable)r   r   r)   r   r,   build_validator1  s   

r   psc                 C   s,   t dd | j D }t |_t |_dS )zThis is a fix for additional empty lines added by prompt toolkit.

    This assumes the layout of the default session doesn't change, if it
    does, this needs an update.c                 s   s.    | ]}t |jtr|jjjd kr|V  qdS )DEFAULT_BUFFERN)r.   contentr   bufferr4   )r*   winr)   r)   r,   	<genexpr>M  s    
z._fix_unecessary_blank_lines.<locals>.<genexpr>N)nextlayoutfind_all_windowsr   dont_extend_heightalways_hide_cursor)r   default_buffer_windowr)   r)   r,   _fix_unecessary_blank_linesG  s
   	r   icget_prompt_tokensr   c                    s   t |fddi|}t| t fdd}t dd fddi|}tt|jjtt t	  dttt
d	t jd
|t	  @ dt|jjt fdddgS )z:Create a layout combining question and inquirer selection.reserve_space_for_menur   c                      s      d uS rB   )r   r)   r   r)   r,   has_search_stringf  s   z1create_inquirer_layout.<locals>.has_search_stringbottom_toolbarc                      s    j S rB   r   r)   r   r)   r,   <lambda>k  s    z(create_inquirer_layout.<locals>.<lambda>)filterr   )heightr   c                      s
    j d uS rB   r   r)   r   r)   r,   r   |  s   
 Nr)   )r
   r   r   r   r   r   	containerr   r   r   r   exactr   r   )r   r   r   r   r   validation_promptr)   r   r,   create_inquirer_layoutZ  sB   

r   r   stylec                 K   sV   ddl m} ddlm} |durtd|fg}nt}||d| fgfd|i| dS )u  Print formatted text.

    Sometimes you want to spice up your printed messages a bit,
    :meth:`questionary.print` is a helper to do just that.

    Example:

        >>> import questionary
        >>> questionary.print("Hello World 🦄", style="bold italic fg:darkred")
        Hello World 🦄

    .. image:: ../images/print.gif

    Args:
        text: Text to be printed.
        style: Style used for printing. The style argument uses the
            prompt :ref:`toolkit style strings <prompt_toolkit:styling>`.
    r   )print_formatted_text)rH   Nr   r   r   )prompt_toolkitr   prompt_toolkit.formatted_textrH   r   r   )r   r   r   pt_printFText
text_styler)   r)   r,   r     s   "r   rB   )0r   typingr   r   r   r   r   r   r   r	   r   r
   prompt_toolkit.filtersr   r   r   prompt_toolkit.keysr   prompt_toolkit.layoutr   r   r   r   r   prompt_toolkit.layout.controlsr   prompt_toolkit.layout.dimensionr   prompt_toolkit.stylesr   prompt_toolkit.validationr   r   questionary.constantsr   r   r   r   r   r6   rH   r   rN   rV   r   r   r   r   r)   r)   r)   r,   <module>   sl     	  l
$)