o
    iBb                     @   sf  d Z ddlZddlmZ ddlmZmZmZmZm	Z	m
Z
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mZmZmZmZ ddlmZmZmZ ddl m!Z!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: erddl;m<Z< dgZ=G dd de*Z>dS )z4Module contains the class to create a number prompt.    N)Decimal)TYPE_CHECKINGAnyCallableOptionalTupleUnioncast)Application)Buffer)	Condition)IsDone)Keys)ConditionalContainerHorizontalAlignHSplitVSplitWindow)BufferControlDummyControlFormattedTextControl)	DimensionLayoutDimension)Layout)SimpleLexer)ValidationError)BaseComplexPromptFakeDocument)InstructionWindow)ValidationWindow)INQUIRERPY_QMARK_SEQUENCE)InvalidArgument)InquirerPyDefaultInquirerPyKeybindingsInquirerPyMessageInquirerPySessionResultInquirerPyStyleInquirerPyValidate)KeyPressEventNumberPromptc                1       sv  e Zd ZdZddddddddeddddddddd	d	d	d
dfdedee dede	dedee
eef  dee
eef  dededededededee dedeeegef  deeegef  dee dededed ed!ee d"df0 fd#d$Zd%ed"eeef fd&d'ZdVd(d)Zd*ed"dfd+d,ZdVd-d.ZdVd/d0ZdVd1d2ZdVd3d4ZdWd7d8ZdVd9d:ZdXd;ee d"dfd<d=ZdWd>d?Z dVd@dAZ!dBe"d"dfdCdDZ#dBe"d"dfdEdFZ$dBe"d"dfdGdHZ%dBe"d"dfdIdJZ&e'd"efdKdLZ(e(j)dVdMdLZ(e'd"e"fdNdOZ*e'd"efdPdQZ+e+j)d%ed"dfdRdQZ+e'd"e
eee,f fdSdTZ-e-j)d%e
eee,f d"dfdUdTZ-  Z.S )Yr)   u`  Create a input prompts that only takes number as input.

    A wrapper class around :class:`~prompt_toolkit.application.Application`.

    Args:
        message: The question to ask the user.
            Refer to :ref:`pages/dynamic:message` documentation for more details.
        style: An :class:`InquirerPyStyle` instance.
            Refer to :ref:`Style <pages/style:Alternate Syntax>` documentation for more details.
        vi_mode: Use vim keybinding for the prompt.
            Refer to :ref:`pages/kb:Keybindings` documentation for more details.
        default: Set the default value of the prompt.
            You can enter either the floating value or integer value as the default.
            Refer to :ref:`pages/dynamic:default` documentation for more details.
        float_allowed: Allow decimal input. This will change the prompt to have 2 input buffer, one for the
            whole value and one for the integral value.
        min_allowed: Set the minimum value of the prompt. When the input value goes below this value, it
            will automatically reset to this value.
        max_allowed: Set the maximum value of the prompt. When the inptu value goes above this value, it
            will automatically reset to this value.
        qmark: Question mark symbol. Custom symbol that will be displayed infront of the question before its answered.
        amark: Answer mark symbol. Custom symbol that will be displayed infront of the question after its answered.
        decimal_symbol: Decimal point symbol. Custom symbol to display as the decimal point.
        replace_mode: Start each input buffer in replace mode if default value is 0.
            When typing, it will replace the 0 with the new value. The replace mode will be disabled once the value
            is changed.
        instruction: Short instruction to display next to the question.
        long_instruction: Long instructions to display at the bottom of the prompt.
        validate: Add validation to user input.
            Refer to :ref:`pages/validator:Validator` documentation for more details.
        invalid_message: Error message to display when user input is invalid.
            Refer to :ref:`pages/validator:Validator` documentation for more details.
        invalid_message: Error message to display when user input is invalid.
            Refer to :ref:`pages/validator:Validator` documentation for more details.
        transformer: A function which performs additional transformation on the value that gets printed to the terminal.
            Different than `filter` parameter, this is only visual effect and won’t affect the actual value returned by :meth:`~InquirerPy.base.simple.BaseSimplePrompt.execute`.
            Refer to :ref:`pages/dynamic:transformer` documentation for more details.
        filter: A function which performs additional transformation on the result.
            This affects the actual value returned by :meth:`~InquirerPy.base.simple.BaseSimplePrompt.execute`.
            Refer to :ref:`pages/dynamic:filter` documentation for more details.
        keybindings: Customise the builtin keybindings.
            Refer to :ref:`pages/kb:Keybindings` for more details.
        wrap_lines: Soft wrap question lines when question exceeds the terminal width.
        raise_keyboard_interrupt: Raise the :class:`KeyboardInterrupt` exception when `ctrl-c` is pressed. If false, the result
            will be `None` and the question is skiped.
        mandatory: Indicate if the prompt is mandatory. If True, then the question cannot be skipped.
        mandatory_message: Error message to show when user attempts to skip mandatory prompt.
        session_result: Used internally for :ref:`index:Classic Syntax (PyInquirer)`.

    Examples:
        >>> from InquirerPy import inquirer
        >>> result = inquirer.number(message="Enter number:").execute()
        >>> print(result)
        0
    NFr   z. ? zInvalid inputTzMandatory promptmessagestylevi_modedefaultfloat_allowedmax_allowedmin_alloweddecimal_symbolreplace_modeqmarkamarkinstructionlong_instructionvalidateinvalid_messagetransformerfilterkeybindings
