
    _j~                        S r SSKJr  SSKrSSKrSSKJr  SSKJr  \R                  " S5      r
\R                  " S5      r " S	 S
\R                  5      r " S S\R                  5      rg)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                  h    \ rS rSr% SrS\S'   SrS\S'   SrS\S'   \SS	 j5       r	SS
 jr
SS jrSrg)HyperlinkParams   z
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                    [         R                  U5      nUc  gU " UR                  S5      UR                  S5      UR                  S5      S9$ )z
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      F/home/rurouni/.local/lib/python3.13/site-packages/wcwidth/hyperlink.pyparseHyperlinkParams.parse#   sC     ##C(9qwwqz!''!*LL    c                R    SU R                    SU R                   U R                   3$ )z(Generate the OSC 8 open escape sequence.z]8;;)r   r   r   selfs    r   	make_openHyperlinkParams.make_open4   s&    Qtxxj0ABBr   c                     SU R                    3$ )z)Generate the OSC 8 close escape sequence.z]8;;)r   r   s    r   
make_closeHyperlinkParams.make_close8   s    $//*++r    N)r   r
   returnzHyperlinkParams | Noner'   r
   )__name__
__module____qualname____firstlineno____doc____annotations__r   r   classmethodr   r!   r$   __static_attributes__r&   r   r   r   r      sA     
HFCJM M C,r   r   c                      \ rS rSr% SrS\S'   S\S'   \SS j5       rSS	S
S.       SS jjr\SSS jj5       r	SS jr
Srg)	Hyperlink=   z
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    [         R                  X5      nUc  gUR                  5       UR                  5       4$ )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.
r7   )HYPERLINK_CLOSE_REsearchstartend)r   r4   open_endr   s       r   
find_closeHyperlink.find_closeH   s3     %%d59	1557##r   r      r   control_codestabsizeambiguous_widthc               .    [        U R                  UUUS9$ )z
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@   )_widthr4   )r    rA   rB   rC   s       r   display_widthHyperlink.display_widthZ   s!    $ II'+	
 	
r   c                   [         R                  " X5      nUc  g[        R                  UR	                  5       5      nUc  gU R                  XR                  5       5      u  pVXV4S:X  a  gU " XAUR                  5       U S9$ )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')
Nr6   )r   r4   )r   r   r   r   r   r=   r;   )r   r4   r:   r   r   close_start	close_ends          r   r   Hyperlink.parses   sw     $$T19 &&qwwy1>!$eeg!>#x/&AEEGK'@AAr   c                    U R                   R                  5       U R                  -   U R                   R                  5       -   $ )z5Rebuild the complete OSC 8 hyperlink escape sequence.)r   r!   r4   r$   r   s    r   make_sequenceHyperlink.make_sequence   s0    {{$$&2T[[5K5K5MMMr   r&   N)r4   r
   r<   intr'   ztuple[int, int])rA   z+typing.Literal['parse', 'strict', 'ignore']rB   rO   rC   rO   r'   rO   )r   )r4   r
   r:   rO   r'   zHyperlink | Noner(   )r)   r*   r+   r,   r-   r.   r/   r=   rF   r   rM   r0   r&   r   r   r2   r2   =   s}     
I$ $( FM 
 C
 	

 
 

2 B B0Nr   r2   )r-   
__future__r   retypingrE   r   escape_sequencesr   compiler   r8   
NamedTupler   r2   r&   r   r   <module>rV      sh    # 
  $ 0JJLM ZZ 89 $,f'' $,NQN!! QNr   