3 § fü3ã@s²dZddlZddlZddlmZddljZddl m Z ddl m Z ddl mZddlmZdd d „ZGd d „d ejƒZGdd„dƒZejƒ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-cliking 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)ÚFont)ÚidleConf)Ú view_text)ÚHovertip)ÚmacosxéPéc Csäd}d}d}x tjd|ƒD]�}|jƒ|}||7}||7}||dkrT|d7}d}n$||||7}||krx|d7}|}|d7}|dkrt|d|ƒ\}} ||7}| d}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ÚdivmodÚlen) ÚsÚ linewidthÚtabwidthÚposZ linecountZcurrent_columnÚmÚnumcharsÚlinesÚcolumn©rú5/opt/alt/python36/lib64/python3.6/idlelib/squeezer.pyÚcount_lines_with_wrappings0   rc@sJeZdZdZdd„Zdd„Zddd„Zdd d „Zdd d „ZdZ 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|_d|j}t j j |||ddd�d}t ||dd�|j d|jƒtjƒr’|j d |jƒn|j d |jƒ|j‡fd d „ƒd|_|j|jƒdS) NzSqueezed text (%d lines).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)ÚoffsetZlength)rrrÚ}sz*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#rZ button_textZbutton_tooltip_textr)rrr)as(      zExpandingButton.__init__csLd|jjƒ‰|jdkpDt|jƒdkpDt‡fdd„tjd|jƒDƒƒ|_dS)Né2ièiPÃc3s |]}t|jdƒƒˆkVqdS)rN)rÚgroup)Ú.0Z line_match)Údangerous_line_lenrrú ˆsz3ExpandingButton.set_is_dangerous..z[^\n]+) rÚ winfo_widthr!rrÚanyr r r,)r.r)r2rr-‚s   z ExpandingButton.set_is_dangerousNcCsŽ|jdkr|jƒ|jrTtjddjddddgƒ|jt|jƒftj|j d�}|sTd S|j j |j j |ƒ|j|j ƒ|j j|ƒ|jjj|ƒ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ƒ|j|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.viewrCrDcCsf|jjdd|j|jfƒtj|jdd�}x&|jD]\}}|j|t||ƒd�q2W|j |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©rCrC©rDrD)rJrK) Ú__name__Ú __module__Ú __qualname__Ú__doc__r)r-r*rCrDrHr+rrrrrVs !   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.ÚmainÚPyShellzauto-squeeze-min-linesrr/)Útyper8N)rZ GetOptionÚauto_squeeze_min_lines)ÚclsrrrÚreloadÏszSqueezer.reloadcsX|ˆ_|jˆ_‰|jjˆ_gˆ_ddlm}t||ƒrTf|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. r)rRcsŽ|dkr|||ƒSˆj|ƒ}|ˆjkr0|||ƒSt|||ˆƒ}ˆjdtjƒˆjd|ddd�ˆjdƒˆjƒˆjdtj ƒˆj j |ƒdS)NÚstdoutZiomarkéé)ÚwindowÚpadxÚpady) Ú count_linesrTrZ mark_gravityr'ZRIGHTÚ window_createZseeÚupdateZLEFTr@Úappend)rr Úwriter!Úexpandingbutton)r.rrrÚmywriteïs      z"Squeezer.__init__..mywriteN) r#rr$r%r&r@Zidlelib.pyshellrRÚ isinstancera)r.r#rRrcr)r.rrr)×s     zSqueezer.__init__cshˆjjƒ}ˆjjjƒ}|dt‡fdd„d Dƒƒ8}tˆjjˆjjjdƒd�}||jdƒ}t|||ƒ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. écsg|]}tˆjjj|ƒƒ‘qSr)rr#rÚcget)r1Zopt)r.rrú sz(Squeezer.count_lines..Úborderr[Úfont)ÚnameÚ0)rhr[) r#Zget_tk_tabwidthrr4ÚsumrrfZmeasurer)r.rrrrir)r.rr] s  zSqueezer.count_linesc Cs|jjtjƒ}x$dD]}||krPqW|jjƒdS|jj|tjdƒ\}}|jj||ƒ}t|ƒdkrŽ|ddkrŽ|jjd|ƒ}|d d…}|j j ||ƒ|j |ƒ}t ||||ƒ}|jj ||d d d �t|jƒ} x0| dkoô|jj|j| dd |ƒ�r| d8} qÔW|jj| |ƒ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. rWÚstderrr:z+1crr r z%s-1cNrXrY)rZr[r\ú>)rWrméÿÿÿÿro)rÚ tag_namesr'ZINSERTZbellZ tag_prevrangeÚgetrr>r&r?r]rr^r@Zcomparer=) r.rBrpZtag_namer Úendrr!rbÚirrrÚsqueeze_current_text_event*s,          z#Squeezer.squeeze_current_text_eventN) rLrMrNrOÚ classmethodrVr)r]rtrrrrrPÉs  4rPÚ__main__)rQzidlelib.idle_test.test_squeezerreF)Ú verbosityÚexit)rr)rOr Ztkinterr'Z tkinter.fontrZtkinter.messageboxZ messageboxr;Zidlelib.configrZidlelib.textviewrZidlelib.tooltiprZidlelibrrr(rrPrVrLZunittestrQrrrrÚs        9s