
    ړi6                    L   d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	 d dl
mZmZmZmZmZmZmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZmZ d dlm Z m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z'm(Z( dgZ)dZ*e G d d             Z+ G d de      Z,	 	 	 d	 	 	 	 	 	 	 	 	 ddZ-ddZ.y)    )annotationsN)	dataclass)Enum)	GeneratorIterable)HTMLAnyFormattedTextFormattedTextOneStyleAndTextTupleStyleAndTextTuplesfragment_list_widthmerge_formatted_textto_formatted_text)split_lines)Input)KeyBindingsKeyPressEvent)Output)PromptSessionprint_formatted_text)	BaseStyleStyleTransformation)pygments_token_to_classname)
get_cwidth)PythonLexerPythonTracebackLexerOutputPrinteri@B c                     e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZdddZ		 	 	 	 	 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 ddZ	 	 	 	 ddZ	 	 	 	 d dZ	 	 	 	 	 	 d!dZy)"r   z
    Result printer.

    Usage::

        printer = OutputPrinter(...)
        printer.display_result(...)
        printer.display_exception(...)
    r   outputr   inputr   styler	   titler   style_transformationc          	         t        |      }t        |      }| j                  | j                  |||| j                  j                         j                  |z
  |      |      }| j                  ||       y)aq  
        Show __repr__ (or `__pt_repr__`) for an `eval` result and print to output.

        :param reformat: Reformat result using 'black' before printing if the
            result is parsable as Python code.
        :param highlight: Syntax highlight the result.
        :param paginate: Show paginator when the result does not fit on the
            screen.
        )reformat	highlightline_lengthpaginate
out_promptr(   N)r   r   "_insert_out_prompt_and_split_lines_format_result_outputr   get_sizecolumns_display_result)selfresultr*   r%   r&   r(   out_prompt_widths          7/tmp/pip-target-20744g9h/lib/python/ptpython/printer.pydisplay_resultzOutputPrinter.display_result4   s    $ 'z2
.z:88&&!# KK002::=MM! '  " 9 	
 	Vh7    c               r    | j                  | j                  ||      d      }| j                  ||       y)z&
        Render an exception.
        r&    r)   r+   N)r,   _format_exception_outputr0   )r1   er&   r(   r2   s        r4   display_exceptionzOutputPrinter.display_exceptionU   sD     88))!y)A 9 
 	Vh7r6   c               L    | j                  | j                  |d      |       y )Nr9   r)   r+   )r0   r,   )r1   r2   r(   s      r4   display_style_and_text_tuplesz+OutputPrinter.display_style_and_text_tuplesa   s-     	33Fr3J 	 	
r6   c                   |r| j                  |       n|D ]  }| j                  |        | j                  j                          y )N)_print_paginated_formatted_text_print_formatted_textr   flush)r1   linesr(   lines       r4   r0   zOutputPrinter._display_resultl   sD     007 1**401 	r6   c                t    t        t        |      | j                  | j                  d| j                  |       y )NF)r!   r#   include_default_pygments_styler   end)r   r
   r!   r#   r   )r1   rD   rG   s      r4   rA   z#OutputPrinter._print_formatted_textz   s/    $**!%!:!:+0;;	
r6   c             #  h  K   	 t        |d      r%t         t        |d                   }|E d{    y	 	 t        |      }t        |      t        k  r	 t        |dd       d}	nd}	|	r=|r;	 ddl}
t        |
d	      st        	 |
j                  ||
j                  |
            }|	r|rt!        |      E d{    yd|f y7 # t        t        f$ r   Y xY w# t        $ r  t        $ r}| j                  |||       Y d}~yd}~ww xY w# t        $ r d}	Y w xY w# t        $ r Y w xY w7 ww)a  
        Format __repr__ for an `eval` result.

        Note: this can raise `KeyboardInterrupt` if either calling `__repr__`,
              `__pt_repr__` or formatting the output with "Black" takes to long
              and the user presses Control-C.
        __pt_repr__N)r&   r(   r9   evalTFr   Mode)r'   )mode)hasattrr   getattrGeneratorExitKeyboardInterruptreprBaseExceptionr<   lenMAX_REFORMAT_SIZEcompileSyntaxErrorblackImportError
