\d"2dZddlZddlZddlmZddlmZddlmZddl m Z ddl m Z dd Z Gd d ejZGd d ZeedkrddlmZeddddSdS)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) messagebox)idleConf) view_text)Hovertip)macosxPcd}d}d}d}tjd|D]s}||z }||z }||z }||dkr||kr ||dz |zz }|dz }d}n&||dksJ||||zz z }||kr|dz }|}|dz }t|t||z z }|dkr ||dz |zz }n|dz}|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)s linewidthtabwidthpos linecountcurrent_columnmnumcharss !..\python\lib\idlelib\squeezer.pycount_lines_with_wrappingrs,H CIN [A & &7799s? x(" S6T>> )) nq0Y>> NINNS6T>>>> h.8*CD DN ))Q !) qc!ffsl"Nnq(Y66  Q c@eZdZdZdZdZd dZd dZd dZdZ d Z 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). cl|_||_||_||_|jx|_}|jx|_}|jj|_|dkrdnd}d|d|d}tj |||dd d } t|| d | d |jtjr| d|jn| d|j|fdd|_||jdS)Nr lineslinezSqueezed text ( z).z#FFFFC0z#FFFFE0)text backgroundactivebackgroundz5Double-click to expand, right-click for more options.r) hover_delayzz z clt|t|t|zSN)int)offsetlengthrs rz*ExpandingButton.__init__..zs(1S[[Vs6{{1J%J#Kr)rtags numoflinessqueezereditwinr#perbottom base_texttkButton__init__rbindexpandrisAquaTkcontext_menu_eventselection_handle is_dangerous after_idleset_is_dangerous) selfrr-r.r/r0r#line_plurality button_textbutton_tooltip_texts ` rr6zExpandingButton.__init___se $  !)!11 w"<' D +$.!OOG GG^GGG  4K&/)  M M M D  *;;;; %t{333 ?   = IIlD$; < < < < IIlD$; < < <  K K K K M M M! -.....rcd|jz|jdkpJt|jdkp2t fdt jd|jD|_dS)N2iiPc3dK|]*}t|dkV+dS)rN)rgroup).0 line_matchdangerous_line_lens r z3ExpandingButton.set_is_dangerous..sSJ$$Q''((,>>rz[^\n]+) r# winfo_widthr.rranyrrr<)r?rIs @rr>z ExpandingButton.set_is_dangerouss$)"7"7"9"99 Od "  KK%   "$+i"@"@   rNc@|j||jr\tjddgd|jt |jfztj|j }|sdS|j |}|j ||j|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>r askokcanceljoinr.rrCANCELr#indexr3insertr-deleter0on_squeezed_expandr/expandingbuttonsremove)r?eventconfirmrVs rr8zExpandingButton.expands   $  ! ! # # #    ,+ %%% os46{{3 4 #)y " " "G w %% eTVTY777 d### ''tvtyAAA &--d33333rcb|||jdS)zMcopy event handler Copy the original text to the clipboard. N)clipboard_clearclipboard_appendrr?r\s rcopyzExpandingButton.copys2  df%%%%%rcBt|jd|jdddS)z]view event handler View the original text in a separate text viewer window. zSqueezed Output ViewerFnone)modalwrapN)rr#rras rviewzExpandingButton.views5 $)5tvF , , , , , ,r))rbrb)rgrgc4|jdd|j|jfzt j|jd}|jD]*\}}||t||+| |j |j dS)NrWz@%d,%dr)tearoff)labelcommandrR) r#mark_setxyr4Menu rmenu_specs add_commandgetattrtk_popupx_rooty_root)r?r\rmenurj method_names rr:z"ExpandingButton.context_menu_events 8X%'0B%BCCC 1---"&"2 O O E;   E743M3M  N N N N u|U\222wrr() __name__ __module__ __qualname____doc__r6r>r8rbrgrpr:rrrrTs  ///@    4444>&&&&,,,,K rrc:eZdZdZedZdZdZdZdS)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. cBtjddddd|_dS)z!Load class variables from config.mainPyShellzauto-squeeze-min-linesr)rD)typerPN)r GetOptionauto_squeeze_min_lines)clss rreloadzSqueezer.reloads1&.%7 I7& & & """rc(|_|jx_|jj_dt dt dzz_g_d|j ffd }||_ 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. borderpadxr|c"|dkr |||Sj}t||kr |||S|}||kr |||St|||}dt jd|ddd dt j j |dS)Nstdoutiomarkwindowrpady) rr count_linesr mark_gravityr4RIGHT window_createseeupdateLEFTrZappend)rr-writerr.expandingbuttonr?r#s rmywritez"Squeezer.__init__..mywrites%xuQ~~%&*%@ "1vv...uQ~~%))!,,J222uQ~~%.az4HHO   h 1 1 1   x$%A  / / / HHX    KKMMM   h 0 0 0  ! ( ( 9 9 9 9 9rN) r0r#r1r2r3r)cgetwindow_width_deltarZr)r?r0rr#s` @rr6zSqueezer.__init__s "<' D!+#$  (## $ $  &!! " " ##  !#gm : : : : : : :<  rc6t||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. )rr0width)r?rs rrzSqueezer.count_liness)DL,>???rc|jtj}dD]}||vrn |jdS|j|tjdz\}}|j||}t|dkr3|ddkr'|jd|z}|dd}|j ||| |}t||||}|j ||d d t|j}|dkr_|j|j|d z d |r5|d z}|dkr*|j|j|d z d |5|j||dS)zSqueeze the text block where the insertion cursor is. If the cursor is not in a squeezable block of text, give the user a small warning and do nothing. )rstderrrRz+1crr z%s-1cNrrrr >)r# tag_namesr4INSERTbell tag_prevrangegetrrVr3rXrrrrZcomparerW) r?rtag_namerendrr.ris rsqueeze_current_textzSqueezer.squeeze_current_text"sI'' 22 ,  H9$$% INN   7Y,,Xry57HII s IMM% % % q66A::!B%4--)//'C-00C#2#A eS)))%%a(( )!Xz4HH o%&Q  0 0 0 % & &!ee ))$*?!*D*-@@e FA!ee ))$*?!*D*-@@e $$Q888wrN) rxryrzr{ classmethodrr6rrr|rrr~r~sj   [ = = = ~ @ @ @-----rr~__main__)rzidlelib.idle_test.test_squeezerrF) verbosityexit)r)r{rtkinterr4ridlelib.configridlelib.textviewridlelib.tooltipridlelibrrr5rr~rrxunittestrr|rrrsR  ######&&&&&&$$$$$$5555pqqqqqbiqqqhGGGGGGGGT  zD *aeDDDDDDr