
    d"2                         d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	Z G d
 dej                  Z G d d          Ze                                 edk    rddlmZ  eddd           dS dS )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)macosxP   c                    d}d}d}d}t          j        d|           D ]s}|                                |z
  }||z  }||z  }| |         dk    r||k    r||dz
  |z  z  }|dz  }d}n&| |         dk    sJ ||||z  z
  z  }||k    r|dz  }|}|dz  }t|t          |           |z
  z  }|dk    r||dz
  |z  z  }n|dz  }|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.
       r      z[\t\n]
	)refinditerstartlen)s	linewidthtabwidthpos	linecountcurrent_columnmnumcharss           "  /croot/python-split_1694437901252/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac/lib/python3.11/idlelib/squeezer.pycount_lines_with_wrappingr      s,    H
CIN[A&&  7799s?x(" S6T>> 	)) nq0Y>>	NINNS6T>>>>h.8*CDDN 	))Q	!)q c!ffsl"Nnq(Y66		 	Q	    c                   @    e Zd ZdZd Zd Zd
dZd
dZd
dZdZ	d	 Z
dS )ExpandingButtona  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                 l   | _         || _        || _        || _        |j        x| _        }|j        x| _        }|j        j        | _        |dk    rdnd}d| d| d}t          j
                            | ||dd	           d
}	t          | |	d           |                     d| j                   t          j                    r|                     d| j                   n|                     d| j                   |                     fd           d | _        |                     | j                   d S )Nr   lineslinezSqueezed text ( z).z#FFFFC0z#FFFFE0)text
backgroundactivebackgroundz5Double-click to expand, right-click for more options.r   )hover_delayz<Double-Button-1>z
<Button-2>z
<Button-3>c                 l    t          |           t          |           t          |          z            S N)int)offsetlengthr   s     r   <lambda>z*ExpandingButton.__init__.<locals>.<lambda>z   s(    1S[[Vs6{{1J%J#K r   )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-   r.   r/   r0   r#   line_pluralitybutton_textbutton_tooltip_texts
    `        r   r6   zExpandingButton.__init___   se   	$ !)!11w"<'	D +$.!OOG
GG^GGG
	4K&/) 	 	M 	M 	M D 	 	*;;;;		%t{333? 	=IIlD$;<<<<IIlD$;<<<KKKK	M 	M 	M !-.....r   c                     d| j                                         z  | j        dk    pJt          | j                  dk    p2t          fdt          j        d| j                  D                       | _        d S )N2   i  iP  c              3   d   K   | ]*}t          |                    d                     k    V  +dS )r   N)r   group).0
line_matchdangerous_line_lens     r   	<genexpr>z3ExpandingButton.set_is_dangerous.<locals>.<genexpr>   sS         J$$Q''((,>>     r   z[^\n]+)	r#   winfo_widthr.   r   r   anyr   r   r<   )r?   rI   s    @r   r>   z ExpandingButton.set_is_dangerous   s    $)"7"7"9"99Od" KK%    "$+i"@"@     	r   Nc                 @   | j         |                                  | j         r\t          j        dd                    g d          | j        t          | j                  fz  t          j        | j	                  }|sdS | j	        
                    |           }| j                            || j        | j                   | j                            |            | j                            || j        | j                   | j        j                            |            dS )aA  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joinr.   r   r   CANCELr#   indexr3   insertr-   deleter0   on_squeezed_expandr/   expandingbuttonsremove)r?   eventconfirmrV   s       r   r8   zExpandingButton.expand   s    $!!### 	 ,+ % % %  
 os46{{34 #)y	" 	" 	"G  w	%%eTVTY777d###''tvtyAAA&--d33333r   c                 b    |                                   |                     | j                   dS )zMcopy event handler

        Copy the original text to the clipboard.
        N)clipboard_clearclipboard_appendr   r?   r\   s     r   copyzExpandingButton.copy   s2    
 	df%%%%%r   c                 B    t          | j        d| j        dd           dS )z]view event handler

        View the original text in a separate text viewer window.
        zSqueezed Output ViewerFnone)modalwrapN)r   r#   r   ra   s     r   viewzExpandingButton.view   s5    
 	$)5tvF	, 	, 	, 	, 	, 	,r   ))rb   rb   )rg   rg   c                 4   | j                             dd|j        |j        fz             t	          j        | j         d          }| j        D ]*\  }}|                    |t          | |                     +|	                    |j
        |j                   dS )NrW   z@%d,%dr   )tearoff)labelcommandrR   )r#   mark_setxyr4   Menurmenu_specsadd_commandgetattrtk_popupx_rooty_root)r?   r\   rmenurj   method_names        r   r:   z"ExpandingButton.context_menu_event   s    	8X%'0B%BCCC	1---"&"2 	O 	OE;E743M3MNNNNu|U\222wr   r(   )__name__
__module____qualname____doc__r6   r>   r8   rb   rg   rp   r:    r   r   r   r   T   s        	 	/ / /@	
 	
 	
4 4 4 4>& & & &, , , ,K    r   r   c                   :    e Zd ZdZed             Zd Zd Zd Zd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.
    c                 B    t          j        ddddd          | _        dS )z!Load class variables from config.mainPyShellzauto-squeeze-min-linesr)   rD   )typerP   N)r   	GetOptionauto_squeeze_min_lines)clss    r   reloadzSqueezer.reload   s1     &.%7I7&
 &
 &
"""r   c                 (    | _         |j        x _        |j        j         _        dt                              d                    t                              d                    z   z   _        g  _        d|j	        f fd	}||_	        dS )ae  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                 "   |dk    r || |          S j         }t          |           |k     r || |          S                     |           }||k     r || |          S t          | ||          }                    dt
          j                                       d|dd                               d           	                                                     dt
          j
                   j                            |           d S )Nstdoutiomark      windowr   pady)r   r   count_linesr   mark_gravityr4   RIGHTwindow_createseeupdateLEFTrZ   append)r   r-   writer   r.   expandingbuttonr?   r#   s         r   mywritez"Squeezer.__init__.<locals>.mywrite   s%   xuQ~~% &*%@"1vv...uQ~~%))!,,J222uQ~~% .az4HHO h111x$%A  / / /HHXKKMMMh000 !((99999r   N)
r0   r#   r1   r2   r3   r)   cgetwindow_width_deltarZ   r   )r?   r0   r   r#   s   `  @r   r6   zSqueezer.__init__   s     "<'	D !+ #$		(##$$		&!!""##

 !# gm 	: 	: 	: 	: 	: 	: 	:<  r   c                 6    t          || j        j                  S )ab  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   r0   width)r?   r   s     r   r   zSqueezer.count_lines  s     )DL,>???r   c                    | j                             t          j                  }dD ]}||v r n	| j                                          dS | j                             |t          j        dz             \  }}| j                             ||          }t          |          dk    r3|d         dk    r'| j                             d|z            }|dd         }| j	        
                    ||           |                     |          }t          ||||           }| j                             ||d	d
           t          | j                  }|dk    r_| j                             | j        |dz
           d|          r5|dz  }|dk    r*| j                             | j        |dz
           d|          5| j                            ||           dS )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   stderrrR   z+1cr   r   z%s-1cNr   r   r   r   >)r#   	tag_namesr4   INSERTbelltag_prevrangegetr   rV   r3   rX   r   r   r   rZ   comparerW   )	r?   r   tag_namer   endr   r.   r   is	            r   squeeze_current_textzSqueezer.squeeze_current_text"  s    I''	22	, 	 	H9$$ % INN7 Y,,Xry57HII
sIMM%%% q66A::!B%4--)//'C-00C#2#A 	eS))) %%a((
)!Xz4HH 		o%&Q 	  	0 	0 	0 %&&!ee	))$*?!*D*-@ @eFA !ee	))$*?!*D*-@ @e 	$$Q888wr   N)	rx   ry   rz   r{   classmethodr   r6   r   r   r|   r   r   r~   r~      sj         
 
 
 [
=  =  = ~@ @ @- - - - -r   r~   __main__)r   zidlelib.idle_test.test_squeezerr   F)	verbosityexit)r   )r{   r   tkinterr4   r   idlelib.configr   idlelib.textviewr   idlelib.tooltipr   idlelibr   r   r5   r   r~   r   rx   unittestr   r|   r   r   <module>r      sR     
			           # # # # # # & & & & & & $ $ $ $ $ $      5 5 5 5pq q q q qbi q q qhG G G G G G G GT 	    zD	*aeDDDDDD r   