B
� f(2 � @ s� d Z ddlZddlZddlmZ ddlmZ ddl m
Z
ddlmZ ddl
mZ ddd �ZG d
d� dej�ZG dd
� d
�Ze�� edkr�ddlmZ edddd� 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�P c C s� 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_wrapping s0 r c @ sJ e Zd ZdZdd� Zdd� Zddd�Zdd d
�Zddd�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| | dd� | �
d
| j� t�� r�| �
d| j� n| �
d| j� | �� fdd�� d | _| �| j� d S )Nr �lines�linezSqueezed text (� z).z#FFFFC0z#FFFFE0)�textZ
backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r )Zhover_delayzz
z
c s � t | �t | �t |� � S )N)�int)�offset�length)r r r �z � z*ExpandingButton.__init__..)r �tags�
numoflines�squeezer�editwinr �per�bottom� base_text�tk�Button�__init__r Zbind�expandr ZisAquaTk�context_menu_eventZselection_handle�is_dangerousZ
after_idle�set_is_dangerous)
�selfr r! r" r# r$ r Zline_pluralityZbutton_textZbutton_tooltip_textr )r r r* _ s*
zExpandingButton.__init__c sL d| j �� � | jdkpDt| j�dkpDt� fdd�t�d| j�D ��| _d S )N�2 i� iP� c 3 s | ]}t |�d ��� kV qdS )r N)r �group)�.0Z
line_match)�dangerous_line_lenr r � � s z3ExpandingButton.set_is_dangerous..z[^\n]+) r Zwinfo_widthr" r r �anyr r r- )r/ r )r3 r r. s
z ExpandingButton.set_is_dangerousNc C s� | j dkr| �� | j rTtjdd�ddddg�| jt| j�f tj| j d�}|sTd S | j
�| j �| �| j| j
� | j
�| � | jj�| � dS )
aA expand 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. �tkMessageBoxZaskokcancel�joinr" r r ZCANCELr r' �insert�indexr! �deleter# �expandingbuttons�remove)r/ �eventZconfirmr r r r+ � s$
zExpandingButton.expandc C s | � � | �| j� dS )zMcopy event handler
Copy the original text to the clipboard.
N)Zclipboard_clearZclipboard_appendr )r/ rB r r r �copy� s zExpandingButton.copyc C s t | jd| jddd� dS )z]view event handler
View the original text in a separate text viewer window.
zSqueezed Output ViewerFZnone)ZmodalZwrapN)r r r )r/ rB r r r �view� s zExpandingButton.view))rC rC )rD rD c C sf | j �dd|j|jf � tj| j dd�}x&| jD ]\}}|j|t| |�d� q2W |� |j
|j� dS )Nr= z@%d,%dr )Ztearoff)�labelZcommandr: )r Zmark_set�x�yr( ZMenu�rmenu_specsZadd_command�getattrZtk_popupZx_rootZy_root)r/ rB ZrmenurE Zmethod_namer r r r, � s z"ExpandingButton.context_menu_event)N)N)N)�__name__�
__module__�__qualname__�__doc__r* r. r+ rC rD rH r, r r r r r T s
r c @ s4 e Zd ZdZedd� �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.
c C s t jdddddd�| _dS )z!Load class variables from config.�mainZPyShellzauto-squeeze-min-linesr r0 )�typer8 N)r Z GetOption�auto_squeeze_min_lines)�clsr r r �reload� s zSqueezer.reloadc sd |� _ |j � _�|jj� _dt��d��t��d�� � _g � _d|j f� �fdd� }||_ dS )ae Initialize 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�padxr c s� |dkr|| |�S � j }t| �|k r.|| |�S � �| �}||k rJ|| |�S t| ||� �}��dtj� �jd|ddd� ��d� �� � ��dtj
� � j�|� d S )N�stdoutZiomark� � )�windowrU �pady)
rQ r �count_linesr Zmark_gravityr( ZRIGHT�
window_createZsee�updateZLEFTr@ �append)r r! �writerQ r" �expandingbutton)r/ r r r �mywrite� s
z"Squeezer.__init__..mywriteN)
r$ r r% r&