wrap_linesraise_keyboard_interrupt	mandatorymandatory_messagesession_resultreturnc                    s\  t  jdDi d|d|d|d|
d|d|d|d|d	|d
|d|d|d|d|d|d| | _t fdd _| _| _d _| _d _	d _
|	 _td _td _d _|d u rpd}d _t|tr|tt||} jrttttt|} jrt|tst|tstt j dnt|tstt j d| _|d u ri }ddid j dd jdgdd id! j dd" jdgdd#id$ j dd% jdgdd&id' j dd( jdgdd)igdtjidd*igd+d, td-D dd.igd/| _ d0 j!igd0 j"igd0 j#igd0 j$igd0 j%igd0 j&igd0 j'igd0 j(igd1 _) *tj+d2d3 }d4 _,t- j. j/d5 _0d4 _1t- j2 j/d5 _3t4 j5slt67d4nd t8 j0t9d6d7 fd8ddd9 _:t4 j5st67d4nd t8 j3t9d6d7 fd:dd; _;t<t=t>t4 j5st67d4nd t? j@ j5ddd<tA j:tB  d=tAt4 j5st67d4nd t?d> jfg j5ddd< jtB  @ d=tA j; jtB  @ d=gtCjDd?tAt4tE d@tB   jF@ d=tG jH jItB  @  j5dAtJ jKtB   jF@  j5dBg _L j: _MtN jL jO jP jQ jRdC _Sd S )ENr,   r-   r.   r5   r6   r;   r<   r:   r9   r7   r8   r>   r?   r@   rA   rB   c                      s    j S N)_float selfrF   V/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/InquirerPy/prompts/number.py<lambda>   s    z'NumberPrompt.__init__.<locals>.<lambda>zRemove any non-integer valueFz^(0*)[0-9]+.*z^.*E-.*r   Tz: argument 'default' should return type of float or Decimalz- argument 'default' should return type of intkeydownzc-n)rK   r<   jupzc-pkleftzc-bhrightzc-fl.zs-tabc                 S   s   g | ]}d t |iqS )rK   )str).0irF   rF   rI   
<listcomp>   s    z)NumberPrompt.__init__.<locals>.<listcomp>
   -)rL   rN   rP   rR   dotfocusinputnegative_togglefunc)rL   rN   rP   rR   r\   r]   r^   r[   c                 S   s   d S rD   rF   )_rF   rF   rI   r`      s   z NumberPrompt.__init__.<locals>._   )on_text_changedon_cursor_position_changedzclass:input)bufferlexerc                         t  j j jdS N)minmax	preferred)r   _whole_widthrF   rG   rF   rI   rJ      
    )heightcontentwidthdont_extend_widthc                      rf   rg   )r   _integral_widthrF   rG   rF   rI   rJ     rl   )rm   rn   ro   )rm   rn   r>   dont_extend_heightrp   )r<   r+   )align)rn   )r:   r<   r>   )r,   r<   r>   )layoutr-   key_bindingsafter_renderediting_moderF   )Tsuper__init__rE   r   	_is_float_max_min_value_error_message_decimal_symbol_whole_replace_integral_replace_replace_moderecompile_leading_zero_pattern_sn_pattern_no_default
isinstancer   r	   r   rU   floatintr!   type__name___default_is_vim_editr   Tabrangekb_maps_handle_down
_handle_up_handle_left_handle_right_handle_focus_handle_input_handle_negative_toggle_handle_dotkb_func_lookupregister_kbr   rk   r   _on_whole_text_change_on_cursor_position_change_whole_bufferrq   _on_integral_text_change_integral_bufferr   _wrap_linesr   exactr   r   _whole_window_integral_windowr   r   r   r   _get_prompt_messager   r   r   LEFTr   _is_displaying_long_instructionr   _get_error_message_is_invalidr   _long_instruction_layoutr\   r
   _style_kb_after_render_editing_mode_application)rH   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   r`   	__class__rG   rI   ry   i   sp  	


















	
 
