o
    �õ±j¢(  ã                   @  sâ   d 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l
mZmZ ddlmZmZmZ ddlmZ d	d
lmZ e	rBd	dlmZ g d¢ZG dd„ ded�ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zQ
Margin implementations for a :class:`~prompt_toolkit.layout.containers.Window`.
é    )Úannotations)ÚABCMetaÚabstractmethod)ÚCallable)ÚTYPE_CHECKING)ÚFilterOrBoolÚ	to_filter)ÚStyleAndTextTuplesÚfragment_list_to_textÚto_formatted_text)Ú
get_cwidthé   )Ú	UIContent)ÚWindowRenderInfo)ÚMarginÚNumberedMarginÚScrollbarMarginÚConditionalMarginÚPromptMarginc                   @  s,   e Zd ZdZeddd„ƒZeddd„ƒZdS )r   z&
    Base interface for a margin.
    Úget_ui_contentúCallable[[], UIContent]ÚreturnÚintc                 C  ó   dS )a  
        Return the width that this margin is going to consume.

        :param get_ui_content: Callable that asks the user control to create
            a :class:`.UIContent` instance. This can be used for instance to
            obtain the number of lines.
        r   © ©Úselfr   r   r   ú�/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/layout/margins.pyÚ	get_width&   s   	zMargin.get_widthÚwindow_render_infor   ÚwidthÚheightr	   c                 C  s   g S )a³  
        Creates a margin.
        This should return a list of (style_str, text) tuples.

        :param window_render_info:
            :class:`~prompt_toolkit.layout.containers.WindowRenderInfo`
            instance, generated after rendering and copying the visible part of
            the :class:`~prompt_toolkit.layout.controls.UIControl` into the
            :class:`~prompt_toolkit.layout.containers.Window`.
        :param width: The width that's available for this margin. (As reported
            by :meth:`.get_width`.)
        :param height: The height that's available for this margin. (The height
            of the :class:`~prompt_toolkit.layout.containers.Window`.)
        r   ©r   r   r    r!   r   r   r   Úcreate_margin1   s   zMargin.create_marginN©r   r   r   r   ©r   r   r    r   r!   r   r   r	   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r#   r   r   r   r   r   !   s    
r   )Ú	metaclassc                   @  s2   e Zd ZdZ	dddd	„Zddd„Zddd„ZdS )r   a  
    Margin that displays the line numbers.

    :param relative: Number relative to the cursor position. Similar to the Vi
                     'relativenumber' option.
    :param display_tildes: Display tildes after the end of the document, just
        like Vi does.
    FÚrelativer   Údisplay_tildesr   ÚNonec                 C  s   t |ƒ| _t |ƒ| _d S ©N)r   r+   r,   )r   r+   r,   r   r   r   Ú__init__P   s   
zNumberedMargin.__init__r   r   r   c                 C  s   |ƒ j }tdt|› ƒd ƒS )Né   r   )Ú
line_countÚmaxÚlen)r   r   r1   r   r   r   r   V   s   zNumberedMargin.get_widthr   r   r    r!   r	   c                 C  sú   |   ¡ }d}d}|jjj}g }d }	t|jƒD ]M\}
}||	kr\|d u r#n9||krD|r5| |d|d  f¡ n'| |d|d   |¡f¡ n|rNt|| ƒd }| |d|d   |¡f¡ |}	| d¡ q|  	¡ r{|
|j
k r{| d¡ |
d7 }
|
|j
k sm|S )Nzclass:line-numberzclass:line-number.currentz%ir   z%i ©Ú Ú
)zclass:tildez~
)r+   Ú
ui_contentÚcursor_positionÚyÚ	enumerateÚdisplayed_linesÚappendÚrjustÚabsr,   Úwindow_height)r   r   r    r!   r+   ÚstyleÚstyle_currentÚcurrent_linenoÚresultÚlast_linenor9   Úlinenor   r   r   r#   Z   s6   
ÿ


þzNumberedMargin.create_marginN)FF)r+   r   r,   r   r   r-   r$   r%   ©r&   r'   r(   r)   r/   r   r#   r   r   r   r   r   F   s    
ÿ
r   c                   @  s.   e Zd ZdZddd	„Zddd„Zddd„ZdS )r   zJ
    Wrapper around other :class:`.Margin` classes to show/hide them.
    Úmarginr   Úfilterr   r   r-   c                 C  s   || _ t|ƒ| _d S r.   )rG   r   rH   )r   rG   rH   r   r   r   r/   �   s   zConditionalMargin.__init__r   r   r   c                 C  s   |   ¡ r
| j |¡S dS )Nr   )rH   rG   r   r   r   r   r   r   “   s   zConditionalMargin.get_widthr   r   r    r!   r	   c                 C  s    |r|   ¡ r| j |||¡S g S r.   )rH   rG   r#   r"   r   r   r   r#   ™   s   zConditionalMargin.create_marginN)rG   r   rH   r   r   r-   r$   r%   rF   r   r   r   r   r   Š   s
    

