o
    
j~                     @  sd   d dl mZ d dlmZ d dlZd dlmZ ddlm	Z	 ddlm
Z
 dddZG dd dejZdS )    )annotationsN)contextmanager   )_ansi_re)term_lentextstrnintreturnc                 C  sx   |dkrdS d}d}d}t | }||k r6t| |}|dur#| }q|d7 }|d7 }|}||kr2n||k s| d| S )zReturn the longest prefix of ``text`` containing at most ``n`` visible
    characters.

    ANSI escape sequences inside the prefix are kept intact and do not count
    toward the visible width. A cut is never placed inside an escape sequence.
    r    Nr   )lenr   matchend)r   r	   visibleicutr   m r   B/home/kuhnn/.local/lib/python3.10/site-packages/click/_textwrap.py_truncate_visible   s$   
r   c                   @  s<   e Zd ZdZdd
dZdddZedddZdddZdS )TextWrappera  ``textwrap.TextWrapper`` variant that measures widths by visible
    character count.

    ANSI escape sequences embedded in chunks, indents, or the placeholder are
    excluded from the width budget. Without this, styled help text (a styled
    ``Usage:`` prefix, a colorized option name, ...) would be wrapped earlier
    than its visible length warrants and tokens would split mid-word.
    reversed_chunks	list[str]cur_linecur_lenr
   widthr   Nonec           	      C  sf   t || d}| jr&|d }t||}|t|d  }|| ||d< d S |s1||  d S d S )Nr   )maxbreak_long_wordsr   r   appendpop)	selfr   r   r   r   
space_leftlastr   resr   r   r   _handle_long_word0   s   

zTextWrapper._handle_long_wordchunksc           	      C  s  g }| j dkrtd| j d| jdur3| jdkr| j}n| j}t|t| j  | j kr3td|  |rAg }d}|rD| j}n| j}| j t| }| j	r^|d 
 dkr^|r^|d= |r{t|d }|| |krx||  ||7 }nn|s`|rt|d |kr| |||| ttt|}| j	r|r|d 
 dkr|t|d 8 }|d= |r?| jdu st|d | jk s|r| j	rt|dkr|d 
 s||kr||d|  nb|r|d 
 r|t| j |kr|| j ||d|  	 	 |S |t|d 8 }|d= |s|r2|d  }t|t| j | j kr2|| j |d< 	 |S ||| j   	 |S |s:|S )	a  Wrap chunks counting widths in visible characters.

        Mirrors the algorithm of :meth:`textwrap.TextWrapper._wrap_chunks`
        with every width measurement routed through
        :func:`click._compat.term_len` instead of :func:`len`, so ANSI escape
        bytes in chunks, indents, or the placeholder do not inflate the count.

        .. seealso::
            :class:`textwrap.TextWrapper` in the Python standard library documentation:
            https://docs.python.org/3/library/textwrap.html#textwrap.TextWrapper

            Reference implementation in CPython:
            https://github.com/python/cpython/blob/main/Lib/textwrap.py
        r   zinvalid width z (must be > 0)Nr   z#placeholder too large for max widthr   r   )r   
ValueError	max_linessubsequent_indentinitial_indentr   placeholderlstripreversedrop_whitespacestripr!   r"   r'   summapr   joinrstrip)	r#   r(   linesindentr   r   r   r	   	prev_liner   r   r   _wrap_chunksB   s   







DzTextWrapper._wrap_chunksr7   r   cabc.Iterator[None]c                 c  sR    | j }| j}|  j |7  _ |  j|7  _zd V  W || _ || _d S || _ || _w )N)r,   r+   )r#   r7   old_initial_indentold_subsequent_indentr   r   r   extra_indent   s   
zTextWrapper.extra_indentr   c                 C  sJ   g }t | D ]\}}| j}|dkr| j}|| |  qd|S )Nr   
)	enumerate
splitlinesr,   r+   r!   r4   )r#   r   rvidxliner7   r   r   r   indent_only   s   
zTextWrapper.indent_onlyN)
r   r   r   r   r   r
   r   r
   r   r   )r(   r   r   r   )r7   r   r   r:   )r   r   r   r   )	__name__
__module____qualname____doc__r'   r9   r   r=   rD   r   r   r   r   r   &   s    
	
br   )r   r   r	   r
   r   r   )
__future__r   collections.abcabccabctextwrap
contextlibr   _compatr   r   r   r   r   r   r   r   <module>   s    
