B � f(2�@s�dZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZddd �ZGd d �d ej�ZGd d �d �Ze��edkr�ddlmZedddd�dS)a An IDLE extension to avoid having very long texts printed in the shell. A common problem in IDLE's interactive shell is printing of large amounts of text into the shell. This makes looking at the previous history difficult. Worse, this can cause IDLE to become very slow, even to the point of being completely unusable. This extension will automatically replace long texts with a small button. Double-clicking this button will remove it and insert the original text instead. Middle-clicking will copy the text to the clipboard. Right-clicking will open the text in a separate viewing window. Additionally, any output can be manually "squeezed" by the user. This includes output written to the standard error stream ("stderr"), such as exception messages and their tracebacks. �N)�idleConf)� view_text)�Hovertip)�macosx�PcCs�d}d}d}d}x�t�d|�D]�}|��|}||7}||7}||dkrp||krb||d|7}|d7}d}n4||dks�t�||||7}||kr�|d7}|}|d7}qW|t|�|7}|dkr�||d|7}n|d8}|S)z�Count the number of lines in a given string. Lines are counted as if the string was wrapped so that lines are never over linewidth characters long. Tabs are considered tabwidth characters long. �r�z[\t\n]� � )�re�finditer�start�AssertionError�len)�sZ linewidthZtabwidth�posZ linecountZcurrent_column�m�numchars�r�5/opt/alt/python37/lib64/python3.7/idlelib/squeezer.py�count_lines_with_wrappings0   rc@sJeZdZdZdd�Zdd�Zddd�Zdd d �Zdd d �Zd Z dd�Z dS)�ExpandingButtona�Class for the "squeezed" text buttons used by Squeezer These buttons are displayed inside a Tk Text widget in place of text. A user can then use the button to replace it with the original text, copy the original text to the clipboard or view the original text in a separate window. Each button is tied to a Squeezer instance, and it knows to update the Squeezer instance when it is expanded (and therefore removed). c s��|_||_||_||_|j|_}|j|_}|jj|_|dkrFdnd}d|�d|�d�}t j j |||ddd �d } t || d d �|� d |j�t��r�|� d|j�n|� d|j�|��fdd��d|_|�|j�dS)Nr�lines�linezSqueezed text (� z).z#FFFFC0z#FFFFE0)�textZ backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r)Z hover_delayzz z cs�t|�t|�t|��S)N)�int)�offset�length)rrr�z�z*ExpandingButton.__init__..)r�tags� numoflines�squeezer�editwinr�per�bottom� base_text�tk�Button�__init__rZbind�expandrZisAquaTk�context_menu_eventZselection_handle� is_dangerousZ after_idle�set_is_dangerous) �selfrr!r"r#r$rZline_pluralityZ button_textZbutton_tooltip_textr)rrr*_s*     zExpandingButton.__init__csLd|j���|jdkpDt|j�dkpDt�fdd�t�d|j�D��|_dS)N�2i�iP�c3s |]}t|�d���kVqdS)rN)r�group)�.0Z line_match)�dangerous_line_lenrr� �sz3ExpandingButton.set_is_dangerous..z[^\n]+) rZ winfo_widthr"rr�anyr r r-)r/r)r3rr.s   z ExpandingButton.set_is_dangerousNcCs�|jdkr|��|jrTtjdd�ddddg�|jt|j�ftj|j d�}|sTd S|j � |j � |�|j|j �|j �|�|jj�|�dS) aAexpand event handler This inserts the original text in place of the button in the Text widget, removes the button and updates the Squeezer instance. If the original text is dangerously long, i.e. expanding it could cause a performance degradation, ask the user for confirmation. NzExpand 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.zReally expand?)�title�message�default�parent�break)r-r.� tkMessageBoxZ askokcancel�joinr"rrZCANCELrr'�insert�indexr!�deleter#�expandingbuttons�remove)r/�eventZconfirmrrrr+�s$   zExpandingButton.expandcCs|��|�|j�dS)zMcopy event handler Copy the original text to the clipboard. N)Zclipboard_clearZclipboard_appendr)r/rBrrr�copy�szExpandingButton.copycCst|jd|jddd�dS)z]view event handler View the original text in a separate text viewer window. zSqueezed Output ViewerFZnone)ZmodalZwrapN)rrr)r/rBrrr�view�s zExpandingButton.view))rCrC)rDrDcCsf|j�dd|j|jf�tj|jdd�}x&|jD]\}}|j|t||�d�q2W|� |j |j �dS)Nr=z@%d,%dr)Ztearoff)�labelZcommandr:) rZmark_set�x�yr(ZMenu� rmenu_specsZ add_command�getattrZtk_popupZx_rootZy_root)r/rBZrmenurEZ method_namerrrr,�s z"ExpandingButton.context_menu_event)N)N)N) �__name__� __module__� __qualname__�__doc__r*r.r+rCrDrHr,rrrrrTs     rc@s4eZdZdZedd��Zdd�Zdd�Zdd �Zd S) �Squeezerz�Replace long outputs in the shell with a simple button. This avoids IDLE's shell slowing down considerably, and even becoming completely unresponsive, when very long outputs are written. cCstjdddddd�|_dS)z!Load class variables from config.�mainZPyShellzauto-squeeze-min-linesrr0)�typer8N)rZ GetOption�auto_squeeze_min_lines)�clsrrr�reload�szSqueezer.reloadcsd|�_|j�_�|jj�_dt��d��t��d���_g�_d|j f��fdd� }||_ dS)aeInitialize settings for Squeezer. editwin is the shell's Editor window. self.text is the editor window text widget. self.base_test is the actual editor window Tk text widget, rather than EditorWindow's wrapper. self.expandingbuttons is the list of all buttons representing "squeezed" output. �Zborder�padxrcs�|dkr|||�S�j}t|�|kr.|||�S��|�}||krJ|||�St|||��}��dtj��jd|ddd���d��� ���dtj ��j � |�dS)N�stdoutZiomark��)�windowrU�pady) rQr� count_linesrZ mark_gravityr(ZRIGHT� window_createZsee�updateZLEFTr@�append)rr!�writerQr"�expandingbutton)r/rrr�mywrite�s        z"Squeezer.__init__..mywriteN) r$rr%r&r'rZcgetZwindow_width_deltar@r_)r/r$rar)r/rrr*�s    zSqueezer.__init__cCst||jj�S)abCount the number of lines in a given text. Before calculation, the tab width and line length of the text are fetched, so that up-to-date values are used. Lines are counted as if the string was wrapped so that lines are never over linewidth characters long. Tabs are considered tabwidth characters long. )rr$�width)r/rrrrr[s zSqueezer.count_linesc Cs|j�tj�}x$dD]}||krPqW|j��dS|j�|tjd�\}}|j�||�}t|�dkr�|ddkr�|j�d|�}|dd�}|j � ||�|� |�}t ||||�}|jj ||d d d �t|j�} x2| dk�r|j�|j| d d |��r| d 8} q�W|j�| |�dS)z�squeeze-current-text event handler Squeeze the block of text inside which contains the "insert" cursor. If the insert cursor is not in a squeezable block of text, give the user a small warning and do nothing. )rV�stderrr:z+1cr�����r z%s-1cNrWrX)rYrUrZr�>)r� tag_namesr(ZINSERTZbellZ tag_prevrange�getrr>r'r?r[rr\r@Zcomparer=) r/rBrfZtag_namer �endrr"r`�irrr�squeeze_current_text_event s,          z#Squeezer.squeeze_current_text_eventN) rJrKrLrM� classmethodrSr*r[rjrrrrrN�s  ? rN�__main__)rOzidlelib.idle_test.test_squeezerrTF)� verbosity�exit)r)rMr Ztkinterr(Ztkinter.messageboxZ messageboxr;Zidlelib.configrZidlelib.textviewrZidlelib.tooltiprZidlelibrrr)rrNrSrJZunittestrOrrrr�s      8r