r   c                   @  s6   e Zd ZdZ			dddd„Zddd„Zddd„ZdS )r   zc
    Margin displaying a scrollbar.

    :param display_arrows: Display scroll up/down arrows.
    Fú^ÚvÚdisplay_arrowsr   Úup_arrow_symbolÚstrÚdown_arrow_symbolr   r-   c                 C  s   t |ƒ| _|| _|| _d S r.   )r   rK   rL   rN   )r   rK   rL   rN   r   r   r   r/   ©   s   

zScrollbarMargin.__init__r   r   r   c                 C  r   )Nr   r   r   r   r   r   r   ³   s   zScrollbarMargin.get_widthr   r   r    r!   r	   c                   sJ  |j }|j}|  ¡ }|r|d8 }z$t|jƒt|ƒ }|jt|ƒ }tt|t	d|| ƒƒƒ‰ t|| ƒ‰W n t
y?   g  Y S w d‡ ‡fdd„}	g }
|rV|
 d	| jfd
g¡ d}d}d}d}t|ƒD ]6}|	|ƒr~|	|d ƒsv|
 |df¡ n|
 |df¡ n|	|d ƒrŒ|
 |df¡ n|
 |df¡ |
 d¡ qb|r£|
 d	| jf¡ |
S )Né   r   Úrowr   r   Úboolc                   s   ˆ|   koˆˆ  kS   S )z/True if we should display a button on this row.r   )rP   ©Úscrollbar_heightÚscrollbar_topr   r   Úis_scroll_buttonÎ   s   z7ScrollbarMargin.create_margin.<locals>.is_scroll_buttonzclass:scrollbar.arrow)zclass:scrollbarr6   zclass:scrollbar.backgroundz*class:scrollbar.background,scrollbar.startzclass:scrollbar.buttonz$class:scrollbar.button,scrollbar.endú r4   )rP   r   r   rQ   )Úcontent_heightr?   rK   r3   r;   ÚfloatÚvertical_scrollr   Úminr2   ÚZeroDivisionErrorÚextendrL   Úranger<   rN   )r   r   r    r!   rW   r?   rK   Úfraction_visibleÚfraction_aboverU   rC   Úscrollbar_backgroundÚscrollbar_background_startÚscrollbar_buttonÚscrollbar_button_endÚir   rR   r   r#   ¶   sR   
ÿÿÿþÿzScrollbarMargin.create_marginN)FrI   rJ   )rK   r   rL   rM   rN   rM   r   r-   r$   r%   rF   r   r   r   r   r   ¢   s    ü

r   c                   @  s2   e Zd ZdZ	ddd	d
„Zddd„Zddd„ZdS )r   a¢  
    [Deprecated]

    Create margin that displays a prompt.
    This can display one prompt at the first line, and a continuation prompt
    (e.g, just dots) on all the following lines.

    This `PromptMargin` implementation has been largely superseded in favor of
    the `get_line_prefix` attribute of `Window`. The reason is that a margin is
    always a fixed width, while `get_line_prefix` can return a variable width
    prefix in front of every line, making it more powerful, especially for line
    continuations.

    :param get_prompt: Callable returns formatted text or a list of
        `(style_str, type)` tuples to be shown as the prompt at the first line.
    :param get_continuation: Callable that takes three inputs. The width (int),
        line_number (int), and is_soft_wrap (bool). It should return formatted
        text or a list of `(style_str, type)` tuples for the next lines of the
        input.
    NÚ
get_promptú Callable[[], StyleAndTextTuples]Úget_continuationú5None | Callable[[int, int, bool], StyleAndTextTuples]r   r-   c                 C  s   || _ || _d S r.   )re   rg   )r   re   rg   r   r   r   r/     s   
zPromptMargin.__init__r   r   r   c                 C  s   t |  ¡ ƒ}t|ƒS )z Width to report to the `Window`.)r
   re   r   )r   r   Útextr   r   r   r     s   zPromptMargin.get_widthr   r   r    r!   r	   c              	   C  sd   | j }g }| t|  ¡ ƒ¡ |r0d }|jdd … D ]}| d¡ | t|||||kƒƒ¡ |}q|S )Nr   r4   )rg   r\   r   re   r;   r<   )r   r   r    r!   rg   rC   Úlast_yr9   r   r   r   r#     s   
ÿzPromptMargin.create_marginr.   )re   rf   rg   rh   r   r-   r$   r%   rF   r   r   r   r   r   ø   s    ü
	r   N)r)   Ú
__future__r   Úabcr   r   Úcollections.abcr   Útypingr   Úprompt_toolkit.filtersr   r   Úprompt_toolkit.formatted_textr	   r
   r   Úprompt_toolkit.utilsr   Úcontrolsr   Ú
containersr   Ú__all__r   r   r   r   r   r   r   r   r   Ú<module>   s"    	%DV