a
    ze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rd dlmZ edddd dS )    N)
messagebox)idleConf)	view_text)Hovertip)macosxP   c                 C   s   d}d}d}d}t d| D ]~}| | }||7 }||7 }| | dkrn||kr`||d | 7 }|d7 }d}n$||||  7 }||kr|d7 }|}|d7 }q|t| | 7 }|dkr||d | 7 }n|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.9/idlelib/squeezer.pycount_lines_with_wrapping   s.    
r   c                   @   sF   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rFdnd}d| d| d}t	j
j| ||ddd	 d
}	t| |	dd | d| j t r| 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       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pDt| jdkpDt 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/   )r1   r   r5   r   r0      s    
z ExpandingButton.set_is_dangerousNc                 C   s   | j d u r|   | j rPtjddg d| jt| jf tj| j	d}|sPdS | 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/   r0   r   Zaskokcanceljoinr#   r   r   ZCANCELr   r(   insertindexr"   deleter$   expandingbuttonsremove)r1   eventZconfirmr   r   r   r-      s     	

zExpandingButton.expandc                 C   s   |    | | j d S r   )Zclipboard_clearZclipboard_appendr   r1   rC   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   rD   r   r   r   view   s    zExpandingButton.view))rE   rE   )rG   rG   c                 C   sb   | j dd|j|jf  tj| j dd}| jD ]\}}|j|t| |d q0|	|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)r1   rC   ZrmenurH   Zmethod_namer   r   r   r.      s    z"ExpandingButton.context_menu_event)N)N)N)
__name__
__module____qualname__r+   r0   r-   rE   rG   rK   r.   r   r   r   r   r   T   s    


r   c                   @   s0   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   r2   )typer:   )r   Z	GetOptionauto_squeeze_min_lines)clsr   r   r   reload   s    zSqueezer.reloadc                    sd   | _ |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 rJ|| |S t| || }dtj jd|ddd d 	  dtj
  j| d S )NstdoutZiomark      ZwindowrW   Zpady)rS   r   count_linesr   Zmark_gravityr)   ZRIGHTwindow_createZseeupdateZLEFTrA   append)r   r"   writerS   r#   expandingbuttonr1   r   r   r   mywrite   s"    




z"Squeezer.__init__.<locals>.mywrite)
r%   r   r&   r'   r(   r   ZcgetZwindow_width_deltarA   r`   )r1   r%   rc   r   rb   r   r+      s    

zSqueezer.__init__c                 C   s   t || jjS r   )r   r%   width)r1   r   r   r   r   r\     s    zSqueezer.count_linesc           
      C   s  | j tj}dD ]}||v r q2q| j   dS | j |tjd \}}| j ||}t|dkr|d dkr| j d| }|d d }| j	
|| | |}t|||| }| j j||dd	d
 t| j}	|	dkr| j | j|	d  d|r|	d8 }	q| j|	| dS )N)rX   stderrr<   z+1cr   r
   z%s-1crY   rZ   r[   r	   >)r   	tag_namesr)   ZINSERTZbellZtag_prevrangegetr   r?   r(   r@   r\   r   r]   rA   Zcomparer>   )
r1   rC   rh   Ztag_namer   endr   r#   ra   ir   r   r   squeeze_current_text_event   s0    	




z#Squeezer.squeeze_current_text_eventN)rM   rN   rO   classmethodrU   r+   r\   rl   r   r   r   r   rP      s
   
?rP   __main__)rQ   zidlelib.idle_test.test_squeezerrV   F)	verbosityexit)r   )r   tkinterr)   r   Zidlelib.configr   Zidlelib.textviewr   Zidlelib.tooltipr   Zidlelibr   r   r*   r   rP   rU   rM   ZunittestrQ   r   r   r   r   <module>   s   
8r 