
    i~                        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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                  b    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
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                    t                               |          }|dS  | |                    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   ms      X/home/piyush/.hermes/hermes-agent/venv/lib/python3.11/site-packages/wcwidth/hyperlink.pyparsezHyperlinkParams.parse#   sV     ##C((94sqwwqzz!''!**LLLL    c                4    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#    BBBtxBB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	   r   r   r   r	   )__name__
__module____qualname____doc____annotations__r   r   classmethodr   r!   r#    r   r   r   r      s           HHHFJM M M [M C C C 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	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                    t                               ||          }|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r4   )HYPERLINK_CLOSE_REsearchstartend)r   r.   r/   r   s       r   
find_closezHyperlink.find_closeH   s>     %%dH5598		15577##r   r      r   control_codestabsizeambiguous_widthr<   +typing.Literal['parse', 'strict', 'ignore']r=   r>   c               2    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
        r;   )_widthr.   )r    r<   r=   r>   s       r   display_widthzHyperlink.display_widthZ   s)    $ I'+	
 
 
 	
r   r   r7   Hyperlink | Nonec                L   t          j        ||          }|dS t                              |                                          }|dS |                     ||                                          \  }}||fdk    rdS  | |||                                |                   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')
        Nr3   )r   r.   )r   r   r   r   r   r9   r8   )r   r.   r7   r   r   close_start	close_ends          r   r   zHyperlink.parses   s     $T51194 &&qwwyy11>4!$aeegg!>!>Y#x//4s&tAEEGGK,?'@AAAAr   c                x    | j                                         | j        z   | 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[5K5K5M5MMMr   N)r.   r	   r/   r0   r   r1   )r<   r?   r=   r0   r>   r0   r   r0   )r   )r.   r	   r7   r0   r   rC   r$   )
r%   r&   r'   r(   r)   r*   r9   rB   r   rH   r+   r   r   r-   r-   =   s           III$ $ $ [$( FM 
 
 
 
 
 
2 B B B B [B0N N N N N Nr   r-   )r(   
__future__r   retypingrA   r   escape_sequencesr   compiler   r5   
NamedTupler   r-   r+   r   r   <module>rO      s     # " " " " " 
			  $ # # # # # 0 0 0 0 0 0BJLMM RZ 899 $, $, $, $, $,f' $, $, $,NQN QN QN QN QN! QN QN QN QN QNr   