format_strrK   _lex_python_result)r1   r2   r%   r&   r'   r(   formatted_result_reprresult_reprr;   valid_pythonrW   s              r4   r-   z#OutputPrinter._format_result_output   sk    $	v}-(92GFM24)% 100 .	v,K {//$R0  $ LHuf-%% .
 $..< / 
 I)+666{##e 101 		 
 ! 	 	""1	H"M	  %$%   7s   D2+C CC D2C D2D "	D2,D! 5D28D09D2C CD2D/DD2DD2DD2DD2!	D-*D2,D--D2c              #     K   t        |      }t        |      }dd|z  f}t        t        |            D ]  \  }}|dk(  rg ||}n|g|}|  yw)z
        Split styled result in lines (based on the \n characters in the result)
        an insert output prompt on whitespace in front of each line. (This does
        not yet do the soft wrapping.)

        Yield lines as a result.
        r9    r   N)r   r   	enumerater   )r1   r2   r*   r3   prefixirD   s          r4   r,   z0OutputPrinter._insert_out_prompt_and_split_lines   sp      'z2
.z:c,,- V!45 	GAtAv++d+J	s   AAc              #    K   | j                   j                         j                  }t        |      D ]Q  \  }}d}g }|D ]?  ^}}}	|D ]5  }
t	        |
      }||z   |kD  r| d}g }||z  }|j                  ||
f       7 A | S yw)z
        Apply soft wrapping to the given lines. Wrap according to the terminal
        width. Insert whitespace in front of each wrapped line to align it with
        the output prompt.
        r   N)r   r.   r/   r`   r   append)r1   rC   r'   linenorD   columns_in_buffercurrent_liner!   text_cwidths               r4   _apply_soft_wrappingz"OutputPrinter._apply_soft_wrapping   s      kk**,44 &e, 	LFD !79L#' 4ta 
4A&qME )50;>**,-)')%.% ''
3
44 #	s   BBc                   	
 | j                  |      }t        | j                  | j                  | j                  | j
                        	dd
| j                  j                         j                  dz
  g }d
	
fd}d}t        |      D ]W  \  }}|j                  |       |j                  d       |dz  }|k\  s3| j                  |d       g }d}
rM |        sW y	 | j                  |       y	)z
        Print formatted text, using --MORE-- style pagination.
        (Avoid filling up the terminal's scrollback buffer.)
        )r   r    F   c                 
   j                  d      } | t        j                  k(  rt        d       dy | t        j                  k(  rdy | t        j
                  k(  rj                  dz
  y | t        j                  k(  rdy y )NT)	in_threadz...rn   )promptPagerResultABORTprint	NEXT_LINE	NEXT_PAGErows	PRINT_ALL)pager_resultabortmax_rowspager_prompt	print_allsizes    r4   
show_pagerzAOutputPrinter._print_paginated_formatted_text.<locals>.show_pager  sx     (...>L{000e!6!66!6!6699q=!6!66 	 7r6   r   )r9   
r9   )rG   N)returnNone)rl   create_pager_promptr!   r"   r   r    r.   rw   r`   extendrd   rA   )r1   rC   pager   rw   re   rD   rz   r{   r|   r}   r~   s          @@@@@r4   r@   z-OutputPrinter._print_paginated_formatted_text  s     ))%0*JJ

4;;djj
 	 {{##%99q= $&	! 	!, %e, 	LFDKKKK
#AIDx**4R*8 L	 	""4(r6   c              #    K   |j                   r*| j                  |j                   |      E d {    d n5|j                  r)| j                  |j                  |      E d {    d t        t	        j
                  |j                              }t        |      D ]  \  }}|d   dk(  s||d  } n t	        j                  |      }|r|j                  dd       |j                  t	        j                  t        |      |             dj                  |      }|r5t               j                  |      D ]  \  }}	}
dt!        |	      z   |
f  y d|f y 7 /7 w)	Nr8   )r9   zG
The above exception was the direct cause of the following exception:

)r9   zF
During handling of the above exception, another exception occurred:

r   z<stdin>z#Traceback (most recent call last):
r9   class:)	__cause__r:   __context__list	traceback
extract_tb__traceback__r`   format_listinsertr   format_exception_onlytypejoinr   get_tokens_unprocessedr   )r1   r;   r&   tblistline_nrtb_tupletb_listtb_strindex	tokentyperh   s              r4   r:   z&OutputPrinter._format_exception_outputA  sd     ;;44Q[[I4VVV  ]]44Q]]i4XXX 
 i**1??;<!*6!2 	GX{i')	
 ''/NN1DEy66tAwBC!
 *>*@*W*W+ P&y$  "=i"HH$OOP
 v,G W Ys)   -E#E5E#%E!&A	E#0B/E#!E#N)r2   objectr*   r	   r%   boolr&   r   r(   r   r   r   )r;   rR   r&   r   r(   r   r   r   )r2   Iterable[OneStyleAndTextTuple]r(   r   r   r   )rC   Iterable[StyleAndTextTuples]r(   r   r   r   )r   )rD   r   rG   strr   r   )r2   r   r%   r   r&   r   r'   intr(   r   r   +Generator[OneStyleAndTextTuple, None, None])r2   r   r*   r	   r   r   )rC   r   r   r   )rC   r   r   r   )r;   rR   r&   r   r   r   )__name__
__module____qualname____doc____annotations__r5   r<   r>   r0   rA   r-   r,   rl   r@   r:    r6   r4   r   r   "   s}    NL--88 %	8
 8 8 8 
8B
8
8.2
8>B
8	
8	
.	
 		

 
	
+ 	
 

I$I$ 	I$
 I$ I$ I$ 
5I$V4BR	%*1	%<=)1=)	=)~''+/'	4'r6   c                      e Zd ZdZdZdZdZy)rr   rs   ru   rv   rx   N)r   r   r   rs   ru   rv   rx   r   r6   r4   rr   rr   k  s    EIIIr6   rr   c                   t               }|j                  d      |j                  d      dd              }|j                  d      dd       }|j                  d      dd       }|j                  d      |j                  d	      |j                  d
      |j                  dd      dd                            }|j                  d      dd       }	t        t        |t	        d      g      |d| ||      }
|
S )z:
    Create a "--MORE--" prompt for paginated output.
    enterdownc                X    | j                   j                  t        j                         y N)r2   )appexitrr   ru   events    r4   	next_linez&create_pager_prompt.<locals>.next_line}  s     			k334r6   spacec                X    | j                   j                  t        j                         y r   )r   r   rr   rv   r   s    r4   	next_pagez&create_pager_prompt.<locals>.next_page      		k334r6   ac                X    | j                   j                  t        j                         y r   )r   r   rr   rx   r   s    r4   r}   z&create_pager_prompt.<locals>.print_all  r   r6   qzc-czc-descapeT)eagerc                X    | j                   j                  t        j                         y r   )r   r   rr   rs   r   s    r4   nozcreate_pager_prompt.<locals>.no  s    
 			k//0r6   z<any>c                     y)zDisallow inserting other text.Nr   r   s    r4   ri   zcreate_pager_prompt.<locals>._  s     	r6   z<status-toolbar><more> -- MORE -- </more> <key>[Enter]</key> Scroll <key>[Space]</key> Next page <key>[a]</key> Print all <key>[q]</key> Quit </status-toolbar>: )key_bindingserase_when_doner!   r    r   )r   r   r   r   )r   addr   r   r   )r!   r"   r    r   bindingsr   r   r}   r   ri   sessions              r4   r   r   r  s#    }H\\'\\&5  5 \\'5 5 \\#5 5 \\#\\%\\%\\($\'1 (   1 \\'  +8*	
 '+G* Nr6   c              #  |   K   t               }|j                  |       }|D ]  \  }}}dt        |      z   |f  yw)z$Return token list for Python string.r   N)r   r   r   )r2   lexertokensr   r   rh   s         r4   rZ   rZ     sM     ME ))&1F"( Hy$5i@@$GGHs   :<)r9   NN)
r!   r   r"   r	   r    zInput | Noner   zOutput | Noner   zPromptSession[PagerResult])r2   r   r   z&Generator[tuple[str, str], None, None])/
__future__r   r   dataclassesr   enumr   typingr   r   prompt_toolkit.formatted_textr   r	   r
   r   r   r   r   r   #prompt_toolkit.formatted_text.utilsr   prompt_toolkit.inputr   prompt_toolkit.key_bindingr   r   prompt_toolkit.outputr   prompt_toolkit.shortcutsr   r   prompt_toolkit.stylesr   r   prompt_toolkit.styles.pygmentsr   prompt_toolkit.utilsr   pygments.lexersr   r   __all__rT   r   rr   r   rZ   r   r6   r4   <module>r      s    "  !  &	 	 	 < & A ( H @ F + =
   E E EP
$  ! 	999 9 	9
  9x	Hr6   