
   u0E                       S r SSKrSSKrSSKJr  SSKJr  SSKJr  SSK	J
r
  SSKJr  SS jr " S	 S
\R                  5      r " S S5      r\R#                  5         \S:X  a  SSKJr  \" SSSS9  gg)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)macosxc                 ^   SnSnSnSn[         R                  " SU 5       Ha  nUR                  5       U-
  nX7-  nXW-  nX   S:X  a  XQ:  a
  XES-
  U-  -  nUS-  nSnOX   S:X  d   eXRXR-  -
  -  nXQ:  a  US-  nUnUS-  nMc     U[        U 5      U-
  -  nUS:  a  XES-
  U-  -  nU$ US-  nU$ )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.
   r      z[\t\n]
	)refinditerstartlen)s	linewidthtabwidthpos	linecountcurrent_columnmnumcharss           '/usr/lib/python3.13/idlelib/squeezer.pycount_lines_with_wrappingr      s    H
CIN[[A&779s?" 6T> ) q0Y>>	NIN6T>!>.*CDDN )Q	!)q; '@ c!fsl"Nq(Y66	
  	Q	    c                   L    \ rS rSrSrS rS rSS jrSS jrSS jr	S	r
S
 rSrg)ExpandingButtonT   a  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                   ^ TU l         X l        X0l        X@l        UR                  =U l        nUR
                  =U l        nUR                  R                  U l        US:w  a  SOSnSU SU S3n[        R                  R                  XUSSS	9  S
n	[        X	SS9  U R                  SU R                  5        [        R                   " 5       (       a  U R                  SU R"                  5        OU R                  SU R"                  5        U R%                  U4S j5        S U l        U R)                  U R*                  5        g )Nr
   lineslinezSqueezed text ( z).z#FFFFC0z#FFFFE0)text
backgroundactivebackgroundz5Double-click to expand, right-click for more options.P   )hover_delayz<Double-Button-1>z
<Button-2>z
<Button-3>c                 J   > T[        U 5      [        U 5      [        U5      -    $ N)int)offsetlengthr   s     r   <lambda>*ExpandingButton.__init__.<locals>.<lambda>z   s    1S[Vs6{1J#Kr   )r   tags
numoflinessqueezereditwinr#   perbottom	base_texttkButton__init__r   bindexpandr   isAquaTkcontext_menu_eventselection_handleis_dangerous
after_idleset_is_dangerous)
selfr   r/   r0   r1   r2   r#   line_pluralitybutton_textbutton_tooltip_texts
    `        r   r8   ExpandingButton.__init___   s   	$ !)!1!11w"<<'	D ++$.!O'
|1^4DBG
		4K&/) 	 	M D 	 	;		%t{{3??IIlD$;$;<IIlD$;$;<K	M !--.r   c                   ^ SU R                   R                  5       -  mU R                  S:  =(       dS    [        U R                  5      S:  =(       d4    [        U4S j[        R                  " SU R                  5       5       5      U l        g )N2   i  iP  c              3   ^   >#    U  H"  n[        UR                  S 5      5      T:  v   M$     g7f)r   N)r   group).0
line_matchdangerous_line_lens     r   	<genexpr>3ExpandingButton.set_is_dangerous.<locals>.<genexpr>   s.      "@J J$$Q'(,>>"@s   *-z[^\n]+)	r#   winfo_widthr0   r   r   anyr   r   r>   )rA   rL   s    @r   r@    ExpandingButton.set_is_dangerous   sm    $))"7"7"99OOd" K% "$++i"@  	r   Nc                    U R                   c  U R                  5         U R                   (       aj  [        R                  " SSR	                  / SQ5      U R
                  [        U R                  5      4-  [        R                  U R                  S9nU(       d  gU R                  R                  U 5      nU R                  R                  X0R                  U R                  5        U R                  R                  U 5        U R                  R!                  X0R                  U R                  5        U R"                  R$                  R'                  U 5        g)a  expand 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.
NzExpand 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.zReally expand?)titlemessagedefaultparentbreak)r>   r@   r   askokcanceljoinr0   r   r   CANCELr#   indexr5   insertr/   deleter2   on_squeezed_expandr1   expandingbuttonsremove)rA   eventconfirmr[   s       r   r:   ExpandingButton.expand   s     $!!# ,,+ % 
 oos466{34 #))yy	"G 		%eVVTYY7d#''vvtyyA&&--d3r   c                 Z    U R                  5         U R                  U R                  5        g)z=copy event handler

Copy the original text to the clipboard.
N)clipboard_clearclipboard_appendr   rA   ra   s     r   copyExpandingButton.copy   s"    
 	dff%r   c                 F    [        U R                  SU R                  SSS9  g)zMview event handler

View the original text in a separate text viewer window.
zSqueezed Output ViewerFnone)modalwrapN)r   r#   r   rg   s     r   viewExpandingButton.view   s     
 	$))5tvvF	,r   ))rh   rh   )rn   rn   c                 T   U R                   R                  SSUR                  UR                  4-  5        [        R
                  " U R                   SS9nU R                   H  u  p4UR                  U[        X5      S9  M      UR                  UR                  UR                  5        g)Nr\   z@%d,%dr   )tearoff)labelcommandrW   )r#   mark_setxyr6   Menurmenu_specsadd_commandgetattrtk_popupx_rooty_root)rA   ra   rmenurr   method_names        r   r<   "ExpandingButton.context_menu_event   s    		8X%''0B%BC		1-"&"2"2EE743MN #3u||U\\2r   )r5   r2   r>   r0   r   r1   r/   r#   r)   )__name__
__module____qualname____firstlineno____doc__r8   r@   r:   rh   rn   rx   r<   __static_attributes__ r   r   r   r   T   s-    	/@	
4>&,Kr   r   c                   :    \ rS rSrSr\S 5       rS rS rS r	Sr
g)	Squeezer   zReplace 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.
c                 >    [         R                  " SSSSSS9U l        g)z!Load class variables from config.mainPyShellzauto-squeeze-min-linesr*   rG   )typerU   N)r   	GetOptionauto_squeeze_min_lines)clss    r   reloadSqueezer.reload   s$     &.%7%7I7&
"r   c                 0  ^ ^ UT l         UR                  =T l        mUR                  R                  T l        S[        TR                  S5      5      [        TR                  S5      5      -   -  T l        / T l        SUR                  4U U4S jjnX!l	        g)a-  Initialize 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.
   borderpadxr   c                   > US:w  a  U" X5      $ TR                   n[        U 5      U:  a  U" X5      $ TR                  U 5      nXC:  a  U" X5      $ [        XUT5      nTR	                  S[
        R                  5        TR                  SUSSS9  TR                  S5        TR                  5         TR	                  S[
        R                  5        TR                  R                  U5        g )Nstdoutiomark      windowr   pady)r   r   count_linesr   mark_gravityr6   RIGHTwindow_createseeupdateLEFTr_   append)r   r/   writer   r0   expandingbuttonrA   r#   s         r   mywrite"Squeezer.__init__.<locals>.mywrite   s    xQ~% &*%@%@"1v..Q~%))!,J2Q~% .az4HO h1x$%A  /HHXKKMh0 !!((9r   N)
r2   r#   r3   r4   r5   r*   cgetwindow_width_deltar_   r   )rA   r2   r   r#   s   `  @r   r8   Squeezer.__init__   s     "<<'	D !++ #$		(#$		&!"##

 !# gmm 	: 	:<  r   c                 @    [        XR                  R                  5      $ )a2  Count 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.
)r   r2   width)rA   r   s     r   r   Squeezer.count_lines  s     )LL,>,>??r   c                    U R                   R                  [        R                  5      nS H
  nX!;   d  M
    O   U R                   R	                  5         gU R                   R                  U[        R                  S-   5      u  p4U R                   R                  X45      n[        U5      S:  a,  US   S:X  a#  U R                   R                  SU-  5      nUSS nU R                  R                  X45        U R                  U5      n[        XRX`5      nU R                   R                  X7S	S
S9  [        U R                  5      nUS:  aq  U R                   R                  U R                  US-
     SU5      (       a?  US-  nUS:  a4  U R                   R                  U R                  US-
     SU5      (       a  M?  U R                  R!                  X5        g)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.
)r   stderrrW   z+1cr   r   z%s-1cNr   r   r   r
   >)r#   	tag_namesr6   INSERTbelltag_prevrangegetr   r[   r5   r]   r   r   r   r_   comparer\   )	rA   r   tag_namer   endr   r0   r   is	            r   squeeze_current_textSqueezer.squeeze_current_text"  s    II''		2	,H$ -
 IINN YY,,Xryy57HI
IIMM%% q6A:!B%4-))//'C-0C#2A 	e) %%a(
)!zH 			%&Q 	  	0 %%&!e		))$*?*?!*D*-@ @FA !e		))$*?*?!*D*-@ @ 	$$Q8r   )r5   r2   r_   r#   r   N)r   r   r   r   r   classmethodr   r8   r   r   r   r   r   r   r   r      s,    
 
 
= ~@-r   r   __main__)r   zidlelib.idle_test.test_squeezerr   F)	verbosityexit)r&   )r   r   tkinterr6   r   idlelib.configr   idlelib.textviewr   idlelib.tooltipr   idlelibr   r   r7   r   r   r   r   unittestr   r   r   r   <module>r      sr     
   # & $ 5pqbii qhG GT 	  z	*aeD r   