
    Dj                    z    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ZddZddZddZ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                    |dS 	 |                      t          |                    S # t          $ r t          |          cY S w xY w)Nz(none))Nameint	Exceptionr   )enum_clsr   s     h/home/thesage/.hermes/venv/lib/python3.11/site-packages/pyicloud/services/notes/rendering/debug_tools.py
_enum_namer      sV    }x}}SZZ(((   5zzs   !( AAnotepb.NoteList[Dict[str, object]]c                   | j         pd}d}g }t          | j                  D ]\  }}t          |||j                  \  }}|                    d          r|j        nd}|                    ||t          t          |dd          pd          ||t          |dd          nd|t          |dd          nd|t          |dd          nd|t          |d	d          ndt          |                    d
                    d	           |}|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pss	            r   map_attribute_runsr9      sH    >RD
C#%CD.//  Q"4ah77	T"#**->"?"?IQT

" Ha!8!8!=A>>> &b,===?A~WRd;;;SW> &-R1NPT%U%U%U> ")_d!C!C!C"&qzz2C'D'D"E"E 	
 	
 	
& J    c                   g }t          |           D ]d}|                    d          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"           fd
                    |          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   ”)r9   getr   replacer   pb	StyleType	AlignmentWritingDirectionr/   join)	r   rowsrowrawprettyst_namealignwdindents	            r   dump_runs_textrR   D   s   D!$'' 
 
"%''&//"=c#f+2 KKg&&WXw''WVU##WXw''	 	 R\377<+@+@AA2<)=)=>>+SWW5H-I-IJJ))#GZ # #M(:Z # #CDTZ # #U# #*0U# #>CU# #MOU# ## # #	
 	
 	
 	

 99T??r:   c                   g d}g }t          |           D ]}t          |d                   }||t          |          z           }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          j        |                              dd                              dd                              dd                              dd          }|                    dt          j        |           d| d| d           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&   rB   r'   z | r   z ind=r"   r    r)   r=   u   <span class=lb>⤶</span>r<   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>)r9   r   lenr   rC   r   rE   rF   rG   rH   htmlescaperD   r/   rI   )
r   palettespansrK   r4   bgrL   tipsafecontents
             r   annotate_note_runs_htmlr]   \   s   
  G E!$'' 
 
#g,S3w<<'(#''&"%%&&O3 O Os=1 O OK8H O O",(=(=>>O OEHWW_E]E]O O",(<(<==O O "-sww7J/K/KLLO O 	 KWX:;;WT677WX=>>WV:;; 	 	aC(8(8aabaaTXaaa	
 	
 	
 	
 ggennG	 
 	  	  	 r:   c                   | j         pd}t          | j                  }g }d}t          |          D ]\  }}t	          |||j                  \  }}|j        }|                    |||j        z
  |j        |t          |dd          t          |dd          t          |dd          t          |dd          t          |dd          dud		           |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/   r0   )	r   r(   mergedr3   r2   r4   mrr6   r`   s	            r   map_merged_runsrc      s    
 >RD+,,F#%C
CV$$ 
 
R!$RY77S

"RYY%c<>>$S+t<<%,S2Et%L%L!(ot!D!D")"lD"A"A"M
 
	
 	
 	
 	
 Jr:   )r   r   r   r   )r   r   r   r   )r   r   r   r   )__doc__
__future__r   rU   typingr   r   r   protobufr   rE   rendererr
   r   r   r   r9   rR   r]   rc    r:   r   <module>rj      s     # " " " " "  ' ' ' ' ' ' ' ' ' ' & & & & & & < ; ; ; ; ; ; ; ; ;   % % % %P   0* * * *Z     r:   