B
    v9a6                 @   s   d dl Z 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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)Font)idleConf)	view_text)Hovertip)macosxP   c             C   s   d}d}d}d}xt d| D ]~}| | }||7 }||7 }| | dkrp||krb||d | 7 }|d7 }d}n$||||  7 }||kr|d7 }|}|d7 }qW |t| | 7 }|dkr||d | 7 }n|d8 }|S )N   r      z[\t\n]
)refinditerstartlen)s	linewidthZtabwidthposZ	linecountZcurrent_columnmnumchars r   /usr/lib/python3.7/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*ExpandingButton.__init__.<locals>.<lambda>)r   tags
numoflinessqueezereditwinr   perbottom	base_texttkButton__init__r   Z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*   a   s*    

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)dangerous_line_lenr   r   	<genexpr>   s   z3ExpandingButton.set_is_dangerous.<locals>.<genexpr>z[^\n]+)	r   winfo_widthr"   r   r   anyr   r   r-   )r/   r   )r3   r   r.      s    

z ExpandingButton.set_is_dangerousNc             C   s   | j d kr|   | j rTtjddddddg| jt| jf tj| j	d}|sTd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-   r.   tkMessageBoxZaskokcanceljoinr"   r   r   ZCANCELr   r'   insertindexr!   deleter#   expandingbuttonsremove)r/   eventZconfirmr   r   r   r+      s$    	

zExpandingButton.expandc             C   s   |    | | j d S )N)Zclipboard_clearZclipboard_appendr   )r/   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   )r/   rC   r   r   r   view   s    zExpandingButton.view))rD   rD   )rF   rF   c             C   sf   | j dd|j|jf  tj| j dd}x&| jD ]\}}|j|t| |d q2W |	|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)r/   rC   ZrmenurG   Zmethod_namer   r   r   r,      s    z"ExpandingButton.context_menu_event)N)N)N)
__name__
__module____qualname__r*   r.   r+   rD   rF   rJ   r,   r   r   r   r   r   V   s    



r   c               @   sD   e Zd ZdZedd Zdd Zdd Zdd	 Zd
d Z	dd Z
dS )SqueezerNc             C   s<   t jdddddd| _| jd k	r8|  }|d k	r8|  d S )NmainZPyShellzauto-squeeze-min-linesr   r0   )typer9   )r   Z	GetOptionauto_squeeze_min_lines_instance_weakref	load_font)clsinstancer   r   r   reload   s    
zSqueezer.reloadc                sx   | _ |j  _|jj _t t_ 	  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      )windowrY   pady)rR   r   count_linesr   Zmark_gravityr(   ZRIGHTwindow_createZseeupdateZLEFTrA   append)r   r!   writerR   r"   expandingbutton)r/   r   r   r   mywrite  s     





z"Squeezer.__init__.<locals>.mywrite)r$   r   r%   r&   r'   weakrefrefrO   rS   rT   r   cgetwindow_width_deltarA   rc   )r/   r$   re   r   )r/   r   r   r*      s    

zSqueezer.__init__c             C   s   |   }t||S )N)get_line_widthr   )r/   r   r   r   r   r   r_   "  s    zSqueezer.count_linesc             C   s   | j  | j }|| j S )N)r'   r5   ri   zero_char_width)r/   Zlinewidth_pixelsr   r   r   rj   0  s    zSqueezer.get_line_widthc             C   s$   | j }t||ddd| _d S )Nfont)rl   0)r'   r   rh   Zmeasurerk   )r/   r   r   r   r   rT   ;  s    zSqueezer.load_fontc       
      C   s  | j tj}x$dD ]}||krP qW | 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}	x2|	dkr| j | j|	d  d|r|	d8 }	qW | j|	| dS )N)rZ   stderrr;   z+1cr   r
   z%s-1cr[   r\   )r]   rY   r^   r	   >)r   	tag_namesr(   ZINSERTZbellZtag_prevrangegetr   r?   r'   r@   r_   r   r`   rA   Zcomparer>   )
r/   rC   rq   Ztag_namer   endr   r"   rd   ir   r   r   squeeze_current_text_event@  s,    	






z#Squeezer.squeeze_current_text_event)rL   rM   rN   rS   classmethodrW   r*   r_   rj   rT   ru   r   r   r   r   rO      s   BrO   __main__)rP   zidlelib.idle_test.test_squeezerrX   F)	verbosityexit)r   )r   rf   Ztkinterr(   Ztkinter.fontr   Ztkinter.messageboxZ
messageboxr<   Zidlelib.configr   Zidlelib.textviewr   Zidlelib.tooltipr   Zidlelibr   r   r)   r   rO   rW   rL   ZunittestrP   r   r   r   r   <module>   s    
8r +