5zNumberPrompt.__init__valuec                 C   s:   | d\}}d}t|d d  |dd }||fS )zFix sciencetific notation format.

        Args:
            value: Value to fix.

        Returns:
            A tuple of whole buffer text and integral buffer text.
        zE-0ra   rT   r+   )splitr   replace)rH   r   rP   rR   whole_buffer_textintegral_buffer_textrF   rF   rI   _fix_snI  s   	 zNumberPrompt._fix_snc                 C   s   | j rdS | jst| j| j_d| j_n(| jt| jdu r*t| j	d\}}n
| 
t| j\}}|| j_|| j_t| jj| j_t| jj| j_| jrk| jjdkr\d| _d| j_| jjdkrmd| _d| j_dS dS dS )z<Additional processing to adjust buffer content after render.Nr   rT   Tr   )r   rE   rU   r   r   textr   r   matchr   r   lencursor_positionr   r   r   )rH   r`   r   r   rF   rF   rI   _on_renderedW  s.   
zNumberPrompt._on_rendered	incrementc                 C   s0  | j rd| _ | j jd7  _zxd}| j| jkr(| j| jj}|dur(|d}t| jj}| jjs7d}d}n0|sW| j| jkrJt	| jjdkrJW dS |t
t	| jjd  }n|t
t	| jjd  }t|}| jj| | }|| j_|| j_| jj|kr|| j_W dS W dS  ty   | j| jd Y dS w )zHandle number increment and decrement.

        Additional processing to handle leading zeros in integral buffer
        as well as SN notation.

        Args:
            increment: Indicate if the operation should increment or decrement.
        Fra   r+   Nr   r   )r,   )buffer_replacefocus_bufferr   r   r   r   r   groupr   r   rU   
ValueError
_set_errorr}   )rH   r   leading_zeroszeroscurrent_text_len	next_textnext_text_lendesired_positionrF   rF   rI   _handle_numberr  s<   	
zNumberPrompt._handle_numberc                 C      | j dd dS )zHandle down key press.Fr   Nr   rH   r`   rF   rF   rI   r        zNumberPrompt._handle_downc                 C   r   )zHandle up key press.Tr   Nr   r   rF   rF   rI   r     r   zNumberPrompt._handle_upc                 C   s>   d| _ | j| jkr| jjdkr| j| _dS | j jd8  _dS )zHandle left key press.

        Move to the left by one cursor position and focus the whole window
        if applicable.
        Fr   ra   N)r   r\   r   r   r   r   r   rF   rF   rI   r     s
   zNumberPrompt._handle_leftc                 C   sL   d| _ | j| jkr| jjt| jjkr| jr| j| _dS | j jd7  _dS )zHandle right key press.

        Move to the right by one cursor position and focus the integral window
        if applicable.
        Fra   N)	r   r\   r   r   r   r   r   rE   r   r   rF   rF   rI   r     s   zNumberPrompt._handle_righteventr(   c              
   C   s   | j s
| jjs
d}n| j r| jjs| jjsd}nt| j}zt|}| j| W n t	yC } z| 
t| W Y d}~dS d}~ww d| jd< || jd< |jj|d dS )z/Handle enter event and answer/close the prompt.r+   NTansweredresult)r   )rE   r   r   r   rU   r   r   
_validatorr9   r   r   statusappexit)rH   r   r   fake_documenterF   rF   rI   _handle_enter  s(   


zNumberPrompt._handle_enterc                 C   s   |  || j dS )z-Focus the integral window if `float_allowed`.N)r   r   r   rF   rF   rI   r     s   zNumberPrompt._handle_dotwindowc                 C   s@   | j sdS |dur|| _dS | j| jkr| j| _dS | j| _dS )z1Focus either the integral window or whole window.N)rE   r\   r   r   )rH   r`   r   rF   rF   rI   r     s   zNumberPrompt._handle_focusc                 C   sH   | j rd| _ |jd j| j_| j jd7  _dS | j|jd j dS )zHandle user input of numbers.

        Buffer will start as replace mode if the value is zero, once
        cursor is moved or content is changed, disable replace mode.
        Fr   ra   N)r   key_sequencedatar   r   r   insert_text)rH   r   rF   rF   rI   r     s
   zNumberPrompt._handle_inputc                 C   s   | j jdkrd| j _dS | j jdr5| j jt| j jk }| j jdd | j _|r3| j  jd8  _dS dS | j jdk}d| j j | j _|rP| j  jd7  _dS dS )zYToggle negativity of the prompt value.

        Force the `-` sign at the start.
        rZ   r   Nra   r   )r   r   
startswithr   r   )rH   r`   move_cursorrF   rF   rI   r     s    z$NumberPrompt._handle_negative_togglerd   c                 C      t |jd | _| | dS z'Handle event of text changes in buffer.ra   N)r   r   rk   _on_text_changerH   rd   rF   rF   rI   r        z"NumberPrompt._on_whole_text_changec                 C   r   r   )r   r   rq   r   r   rF   rF   rI   r     r   z%NumberPrompt._on_integral_text_changec                 C   sF   d| _ |jr|jdkr| j| _|jdr|jdkr!d|_dS dS dS )z=Disable replace mode and fix cursor position on text changes.FrZ   r   ra   N)r   r   r   r   r   r   rF   rF   rI   r     s   
zNumberPrompt._on_text_changec                 C   s*   | j jdr|jdkrd|_dS dS dS )z'Fix cursor position on cursor movement.rZ   r   ra   N)r   r   r   r   r   rF   rF   rI   r     s   
z'NumberPrompt._on_cursor_position_changec                 C      | j | jkr	| jS | jS )z"bool: Current buffer replace mode.r   r   r   r   rG   rF   rF   rI   r   $     zNumberPrompt.buffer_replacec                 C   s    | j | jkr|| _d S || _d S rD   r   rH   r   rF   rF   rI   r   ,  s   

