o
     JAf"2                     @   s   	 d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ ddd	ZG d
d dejZG dd dZe  edkrSd dlmZ edddd dS dS )    N)
messagebox)idleConf)	view_text)Hovertip)macosxP   c                 C   s   	 d}d}d}d}t d| D ]?}| | }||7 }||7 }| | dkr8||kr1||d | 7 }|d7 }d}n||||  7 }||krJ|d7 }|}|d7 }q|t| | 7 }|dkre||d | 7 }|S |d8 }|S )N   r      z[\t\n]
)refinditerstartlen)sZ	linewidthZtabwidthposZ	linecountZcurrent_columnmnumchars r   '/usr/lib/python3.10/idlelib/squeezer.pycount_lines_with_wrapping   s2   
r   c                   @   sH   e Zd Z	 dd Zdd ZdddZddd	Zdd
dZdZdd Z	dS )ExpandingButtonc           
         s    | _ || _|| _|| _|j | _}|j | _}|jj| _|dkr#dnd}d| d| d}t	j
j| ||ddd	 d
}	t| |	dd | d| j t rU| d| j n| d| j |  fdd d | _| | j d S )Nr	   lineslinezSqueezed text ( z).z#FFFFC0z#FFFFE0)textZ
backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r   )Zhover_delayz<Double-Button-1>z
<Button-2>z
<Button-3>c                    s    t | t | t |  S N)int)offsetlengthr   r   r   <lambda>z   s    z*ExpandingButton.__init__.<locals>.<lambda>)r   tags
numoflinessqueezereditwinr   perbottom	base_texttkButton__init__r   bindexpandr   ZisAquaTkcontext_menu_eventZselection_handleis_dangerousZ
after_idleset_is_dangerous)
selfr   r!   r"   r#   r$   r   Zline_pluralityZbutton_textZbutton_tooltip_textr   r   r   r*   _   s0   

zExpandingButton.__init__c                    sL   d| j    | jdkp"t| jdkp"t fddtd| jD | _d S )N2   i  iP  c                 3   s"    | ]}t |d  kV  qdS )r   N)r   group).0Z
line_matchZdangerous_line_lenr   r   	<genexpr>   s
    
z3ExpandingButton.set_is_dangerous.<locals>.<genexpr>z[^\n]+)	r   Zwinfo_widthr"   r   r   anyr   r   r.   )r0   r   r4   r   r/      s   
z ExpandingButton.set_is_dangerousNc                 C   s   	 | j d u r
|   | j r)tjddg d| jt| jf tj| j	d}|s)dS | j	
| }| j|| j| j | j|  | j|| j| j | jj|  d S )NzExpand huge output?z

)z5The squeezed output is very long: %d lines, %d chars.z2Expanding it could make IDLE slow or unresponsive.z5It is recommended to view or copy the output instead.zReally expand?)titlemessagedefaultparentbreak)r.   r/   r   Zaskokcanceljoinr"   r   r   ZCANCELr   indexr'   insertr!   deleter$   Zon_squeezed_expandr#   expandingbuttonsremove)r0   eventZconfirmr=   r   r   r   r,      s&   

zExpandingButton.expandc                 C   s   	 |    | | j d S r   )Zclipboard_clearZclipboard_appendr   r0   rB   r   r   r   copy   s   zExpandingButton.copyc                 C   s   	 t | jd| jddd d S )NzSqueezed Output ViewerFZnone)Zmodalwrap)r   r   r   rC   r   r   r   view   s   
zExpandingButton.view))rD   rD   )rF   rF   c                 C   sb   | j dd|j|jf  tj| j dd}| jD ]\}}|j|t| |d q|	|j
|j dS )Nr>   z@%d,%dr   )Ztearoff)labelZcommandr;   )r   Zmark_setxyr(   ZMenurmenu_specsZadd_commandgetattrZtk_popupZx_rootZy_root)r0   rB   ZrmenurG   Zmethod_namer   r   r   r-      s   z"ExpandingButton.context_menu_eventr   )
__name__
__module____qualname__r*   r/   r,   rD   rF   rJ   r-   r   r   r   r   r   T   s    
 


r   c                   @   s2   e Zd Z	 edd Zdd Zdd Zdd Zd	S )
Squeezerc                 C   s   	 t jdddddd| _d S )NmainZPyShellzauto-squeeze-min-linesr   r1   )typer9   )r   Z	GetOptionauto_squeeze_min_lines)clsr   r   r   reload   s
   zSqueezer.reloadc                    sf   	 | _ |j  _|jj _dtdtd   _g  _d|j	f fdd	}||_	d S )N   Zborderpadxr   c                    s   |dkr	|| |S  j }t| |k r|| |S  | }||k r%|| |S t| || }dtj jd|ddd d 	  dtj
  j| d S )NstdoutZiomark      ZwindowrV   Zpady)rR   r   count_linesr   Zmark_gravityr(   ZRIGHTwindow_createZseeupdateZLEFTr@   append)r   r!   writerR   r"   expandingbuttonr0   r   r   r   mywrite   s"   




z"Squeezer.__init__.<locals>.mywrite)
r$   r   r%   r&   r'   r   ZcgetZwindow_width_deltar@   r_   )r0   r$   rb   r   ra   r   r*      s   	

zSqueezer.__init__c                 C   s   	 t || jjS r   )r   r$   width)r0   r   r   r   r   r[     s   
zSqueezer.count_linesc           	      C   s0  	 | j tj}dD ]}||v r nq
| j   dS | j |tjd \}}| j ||}t|dkrG|d dkrG| j d| }|d d }| j	
|| | |}t|||| }| j j||dd	d
 t| j}|dkr| j | j|d  d|r|d8 }|dkr| j | j|d  d|sz| j|| dS )N)rW   stderrr;   z+1cr   r
   z%s-1crX   rY   rZ   r	   >)r   	tag_namesr(   ZINSERTZbellZtag_prevrangegetr   r=   r'   r?   r[   r   r\   r@   Zcomparer>   )	r0   rg   Ztag_namer   endr   r"   r`   ir   r   r   squeeze_current_text"  s:   



zSqueezer.squeeze_current_textN)rL   rM   rN   classmethodrT   r*   r[   rk   r   r   r   r   rO      s    
?rO   __main__)rP   zidlelib.idle_test.test_squeezerrU   F)	verbosityexit)r   )r   tkinterr(   r   Zidlelib.configr   Zidlelib.textviewr   Zidlelib.tooltipr   Zidlelibr   r   r)   r   rO   rT   rL   ZunittestrP   r   r   r   r   <module>   s"    
8t 