f _(2@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. rz[\t\n] )refinditerstartlen)sZ linewidthZtabwidthposZ linecountZcurrent_columnmnumcharsr)/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)NrlineslinezSqueezed text ( z).z#FFFFC0z#FFFFE0)textZ backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r)Z hover_delayzz z cst|t|t|S)N)int)offsetlengthrrrzz*ExpandingButton.__init__..)rtags numoflinessqueezereditwinrperbottom base_texttkButton__init__rbindexpandrZisAquaTkcontext_menu_eventZselection_handle is_dangerousZ after_idleset_is_dangerous) selfrr r!r"r#rZline_pluralityZ button_textZbutton_tooltip_textrrrr)_s0     zExpandingButton.__init__csLd|j|jdkpDt|jdkpDtfddtd|jD|_dS)N2iiPc3s |]}t|dkVqdS)rN)r group).0Z line_matchZdangerous_line_lenrr sz3ExpandingButton.set_is_dangerous..z[^\n]+) rZ winfo_widthr!r ranyr r r-)r/rr3rr.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?)titlemessagedefaultparentbreak)r-r. tkMessageBoxZ askokcanceljoinr!r rZCANCELrr&insertindexr deleter"expandingbuttonsremove)r/eventZconfirmrrrr+s    zExpandingButton.expandcCs|||jdS)zMcopy event handler Copy the original text to the clipboard. N)Zclipboard_clearZclipboard_appendrr/rBrrrcopyszExpandingButton.copycCst|jd|jddddS)z]view event handler View the original text in a separate text viewer window. zSqueezed Output ViewerFZnone)ZmodalwrapN)rrrrCrrrviews zExpandingButton.view))rDrD)rFrFcCsb|jdd|j|jftj|jdd}|jD]\}}|j|t||dq0| |j |j dS)Nr=z@%d,%dr)Ztearoff)labelZcommandr:) rZmark_setxyr'ZMenu rmenu_specsZ add_commandgetattrZtk_popupZx_rootZy_root)r/rBZrmenurGZ method_namerrrr,s z"ExpandingButton.context_menu_event)N)N)N) __name__ __module__ __qualname____doc__r)r.r+rDrFrJr,rrrrrTs     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-linesrr0)typer8N)rZ GetOptionauto_squeeze_min_lines)clsrrrreloadszSqueezer.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. Zborderpadxrcs|dkr|||Sj}t||kr.|||S|}||krJ|||St|||}dtjjd|dddd dtj j |dS)NstdoutZiomarkZwindowrWZpady) rSr count_linesrZ mark_gravityr'ZRIGHT window_createZseeupdateZLEFTr@append)rr writerSr!expandingbuttonr/rrrmywrites"      z"Squeezer.__init__..mywriteN) r#rr$r%r&rZcgetZwindow_width_deltar@r`)r/r#rcrrbrr)s     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. )rr#width)r/rrrrr\s 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. )rXstderrr:z+1crr z%s-1cNrYrZr[r>)r tag_namesr'ZINSERTZbellZ tag_prevrangegetr r>r&r?r\rr]r@Zcomparer=) r/rBrhZtag_namer endrr!rairrrsqueeze_current_text_event s0       z#Squeezer.squeeze_current_text_eventN) rLrMrNrO classmethodrUr)r\rlrrrrrPs  ? rP__main__)rQzidlelib.idle_test.test_squeezerrVF) verbosityexit)r)rOr tkinterr'Ztkinter.messageboxZ messageboxr;Zidlelib.configrZidlelib.textviewrZidlelib.tooltiprZidlelibrrr(rrPrUrLZunittestrQrrrrs      8r