
    ?~j                       d Z ddlmZ ddlZddlZddlZddlZddlmZ  ej	                    dz  dz  dz  Z
dZd	Zd
ZdZdZdZd&dZd'dZd(dZ	 	 d)d*d Zed!k    r eej                  dk    r eej        d                   ndZ eej                  d"k    r eej        d"                   ndZ eee#          D ]Z e ej        ed$%                     dS dS )+uc  
Parser for himalaya v2.0.0 `envelope list` table output.

Why this exists
---------------
v2.0.0 removed `--output json` from `envelope list`. The only output is a
fixed-width table using `┆` (U+2506) as a column separator, capped at ~177
chars total width. Long subjects get truncated mid-word and the columns after
them shift left, so naive `split("│")` gives wrong results.

This script:
  1. Parses the table with fixed-width column slicing (NOT splits).
  2. Detects truncated subjects (≥ 85 chars in the SUBJECT field).
  3. For truncated rows, fetches the full subject via `himalaya message read`.

Usage
-----
  # Print envelopes as JSON (one per line, no truncation):
  python parse-himalaya-envelope.py

  # As a library:
  from parse_himalaya_envelope import parse_envelope_table, fetch_envelopes
  envelopes = fetch_envelopes(page=1, page_size=50)
    )annotationsN)Pathz.localbinzhimalaya.exe)r      )r      )r   i   )r      )r	      )r
      textstrreturn
list[dict]c           	        g }g }|                                  D ]^}|                    d          rG|                    d          s2|                    d          sd|vrd|vr|                    |           _|D ]i}t          |          dk     r|t          d         t          d                                      dd	                              d
d	                                          }|                                s|t          d         t          d                                                  }|t          d         t          d                                                  }|t          d         t          d                                                  }|t          d         t          d                                                  }|                    |||||d           k|S )z=Parse the v2 fixed-width envelope table into a list of dicts.u   │u   ╞u   └IDSUBJECTr   r       u   ┆)idsubjectfromdatesize)
splitlines
startswithappendlenCOL_IDreplacestripisdigitCOL_SUBJECTCOL_FROMCOL_DATECOL_SIZE)	r   	envelopes
data_lineslineid_r   senderr   r   s	            :C:\Users\server\.hermes\scripts\parse_himalaya_envelope.pyparse_envelope_tabler,   .   s   IJ!! $ $??5!! 	$$//%*@*@ 	$Y^I_I_ 	$$$$)>)>d###  t99s??6!9VAY&'//r::BB5"MMSSUU{{}} 	{1~k!n45;;==x{8A;./5577x{8A;./5577x{8A;./5577
 
 	 	 	 	     r   boolc                (    t          |           dk    S )z{Heuristic: heimdall caps SUBJECT at ~90 chars; if we filled the column,
    the row is almost certainly truncated mid-word.U   )r   )r   s    r+   is_truncatedr1   J   s     w<<2r-   env_id
str | Nonec                >   t          j        t          t                    dd| gddd          }|j        dk    rdS |j                                        D ]G}|                                                    d          r|d	d         	                                c S HdS )
z?Fetch the untruncated Subject via `himalaya message read <id>`.messagereadT   capture_outputr   timeoutr   Nzsubject:   )

subprocessrunr   HIMALAYA
returncodestdoutr   lowerr   r    )r2   resultr(   s      r+   fetch_full_subjectrC   P   s    ^	X	662$  F At((** $ $::<<"":.. 	$8>>#####	$4r-   r   2   Tpageint	page_size
untruncatec                R   t          j        t          t                    dddt          |           dt          |          ddg	ddd	          }|j        d
k    rg S t          |j                  }|r6|D ]3}t          |d                   rt          |d                   }|r||d<   4|S )zFFetch a page of envelopes, optionally filling in untruncated subjects.envelopelistz--pagez--page-sizez--max-width200Tr7   r8   r   r   r   )	r<   r=   r   r>   r?   r,   r@   r1   rC   )rE   rG   rH   rB   r&   envfulls          r+   fetch_envelopesrO   ^   s     ^	X
F	3t99mS^^		 $	  F A	$V]33I * 	* 	*CC	N++ *)#d)44 *%)C	Nr-   __main__   )rE   rG   F)ensure_ascii)r   r   r   r   )r   r   r   r.   )r2   r   r   r3   )r   rD   T)rE   rF   rG   rF   rH   r.   r   r   )__doc__
__future__r   jsonrer<   syspathlibr   homer>   r   	COL_FLAGSr"   r#   r$   r%   r,   r1   rC   rO   __name__r   argvrF   rE   rG   rM   printdumps r-   r+   <module>r`      s   2 # " " " " "  				     



      49;;!E)N: 	   8       57(,    , z"s38}}q0033sx{aD$'CMMA$5$5CHQK   2IDI>>> 3 3jdj51112222	 3 3r-   