o
    i~                     @  st   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	dZ
e	dZG d	d
 d
ejZG dd dejZdS )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                   @  sT   e Zd ZU dZded< dZded< dZded< edddZdddZ	dddZ
dS )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
terminatorseqreturnHyperlinkParams | Nonec                 C  s6   t |}|du rdS | |d|d|d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)clsr   m r   </usr/local/lib/python3.10/dist-packages/wcwidth/hyperlink.pyparse#   s   
 zHyperlinkParams.parsec                 C  s   d| j  d| j | j S )z(Generate the OSC 8 open escape sequence.z]8;;)r
   r   r   selfr   r   r   	make_open4      zHyperlinkParams.make_openc                 C  s   d| j  S )z)Generate the OSC 8 close escape sequence.z]8;;)r   r   r   r   r   
make_close8   s   zHyperlinkParams.make_closeN)r   r   r   r   r   r   )__name__
__module____qualname____doc____annotations__r
   r   classmethodr   r   r   r   r   r   r   r      s   
 
r   c                   @  s^   e Zd ZU dZded< ded< edd
dZdddddddZed d!ddZd"ddZ	dS )#	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   textopen_endintr   tuple[int, int]c                 C  s(   t ||}|du rdS | | 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.
        Nr-   )HYPERLINK_CLOSE_REsearchstartend)r   r(   r)   r   r   r   r   
find_closeH   s   zHyperlink.find_closer      r   control_codestabsizeambiguous_widthr5   +typing.Literal['parse', 'strict', 'ignore']r6   r7   c                C  s   t | j|||d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
        r4   )_widthr(   )r   r5   r6   r7   r   r   r   display_widthZ   s   zHyperlink.display_widthr   r0   Hyperlink | Nonec                 C  sn   t ||}|du rdS t| }|du rdS | || \}}||fdkr+dS | ||| | d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   r2   r1   )r   r(   r0   r   r
   close_start	close_endr   r   r   r   s   s   zHyperlink.parsec                 C  s   | j  | j | j   S )z5Rebuild the complete OSC 8 hyperlink escape sequence.)r
   r   r(   r   r   r   r   r   make_sequence   r   zHyperlink.make_sequenceN)r(   r   r)   r*   r   r+   )r5   r8   r6   r*   r7   r*   r   r*   )r   )r(   r   r0   r*   r   r;   r    )
r!   r"   r#   r$   r%   r&   r2   r:   r   r>   r   r   r   r   r'   =   s   
 r'   )r$   
__future__r   retypingr9   r   escape_sequencesr   compiler   r.   
NamedTupler   r'   r   r   r   r   <module>   s    

'