f _(2c@sdZddlZddlZddlmZddlmZddl m Z ddl m Z ddl mZddd ZGd d d ejZGd d d ZeedkrddlmZedddddS)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)macosxPcCsd}d}d}d}td|D]~}||}||7}||7}||dkrn||kr`||d|7}|d7}d}n$||||7}||kr|d7}|}|d7}q|t||7}|dkr||d|7}n|d8}|S)zCount 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. irz[\t\n] )refinditerstartlen)sZ linewidthZtabwidthZposZ linecountZcurrent_columnZmZnumcharsr )/usr/lib64/python3.10/idlelib/squeezer.pycount_lines_with_wrappings.   rc@sJeZdZdZddZddZdddZdd d Zdd d Zd Z ddZ dS)ExpandingButtonaClass 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 |jtr| d|jn| d|j|fddd|_||jdS)NrZlinesZlinezSqueezed text (z z).z#FFFFC0z#FFFFE0)textZ backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r)Z hover_delayzz z cst|t|t|SN)int)ZoffsetZlengthr r rZzsz*ExpandingButton.__init__..)r tags numoflinessqueezereditwinrperbottom base_texttkButton__init__rZbindexpandrZisAquaTkcontext_menu_eventZselection_handle is_dangerousZ after_idleset_is_dangerous) selfr rrrrrZline_pluralityZ button_textZbutton_tooltip_textr rrr_s0     zExpandingButton.__init__csLd|j|jdkpDt|jdkpDtfddtd|jD|_dS)N2iiPc3s |]}t|dkVqdS)rN)r Zgroup)Z.0Z line_matchZdangerous_line_lenr rZ sz3ExpandingButton.set_is_dangerous..z[^\n]+) rZ winfo_widthrr r Zanyrr r!)r#r r%rr"s    z ExpandingButton.set_is_dangerousNcCs|jdur||jrPtjddgd|jt|jftj|j d}|sPdS|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?)ZtitleZmessagedefaultZparentbreak)r!r" tkMessageBoxZ askokcancelZjoinrr r ZCANCELrrinsertindexrdeleterexpandingbuttonsZremove)r#eventZconfirmr r rrs    zExpandingButton.expandcCs|||jdS)zMcopy event handler Copy the original text to the clipboard. N)Zclipboard_clearZclipboard_appendr r#r-r r rcopyszExpandingButton.copycCst|jd|jddddS)z]view event handler View the original text in a separate text viewer window. zSqueezed Output ViewerFZnone)ZmodalZwrapN)rrr r.r r rviews zExpandingButton.view))r/r/)r0r0cCsb|jdd|j|jftj|jdd}|jD]\}}|j|t||dq0| |j |j dS)Nr)z@%d,%dr)Ztearoff)labelZcommandr') rZmark_setZxZyrZMenu rmenu_specsZ add_commandZgetattrZtk_popupZx_rootZy_root)r#r-Zrmenur1Z method_namer r rr s z"ExpandingButton.context_menu_eventrrr) __name__ __module__ __qualname____doc__rr"rr/r0r2r r r r rrTs     rc@s4eZdZdZeddZddZddZdd Zd S) SqueezerzReplace 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-linesrr$)Ztyper&N)rZ GetOptionauto_squeeze_min_lines)Zclsr r rreloadszSqueezer.reloadcsd|_|j_|jj_dtdtd_g_d|j ffdd }||_ 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. Zborderpadxr cs|dkr|||Sj}t||kr.|||S|}||krJ|||St|||}dtjjd|dddd dtj j |dS)NstdoutZiomarkZwindowr<Zpady) r9r count_linesrZ mark_gravityrZRIGHT window_createZseeZupdateZLEFTr,Zappend)r rwriter9rexpandingbuttonr#rr rmywrites"      z"Squeezer.__init__..mywriteN) rrrrrrZcgetZwindow_width_deltar,rC)r#rrFr rErrs     zSqueezer.__init__cCst||jjS)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. )rrZwidth)r#r r r rrAs zSqueezer.count_linesc Cs|jtj}dD]}||vrq2q|jdS|j|tjd\}}|j||}t|dkr|ddkr|jd|}|dd}|j ||| |}t ||||}|jj ||d d d t|j} | dkr|j|j| d d |r| d 8} q|j| |dS)zsqueeze-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. )r=Zstderrr'z+1crirz%s-1cNr>r?r@rz>)r tag_namesrZINSERTZbellZ tag_prevrangeZgetr r*rr+rArrBr,Zcomparer)) r#r-rGZtag_namer Zendr rrDZir r rsqueeze_current_text_event s0       z#Squeezer.squeeze_current_text_eventN) r3r4r5r6Z classmethodr:rrArHr r r rr7s  ? r7Z__main__)r8zidlelib.idle_test.test_squeezerr;F)Z verbosityZexit)r)r6rZtkinterrZtkinter.messageboxZ messageboxr(Zidlelib.configrZidlelib.textviewrZidlelib.tooltiprZidlelibrrrrr7r:r3Zunittestr8r r r rZs      8r