
    Mh.                        d Z ddlZddlZddlZddl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lmZ dd	lmZ dd
lmZ ddlmZ d dZd Z ej        d          Zd dZd Zd!d"dZd!dedefdZ d#dZ!d$dZ"d Z#ej$        dk    r(ej%        &                    dd          dk    r	ddl'Z'd Z(dS d Z(dS )%z
Paging capabilities for IPython.core

Notes
-----

For now this uses IPython hooks, so it can't be in IPython.utils.  If we can get
rid of that dependency, we could move it there.
-----
    N)UnsupportedOperation)Path)get_ipython)display)TryNext)chop)system)get_terminal_size)	py3compat   c                     t          | t                    r| }n5|r/d                    |                                 |d                   } d| i}t	          |d           dS )z1Just display, no paging. screen_lines is ignored.
N
text/plainT)raw)
isinstancedictjoin
splitlinesr   )strngstartscreen_linesdatas       Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/IPython/core/page.pydisplay_pager   $   sl    % ' 	;JJu//11%&&9::Eu&Dd    c                       fdS )zRWrap a pager func to strip the `self` arg

    so it can be called as a hook.
    c                      |i |S N )selfargskwargs	page_funcs      r   <lambda>zas_hook.<locals>.<lambda>4   s    D)CF)C)C r   r   )r#   s   `r   as_hookr%   /   s    
 DCCCCr   z(\x1b[^m]+m)c                    t          | t                    r|                     dd          } |                                 |d         }t	          ||dz
            }t          |          dk    r4t          t          j        	                    |d                              dS d}|dd         D ]y}t          j        	                    |          }t          ||z              t                      s dS t                              |          }t          |          dk    r|d         }zt          |t          j        	                    |d                   z              dS )zVery dumb 'pager' in Python, for when nothing else works.

    Only moves forward, same interface as page(), except for pager_cmd and
    mode.
    r    N   r   )r   r   getr   r   lenprintoslinesepr   	page_moreesc_refindall)	r   r   r   out_lnscreenslast_escapescrhunkesc_lists	            r   	page_dumbr8   9   s9    % ,		,++  (F6,q.))G
7||qbjoogaj))*****1R4= 	+ 	+C:??3''D+$%%%;; ~~d++H8}}q  &rlkBJOOGBK88899999r   c                 X   t           j                            dd          }|dk    s|dk    rt          j        dk    s| S 	 ddl}ddl}n# t          $ r | cY S w xY w	 |                    t          j	                  }n5# |j
        $ r(}t          d                    |                    |d}~ww xY w	 |                                }n# t          $ r | cY S w xY w|                                \  }}|                                 |                    t          j	        |j        |           |S )zAttempt to work out the number of lines on the screen.

    This is called by page(). It can raise an error (e.g. when run in the
    test suite), so it's separated out so it can easily be called in a try block.
    TERMNxtermzxterm-colorsunos5r   ztermios error: {0})r-   environr*   sysplatformtermioscursesImportError	tcgetattrstdouterror	TypeErrorformatinitscrAttributeErrorgetmaxyxendwin	tcsetattrTCSANOW)	screen_lines_defr:   r@   rA   
term_flagserrr5   screen_lines_realscreen_colss	            r   _detect_screen_sizerS   Q   sd    :>>&&&D'MMT=00clh6N6N          C&&sz22

= C C C,33C8899sBC nn        %(LLNN!k
MMOOO cj<<<s<    A	 	AAA< <
B.#B))B.2C CCreturnc                    t          | t                    r| d         } t          j                            dd          }|dv r!t          j        dk    rt          |            dS |                                 |d         }t          j        	                    |          }t          |          }t          |          }t          |t          |dz            dz             }	t                      d         }
|d	k    r;	 |t          |
          z  }n'# t          t           f$ r t          |           Y dS w xY w|	|k    rt          |           dS t#          |          }|d
t%          ||          z   z  }t          j        dk    r|                    d          rd}nt)          j        d          \  }}t-          |          }	 t          j        |           |                    dd          5 }|                    |            |d|}ddd           n# 1 swxY w Y   t          j        |          rd}nd}t-          j        |           n# t-          j        |           w xY w	 d}t9          j        |dt8          j        t8          j                  }t          j         tC          j"        |j#        d          |          }	 |j$        ptJ          j&        j$        }|                    |            |                                }n# |                                }w xY wn4# tN          $ r}|j(        dk    rd}nd}Y d}~nd}~wtR          $ r d}Y nw xY w|tU          | |           dS dS )a  Display a string, piping through a pager after a certain length.

    strng can be a mime-bundle dict, supplying multiple representations,
    keyed by mime-type.

    The screen_lines parameter specifies the number of *usable* lines of your
    terminal screen (total lines minus lines you need to reserve to show other
    information).

    If you set screen_lines to a number <=0, page() will try to auto-determine
    your screen size and will only use up to (screen_size+screen_lines) for
    printing, paging after that. That is, if you want auto-detection but need
    to reserve the bottom 3 lines of the screen, use screen_lines = -3, and for
    auto-detection without any lines reserved simply use screen_lines = 0.

    If a string won't fit in the allowed lines, it is sent through the
    specified pager command. If none given, look for PAGER in the environment,
    and ultimately default to less.

    If no system pager works, the string is sent through a 'dumb pager'
    written in python, very simplistic.
    r   r:   dumb)rV   emacsntNP   r(   r    typez.txtwtutf-8encodingz < T)shellstdinstderr)    zBroken pipe)r   )+r   r   r-   r=   r*   namer,   r   r.   r   r+   maxintr
   rS   rF   r   get_pager_cmdget_pager_start
