o
    Vh                     @  s   d dl mZ d dlZd dlZ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 er>d d	lmZ e
ege	f Ze
ege	f ZG d
d dZdS )    )annotationsN)Path)TYPE_CHECKING)Any)Callable)Style)StreamOutput)IOc                   @  s   e Zd ZdZd>d?d	d
Zed@ddZedAddZedBddZedCddZ	dDddZ
dEdFddZdGddZdHd d!ZdId$d%ZdJd(d)ZdJd*d+ZdKd,d-ZdLd0d1ZdMd2d3ZdMd4d5ZdNd8d9ZdMd:d;ZdDd<d=ZdS )OQuestionz5
    A question that will be asked in a Console.
    Nquestionstrdefaultr   returnNonec                 C  sB   || _ || _d | _d| _d| _g | _dd | _dd | _d| _d S )NFTc                 S     | S N sr   r   d/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/cleo/ui/question.py<lambda>$       z#Question.__init__.<locals>.<lambda>c                 S  r   r   r   r   r   r   r   r   %   r   zValue "{}" is invalid)		_question_default	_attempts_hidden_hidden_fallback_autocomplete_values
_validator_normalizer_error_message)selfr   r   r   r   r   __init__   s   


zQuestion.__init__c                 C     | j S r   )r   r!   r   r   r   r   (      zQuestion.questionc                 C  r#   r   )r   r$   r   r   r   r   ,   r%   zQuestion.default	list[str]c                 C  r#   r   )r   r$   r   r   r   autocomplete_values0   r%   zQuestion.autocomplete_values
int | Nonec                 C  r#   r   r   r$   r   r   r   max_attempts4   r%   zQuestion.max_attemptsboolc                 C  r#   r   )r   r$   r   r   r   	is_hidden8   s   zQuestion.is_hiddenThiddenc                 C  s    |du r| j rtd|| _d S )NT/A hidden question cannot use the autocompleter.)r   RuntimeErrorr   )r!   r-   r   r   r   hide;   s   
zQuestion.hider'   c                 C  s   |   rtd|| _d S )Nr.   )r,   r/   r   )r!   r'   r   r   r   set_autocomplete_valuesA   s   
z Question.set_autocomplete_valuesattemptsc                 C  
   || _ d S r   r)   )r!   r2   r   r   r   set_max_attemptsG      
zQuestion.set_max_attempts	validator	Validatorc                 C  r3   r   )r   )r!   r6   r   r   r   set_validatorJ   r5   zQuestion.set_validatorior	   c                   s$      sjS  fdd S )0
        Asks the question to the user.
        c                     s
     S r   )_do_askr   r9   r!   r   r   r   S   s   
 zQuestion.ask.<locals>.<lambda>)is_interactiver   _validate_attemptsr!   r9   r   r<   r   askM   s   zQuestion.askc                 C  s   |  | | jr|  s/d}|  r'z| |}W n ty&   | js$ Y nw |s.| |}n| |}t	|dkr=| j
}| |S )r:   Nr   )_write_promptr   _has_stty_availabler,   _get_hidden_responser/   r   _read_from_input_autocompletelenr   r   r!   r9   retr   r   r   r;   U   s$   



zQuestion._do_askc                 C  s   | d| j d dS )z.
        Outputs the question prompt.
        z
<question>z</question> N)write_errorr   r?   r   r   r   rA   o   s   zQuestion._write_prompterror	Exceptionc                 C  s   | d|d dS )z+
        Outputs an error message.
        z<error>z</error>N)write_error_line)r!   r9   rJ   r   r   r   _write_erroru   s   zQuestion._write_errorc              
   C  sr  | j }d}d}d}t|}t|}tdgd}|jjd| tddg	 
d	}	tg d
 z	 |d}
|
dkrf|dkrN|dkrN|d8 }|d |dkr]d}t|}t|}nd}|d| }n|
dkr|
|d7 }
|
d dks}|
d dkr|
d dkr|dkrd}|dkrq3||
d dkrdnd7 }|| | }n[t|
dk r|
dv r|dkr|dkr|| }|||d  t|}|
d	kr||
 nWd}q3||
 ||
7 }|d7 }d}d}|D ]}||r|t|kr|d7 }|||d < q|d |dkr$|dkr$|d |d|| |d  d  |d q4W td|	 g |S td|	 g w )z*
        Autocomplete a question.
         r   reverse)optionshlsttyz-g
)rS   z-icanonz-echoT   z[1DN   AB    )	rT   z[Kz7z<hl>z</hl>z8)r   listrF   r   error_output	formatter	set_style
subprocesscheck_outputdecoderstripreadrI   ord
startswithcall)r!   r9   autocompleterH   iofsmatchesnum_matchesstyle	stty_modecvaluer   r   r   rE   {   s   







+zQuestion._autocompletec                 C  s&   d}t |jtr|jj}tjd|dS )z3
        Gets a hidden response from user.
        NrN   )stream)
isinstancer^   r   rr   getpass)r!   r9   rr   r   r   r   rC      s   zQuestion._get_hidden_responseinterviewerCallable[[], Any]c              
   C  s   d}| j }|du s|r?|dur| || z| | W S  ty0 } z|}W Y d}~nd}~ww |dur9|d8 }|du s|s|sCJ |)z'
        Validates an attempt.
        NrU   )r   rM   r   rK   )r!   ru   r9   rJ   r2   er   r   r   r>      s    zQuestion._validate_attemptsc                 C  s   | d}|std| S )z"
        Read user input.
        i   Aborted)	read_liner/   striprG   r   r   r   rD      s   
zQuestion._read_from_inputc              	   C  s   t tjd,}ztjdg||d}W n ty   d}Y nw W d    |dkS W d    |dkS 1 s7w   Y  |dkS )NwrS   )stdoutstderrrX   r   )r   osdevnullopenra   rh   rK   )r!   r   	exit_coder   r   r   rB     s   

zQuestion._has_stty_availabler   )r   r   r   r   r   r   )r   r   )r   r   )r   r&   )r   r(   )r   r+   )T)r-   r+   r   r   )r'   r&   r   r   )r2   r(   r   r   )r6   r7   r   r   )r9   r	   r   r   )r9   r	   r   r   )r9   r	   rJ   rK   r   r   )r9   r	   r   r   )ru   rv   r9   r	   r   r   )__name__
__module____qualname____doc__r"   propertyr   r   r'   r*   r,   r0   r1   r4   r8   r@   r;   rA   rM   rE   rC   r>   rD   rB   r   r   r   r   r
      s2    









f
	
r
   )
__future__r   rt   r~   ra   pathlibr   typingr   r   r   cleo.formatters.styler   cleo.io.outputs.stream_outputr   
cleo.io.ior	   r   r7   
Normalizerr
   r   r   r   r   <module>   s    