o
    l>j                     @  s   d Z ddlmZ ddlZddlmZmZmZ ddlm	Z
 ddlmZmZmZ dddZdddZdddZdddZdddZdS )z
Debug helpers for mapping AttributeRuns to the exact text slices they cover.

These utilities are intended for troubleshooting renderer issues. They do not
perform any network I/O and can be safely used in tests.
    )annotationsN)DictListOptional   )	notes_pb2   )StyleSig_merge_runs_slice_for_runvalueOptional[int]returnstrc                 C  s8   |d u rdS z|  t|W S  ty   t| Y S w )Nz(none))Nameint	Exceptionr   )enum_clsr    r   b/home/thesage/.local/lib/python3.10/site-packages/pyicloud/services/notes/rendering/debug_tools.py
_enum_name   s   r   notepb.NoteList[Dict[str, object]]c           	      C  s   | j pd}d}g }t| jD ]]\}}t|||j\}}|dr#|jnd}|||tt	|ddp1d||dur=t	|ddnd|durHt	|ddnd|durSt	|ddnd|dur^t	|d	dndt
|d
d	 |}q|S )az  Return a list of dictionaries mapping each AttributeRun to its text.

    Each dict contains:
      - index: run index
      - utf16_start: start offset in UTF-16 code units
      - utf16_len: run.length
      - text: Python string slice for the run
      - style_type, alignment, writing_direction, indent_amount
      - has_attachment: whether run carries attachment_info
     r   paragraph_styleNlength
style_type	alignmentwriting_direction_paragraphindent_amountattachment_info	indexutf16_start	utf16_lentextr   r   writing_directionr    has_attachment)	note_text	enumerateattribute_runr   r   HasFieldr   appendr   getattrbool)	r   r&   posoutidxrsegpos2psr   r   r   map_attribute_runs   s4   
r7   c           	      C  s   g }t | D ]n}|ddurt|d nd}|dddddd	d
d}ttj|d}ttj|d}ttj|d}|d}|	d|d dd|d dd|d dd|dd|dd|dd|d d!| d" qd
|S )#zEReturn a human-readable dump of runs with escaped whitespace markers.r&   Nr   
u   ⏎
    u   ⤶
 u   ␀   ￼z{OBJ}r   r   r'   r    [r#   03dz] off=r$   z<5 len=r%   z<4z style=z<26z indent=z<2z align=z<16z wd=z<8u	    text=“u   ”)r7   getr   replacer   pb	StyleType	AlignmentWritingDirectionr-   join)	r   rowsrowrawprettyst_namealignwdindentr   r   r   dump_runs_textD   s4   

&
rN   c           
      C  s  g d}g }t | D ]r}t|d }||t|  }t|dd}d| d|d  d|d	  d
ttj|d d|d d
ttj|d d
ttj	|d }t
|dddddddd}|dt
| d| d| d q
d|}	d|	 dS )zReturn a small HTML page highlighting each run in a different color.

    Hover tooltips include run index, offsets, and style information.
    )z#FFF3CDz#D1ECF1z#F8D7DAz#D4EDDAz#E2E3E5r#   r&   r   zrun z | off=r$   r>   r%   z | r   z ind=r    r   r'   r9   u   <span class=lb>⤶</span>r8   u   <span class=lb>⏎</span>r;   z<span class=obj>{OBJ}</span>r:   u   <span class=null>␀</span>z<span class="run" title="z" style="background:z">z</span>a6  <!doctype html><meta charset="utf-8"><style>body{font-family:-apple-system,Segoe UI,Roboto,Helvetica,Arial,sans-serif;line-height:1.5}.run{padding:0 .15em;margin:.05em;border-radius:.2em}.lb{color:#888} .obj{color:#960} .null{color:#c00}pre{white-space:pre-wrap;border:1px solid #eee;padding:.5em}</style><pre>z</pre>)r7   r   lenr   r?   r   rA   rB   rC   rD   htmlescaper@   r-   rE   )
r   palettespansrG   r2   bgrH   tipsafecontentr   r   r   annotate_note_runs_html\   s<   
rX   c           	      C  s   | j pd}t| j}g }d}t|D ]9\}}t|||j\}}|j}||||j |j|t|ddt|ddt|ddt|ddt|dddud		 q|S )
zSame as map_attribute_runs, but after the renderer's run merge step.

    Useful to understand how the renderer will chunk paragraphs.
    r   r   r   Nr   r'   r    
attachmentr"   )	r)   r
   r+   r*   r   r   sigr-   r.   )	r   r&   mergedr1   r0   r2   mrr4   rZ   r   r   r   map_merged_runs   s(   





r]   )r   r   r   r   )r   r   r   r   )r   r   r   r   )__doc__
__future__r   rP   typingr   r   r   protobufr   rA   rendererr	   r
   r   r   r7   rN   rX   r]   r   r   r   r   <module>   s    

	
(
-