startswithtempfilemkstempr   closeopenwriter	   unlink
subprocessPopenPIPEDEVNULL_wrap_closeioTextIOWrapperra   r_   r>   rD   IOErrorr!   OSErrorr8   )r   r   r   	pager_cmdr:   	str_linesstr_toprintnum_newlineslen_strnumlinesrN   retvalfdtmpnametmppathtmpfilecmdprocpagerpager_encodingmsgs                        r   
pager_pager      s   2 % $l# :>>&((DBGtOOe  ""566*I*//),,Ky>>L+G
 <GBJ 122H(**1- q	/0@AAALL/0 	 	 	+FF	
 <k "),,	S?9U;;;;	7d??##F++ )&.v66Gw--)HRLLL dW== ?e,,,+499gg>? ? ? ? ? ? ? ? ? ? ? ? ? ? ? y~~ &!"!%K((((DK((((!'$/%-	   $TZ'BBBD +%*^%Jsz7JNKK&&&"[[]]FFU[[]]F****F   8222!FFF    e666666 sy   :D  D10D1 +I +HI HI HI I$(AL! -L 2L! LL! !
M+M  MMr   r   c                     t          d|          }t                      }|r0	 |j                            | ||           dS # t          $ r Y nw xY wt          | |||          S )a1  Display content in a pager, piping through a pager after a certain length.

    data can be a mime-bundle dict, supplying multiple representations,
    keyed by mime-type, or text.

    Pager is dispatched via the `show_in_pager` IPython hook.
    If no hook is registered, `pager_page` will be used.
    r   )r   r   N)re   r   hooksshow_in_pagerr   r   )r   r   r   ry   ips        r   pager      s     5MME 
B	 	H""4u<"PPPF 	 	 	D	 dE<;;;s   A 
AAc                    t          |          }|dt          ||          z   z  }	 t          j        d         dv rt          t          |dz   | z              d	S #  	 |dk    r|dz  }t          t          | d                                          |           Y d	S #  t          dt          |                      Y Y d	S xY wxY w)
zDPage a file, using an optional pager command and starting line.
    rZ   r:   )rW   rV   r   r(   r]   r^   zUnable to show fileN)rg   rh   r-   r=   EnvironmentErrorr	   r   rm   readr,   repr)fnamer   ry   s      r   	page_filer     s     i((Iy7777I
5:f!111""y3&'''''5	5qyy
eg...3355u======	5'U4444444s#   0A C<BB>:C>Cc                    t           j        dk    rd}nt           j        dv rd}| 	 t           j        d         } n	#  |} Y nxY w| dk    r9dt           j                            d	d
                                          vr| dz  } | S )zSReturn a pager command.

    Makes some attempts at finding an OS-correct one.
    posixzless -R)rX   dosr[   NPAGERlessz-rLESSr'   z -R)r-   rd   r=   r*   lower)ry   default_pager_cmds     r   rg   rg      s    
 
w'%	L	 	 "	*
7+II	*)IIIFt2:>>&"+E+E+K+K+M+MMMU	s	   : A c                 B    | dv r|rdt          |          z   }nd}nd}|S )zReturn the string for paging files with an offset.

    This is the '+N' argument which less and more (under Unix) accept.
    )r   more+r'   )str)r   r   start_strings      r   rh   rh   6  s;      	U+LLLLr   rX   r:   rV   rW   c                      t           j                            d           t          j                    } | dv rd}nd}t           j                            d           |S )zg Smart pausing between pages

        @return:    True if need print more lines, False if quit
        $---Return to continue, q to quit--- )qQFTzo                                     )r>   rD   rn   msvcrtgetwch)ansresults     r   r/   r/   I  sY    
 	
?@@@moo*FFF
3444r   c                      t          j        d          } |                                                     d          rdS dS )Nr   r   FT)r   inputr   ri   )r   s    r   r/   r/   W  s:    oDEE99;;!!#&& 	54r   )r   r   )r   r   N)rT   N)r   Nr   ))__doc__r-   ru   rer>   rj   rp   r   pathlibr   IPythonr   IPython.displayr   IPython.core.errorr   IPython.utils.datar   IPython.utils.processr	   IPython.utils.terminalr
   IPython.utilsr   r   r%   compiler0   r8   rS   r   rf   r   r   rg   rh   rd   r=   r*   r   r/   r   r   r   <module>r      s(  	 	 
			 				 				 



      # # # # # #             # # # # # # & & & & & & # # # # # # ( ( ( ( ( ( 4 4 4 4 4 4 # # # # # #   D D D 
O	$	$: : : :0+ + +^o7 o7 o7 o7 o7d< <c <S < < < <45 5 5 5(   ,  " 7d??rz~~fV44??MMM        r   