3 ^3c@sdZddlZddlZddlmZddljZddl m Z ddl m Z ddl mZddlmZdd d ZGd d d ejZGdddZejedkrddlmZedddddS)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)macosxPc Csd}d}d}xtjd|D]}|j|}||7}||7}||dkrT|d7}d}n4||dksdt||||7}||kr|d7}|}|d7}|dkrt|d|\}} ||7}| d}qW|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] z )refinditerstartZAssertionErrorZdivmodlen) s linewidthtabwidthZposZ linecountZcurrent_columnZmZnumcharsZlinesZcolumnr(/usr/lib64/python3.6/idlelib/squeezer.pycount_lines_with_wrappings2   rc@sJeZdZdZddZddZdddZdd d Zdd d ZdZ 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|_d|j}t j j |||dddd}t ||dd|j d|jtjr|j d |jn|j d |j|jfd d d|_|j|jdS) NzSqueezed text (%d lines).z#FFFFC0z#FFFFE0)textZ backgroundZactivebackgroundz5Double-click to expand, right-click for more options.r)Z hover_delayzz z cst|t|t|SN)int)ZoffsetZlengthrrrZ}sz*ExpandingButton.__init__..)rtags numoflinessqueezereditwinrperbottom base_texttkButton__init__rZbindexpandrZisAquaTkcontext_menu_eventZselection_handle is_dangerousZ after_idleset_is_dangerous) selfrrrrrrZ button_textZbutton_tooltip_textrrrr"as(      zExpandingButton.__init__csLd|jj|jdkpDt|jdkpDtfddtjd|jD|_dS)N2iiPc3s |]}t|jdkVqdS)rN)r Zgroup).0Z line_matchZdangerous_line_lenrrz sz3ExpandingButton.set_is_dangerous..z[^\n]+) r winfo_widthrr rZanyr r r%r'rr*rr&s   z ExpandingButton.set_is_dangerousNcCs|jdkr|j|jrTtjddjddddg|jt|jftj|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?)ZtitleZmessagedefaultZparentbreak)r%r& tkMessageBoxZ askokcancelZjoinrr rZCANCELrrinsertindexrdeleterexpandingbuttonsZremove)r'eventZconfirmrrrr#s$   zExpandingButton.expandcCs|j|j|jdS)zMcopy event handler Copy the original text to the clipboard. N)Zclipboard_clearZclipboard_appendrr'r4rrrcopyszExpandingButton.copycCst|jd|jddddS)z]view event handler View the original text in a separate text viewer window. zSqueezed Output ViewerFZnone)ZmodalZwrapN)rrrr5rrrviews zExpandingButton.viewr6r7cCsf|jjdd|j|jftj|jdd}x&|jD]\}}|j|t||dq2W|j |j |j dS)Nr0z@%d,%dr)Ztearoff)labelZcommandr.) rZmark_setZxZyr ZMenu rmenu_specsZ add_commandZgetattrZtk_popupZx_rootZy_root)r'r4Zrmenur8Z method_namerrrr$s z"ExpandingButton.context_menu_eventrrrr6r6r7r7)r:r;) __name__ __module__ __qualname____doc__r"r&r#r6r7r9r$rrrrrVs !   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.mainPyShellzauto-squeeze-min-linesrr()Ztyper-N)rZ GetOptionauto_squeeze_min_lines)ZclsrrrreloadszSqueezer.reloadcsX|_|j_|jj_g_ddlm}t||rTf|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. r)rBcs|dkr|||Sj|}|jkr0|||St|||}jdtjjd|dddjdjjdtj j j |dS)NstdoutZiomarkZwindowpadxZpady) count_linesrCrZ mark_gravityr ZRIGHT window_createZseeZupdateZLEFTr3Zappend)rrwriterexpandingbuttonr'rrrmywrites      z"Squeezer.__init__..mywriteN) rrrrrr3Zidlelib.pyshellrBZ isinstancerL)r'rrBrOrrNrr"s     zSqueezer.__init__cshjj}jjj}|dtfddd D8}tjjjjjdd}||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|]}tjjj|qSr)rrrcget)r)Zoptr,rrz sz(Squeezer.count_lines..borderrIfont)ZnameZ0)rRrI) rZget_tk_tabwidthrr+ZsumrrQZmeasurer)r'rrrrSrr,rrJ s  zSqueezer.count_linesc Cs|jjtj}x$dD]}||krPqW|jjdS|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} qW|jj| |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. rEstderrr.z+1crrr z%s-1cNrFrGrHz>)rErTrU)r tag_namesr ZINSERTZbellZ tag_prevrangeZgetr r1rr2rJrrKr3Zcomparer0) r'r4rVZtag_namer ZendrrrMZirrrsqueeze_current_text_event*s,          z#Squeezer.squeeze_current_text_eventN) r<r=r>r?Z classmethodrDr"rJrWrrrrr@s  4r@Z__main__)rAzidlelib.idle_test.test_squeezerrPF)Z verbosityZexit)rr)r?r Ztkinterr Z tkinter.fontrZtkinter.messageboxZ messageboxr/Zidlelib.configrZidlelib.textviewrZidlelib.tooltiprZidlelibrrr!rr@rDr<ZunittestrArrrrZs        9s