c                 C   r   )z Buffer: Current editable buffer.)r\   r   r   r   rG   rF   rF   rI   r   3  r   zNumberPrompt.focus_bufferc                 C   s   | j S )zWindow: Current focused window.)_focusrG   rF   rF   rI   r\   ;  s   zNumberPrompt.focusc                 C   s   || _ | j| j  d S rD   )r   r   r\   r   rF   rF   rI   r\   @  s   c                 C   sb   z| j st| jjW S t| jj d| jjr| jjnd W S  ty0   | | j | j	 Y S w )zfUnion[int, float]: The actual value of the prompt, combining and transforming all input buffer values.rT   r   )
rE   r   r   r   r   r   r   r   r}   r   rG   rF   rF   rI   r   E  s    
zNumberPrompt.valuec                 C   s   | j d urt|| js| j ntt| j }| jd ur*t|| js"| jntt| j}| js5t|| j_d S | j	
t|d u rIt|d\}}n	| t|\}}| jjrZ|| j_| jjrd|| j_d S d S )NrT   )r|   ri   rE   r   rU   r{   rh   r   r   r   r   r   r   r   )rH   r   r   r   rF   rF   rI   r   S  s$   

)rC   N)r   r(   rC   NrD   )/r   
__module____qualname____doc__r    r$   r   r&   boolr"   r   r   r   rU   r'   r   r   r#   r%   ry   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   setterr   r\   r   r   __classcell__rF   rF   r   rI   r)   0   s    ;	
 a

+






&)?r   r   decimalr   typingr   r   r   r   r   r   r	   &prompt_toolkit.application.applicationr
   prompt_toolkit.bufferr   prompt_toolkit.filters.baser   prompt_toolkit.filters.clir   prompt_toolkit.keysr    prompt_toolkit.layout.containersr   r   r   r   r   prompt_toolkit.layout.controlsr   r   r   prompt_toolkit.layout.dimensionr   r   prompt_toolkit.layout.layoutr   prompt_toolkit.lexers.baser   prompt_toolkit.validationr   InquirerPy.base.complexr   r   !InquirerPy.containers.instructionr    InquirerPy.containers.validationr   InquirerPy.enumr    InquirerPy.exceptionsr!   InquirerPy.utilsr"   r#   r$   r%   r&   r'   (prompt_toolkit.key_binding.key_processorr(   __all__r)   rF   rF   rF   rI   <module>   s2    $ 	