
    ^j~                        d Z ddlmZ ddlZddlZddlmZ ddlmZ  ej                  d      Z
 ej                  d      Z G d	 d
ej                        Z G d dej                        Zy)zC
OSC 8 hyperlink parsing and measurement.

.. versionadded:: 0.7.0
    )annotationsN   )width)_SEQUENCE_CLASSIFYz*\x1b]8;([^;]*);([^\x07\x1b]*)(\x07|\x1b\\)z\x1b]8;;(\x07|\x1b\\)c                  Z    e Zd ZU dZded<   dZded<   dZded<   edd       Zdd	Z	dd
Z
y)HyperlinkParamsz
    Parsed parameters from an OSC 8 hyperlink open sequence.

    :param url: The hyperlink URL.
    :param params: Colon-separated metadata string (often empty).
    :param terminator: Sequence terminator (``\x07`` or ``\x1b\\``).
    strurl params
terminatorc                    t         j                  |      }|y | |j                  d      |j                  d      |j                  d            S )a%  
        Parse an OSC 8 open sequence string.

        Returns ``None`` if *seq* is not a valid OSC 8 open.

        Example::

            >>> HyperlinkParams.parse('\x1b]8;;http://example.com\x07')
            HyperlinkParams(url='http://example.com', params='', terminator='\\x07')
        N   r      )r
   r   r   )HYPERLINK_OPEN_REmatchgroup)clsseqms      J/opt/ringagent/.cad-venv/lib/python3.12/site-packages/wcwidth/hyperlink.pyparsezHyperlinkParams.parse#   sC     ##C(9qwwqz!''!*LL    c                R    d| j                    d| j                   | j                   S )z(Generate the OSC 8 open escape sequence.z]8;;)r   r
   r   selfs    r   	make_openzHyperlinkParams.make_open4   s&    Qtxxj0ABBr   c                     d| j                    S )z)Generate the OSC 8 close escape sequence.z]8;;)r   r   s    r   
make_closezHyperlinkParams.make_close8   s    $//*++r   N)r   r	   returnzHyperlinkParams | Noner"   r	   )__name__
__module____qualname____doc____annotations__r   r   classmethodr   r   r!    r   r   r   r      sA     
HFCJM M C,r   r   c                  t    e Zd ZU dZded<   ded<   edd       Zddd	d
	 	 	 	 	 	 	 ddZeddd       ZddZ	y)	Hyperlinkz
    A complete OSC 8 hyperlink with target and inner text.

    :param params: Parsed open sequence parameters.
    :param text: Inner text between the open and close sequences.
    r   r   r	   textc                t    t         j                  ||      }|y|j                         |j                         fS )a  
        Find the matching OSC 8 close sequence.

        Searches 'text' starting at 'open_end', the position just past the open
        sequence.  Returns position of close sequence ``(close_start,
        close_end)`` or ``(-1, -1)`` if not found.

        Per the OSC 8 specification, terminal emulators treat hyperlinks as a
        state attribute, not as nested HTML anchors.  A close sequence closes
        the current hyperlink regardless of how many open sequences preceded it.
        r0   )HYPERLINK_CLOSE_REsearchstartend)r   r-   open_endr   s       r   
find_closezHyperlink.find_closeH   s5     %%dH59	1557##r   r      r   control_codestabsizeambiguous_widthc               4    t        | j                  |||      S )a1  
        Measure the display width of the hyperlink's inner text.

        Delegates to :func:`wcwidth.width` with the given parameters.

        Example::

            >>> hl = Hyperlink.parse('\x1b]8;;http://example.com\x07Hello\x1b]8;;\x07', 0)
            >>> hl.display_width()
            5
        r8   )_widthr-   )r   r9   r:   r;   s       r   display_widthzHyperlink.display_widthZ   s!    $ II'+	
 	
r   c                   t        j                  ||      }|yt        j                  |j	                               }|y| j                  ||j                               \  }}||fdk(  ry | |||j                         |       S )a  
        Parse a complete OSC 8 hyperlink unit from *text* at position *start*.

        Locates the open sequence, finds the matching close, and returns a
        ``Hyperlink`` containing the parsed parameters and inner text.  Returns
        ``None`` if the text at *start* is not a complete OSC 8 hyperlink.

        Example::

            >>> Hyperlink.parse('\x1b]8;;http://example.com\x07Hello\x1b]8;;\x07')
            Hyperlink(params=HyperlinkParams(url='http://example.com', ...), text='Hello')
        Nr/   )r   r-   )r   r   r   r   r   r6   r4   )r   r-   r3   r   r   close_start	close_ends          r   r   zHyperlink.parses   s     $$T519 &&qwwy1>!$aeeg!>Y#x/&tAEEGK'@AAr   c                    | j                   j                         | j                  z   | j                   j                         z   S )z5Rebuild the complete OSC 8 hyperlink escape sequence.)r   r   r-   r!   r   s    r   make_sequencezHyperlink.make_sequence   s0    {{$$&2T[[5K5K5MMMr   N)r-   r	   r5   intr"   ztuple[int, int])r9   z+typing.Literal['parse', 'strict', 'ignore']r:   rD   r;   rD   r"   rD   )r   )r-   r	   r3   rD   r"   zHyperlink | Noner#   )
r$   r%   r&   r'   r(   r)   r6   r>   r   rC   r*   r   r   r,   r,   =   s}     
I$ $( FM 
 C
 	

 
 

2 B B0Nr   r,   )r'   
__future__r   retypingr=   r   escape_sequencesr   compiler   r1   
NamedTupler   r,   r*   r   r   <module>rK      sl    # 
  $ 0BJJLM RZZ 89 $,f'' $,NQN!! QNr   