xhUddlmZddlZddlZddlZddlZddlmZm Z ddl m Z ddl m Z mZmZmZmZmZmZmZmZmZmZddlmZddlmZmZddlmZdd l m!Z!dd l"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,dd l-m.Z.dd l/m0Z0dd l1m2Z2m3Z3ddl4m5Z5ddl6m7Z7ddl8m9Z9m:Z:ddl;mZ>m?Z?ddl@mAZAddlBmCZCddlDmEZEmFZFddlGmZmHZHddlImJZJeeKdfZLejMdkZNdZOie+ee,ede#ede#jPede&ed e&jede)jQed!e'jRede'jSed"e'jTedd#e'jUed"d#e'jVede'jWed!d$e'jXed%e'jYed%e'jZede'j[ed&e*ed'e(ed e%j\ed(e%j]ed"e%j^ed)e%j_ed!d$e%j`ed)e%j$ed(e$ed%d#i Zad*ebd+<ie+ee,ed,e#ede#jPed-e&ed&e&jed-e)jQed.e'jRed-e'jSed/e'jTed-d#e'jUed/d#e'jVed-e'jWed.d$e'jXed(e'jYed(e'jZed-e'j[ed&e*ed'e(ed&e%j\ed(e%j]ed/e%j^ed)e%j_ed.d$e%j`ed)e%j$ed(e$ed%d#i Zcd*ebd0<eaecd1Zdd2ZeGd3d4eZfGd5d6efZgGd7d8efZheeieifZjGd9d:eZkGd;d<ZlGd=d>eAZmddEZneodFkrRddlpZpddlZepjqdGHZrersdIdJdKLersdMdNdOdPddQRersdSdTdUdPdVdWRersdXdYdZdPd[\ersd]d^eid_dd`aersdbdcdddPdVdeRersdfdgdPdhdVdijersdkdldmddnoersdpdqdrddsoersdtduddvdwxersdydzeidd{d|}ersd~eiddd}ertZuddlvmu-E-E**  J J J->y-I-I*** J*/D &!%!;!L!&t/E!F!F!Fs3AArRrSrc f |j|S#t$r |j|}|d}|d}t |rd|znd|rd|zn|j|d|d|d}n##t$rt j}YnwxYw||j|<YnwxYw|S) z"Get a style from a Pygments class.r9ro#z#000000rAitalicr@)r9rorArzr@)rpKeyErrorrsstyle_for_tokenrrunull)rXrRpygments_styler9rostyles rYrZz'PygmentsSyntaxTheme.get_style_for_tokens 2$Z0 0 2 2 2 !%!;!K!KJ!W!W'w/(3).=#++I-4PC'MM$:P'/)(3,[9   % % %  %-2D j ) ) ) 2  s2  B.A>A B.>BB.B B.-B.c|jSNrvr^s rYr_z(PygmentsSyntaxTheme.get_background_style %%r\N)rirjrTrkr`rarbrcrdrerwrZr_rfr\rYrhrhsZ88 G G G G,&&&&&&r\rhc*eZdZdZd dZdd Zdd Zd S)ANSISyntaxThemez$Syntax theme to use standard colors. style_maprFrTrkc||_tj|_tj|_i|_dSr)rrr}_missing_stylervrp)rXrs rYrwzANSISyntaxTheme.__init__s3"#jll!&46r\rRrSrc |j|S#t$rO|jj}t |}|j}|r||}||}n |dd}|||j|<|cYSwxYw)zLook up style in the style map.N)rpr{rgettupler)rXrR get_styletokenr_styles rYrZz#ANSISyntaxTheme.get_style_for_tokens $Z0 0   *I*%%E'E #"5))%"Ecrc   # -2D j )LLL s AA('A(c|jSrrr^s rYr_z$ANSISyntaxTheme.get_background_stylerr\N)rrFrTrkr`rarrfr\rYrrsV..7777 (&&&&&&r\rc@eZdZUdZded<ded<ded<dZded <d S) _SyntaxHighlightRangez A range to highlight in a Syntax object. `start` and `end` are 2-integers tuples, where the first integer is the line number (starting from 1) and the second integer is the column index (starting from 0). r3rSyntaxPositionstartendFbool style_beforeN)rbrcrdre__annotations__rrfr\rYrrsS Lr\rc"eZdZdZddZdd Zd S)PaddingPropertyz"Descriptor to get and set padding.objSyntaxobjtype Type[Syntax]rTTuple[int, int, int, int]c|jS)zSpace around the Syntax.)_padding)rXrrs rY__get__zPaddingProperty.__get__s |r\paddingr$rkc8tj||_dSr)r#unpackr)rXrrs rY__set__zPaddingProperty.__set__s~g.. r\N)rrrrrTr)rrrr$rTrk)rbrcrdrerrrfr\rYrrsB,,//////r\rcreZdZUdZded<ded<ed\d Zed d d d d d d d d d dd d]d(Ze Z ed)d ed d d d d d d d d d dfd^d/Z ed_d`d0Z dad2Z dbd6Zedcd8Zeddd:Z d_ded<Z dfdgdCZdhdidHZedjdIZdkdMZdldRZdmdUZdndWZdodYZdpd[Zd S)qraConstruct a Syntax object to render syntax highlighted code. Args: code (str): Code to highlight. lexer (Lexer | str): Lexer to use (see https://pygments.org/docs/lexers/) theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "monokai". dedent (bool, optional): Enable stripping of initial whitespace. Defaults to False. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False. start_line (int, optional): Starting number for line numbers. Defaults to 1. line_range (Tuple[int | None, int | None], optional): If given should be a tuple of the start and end line to render. A value of None in the tuple indicates the range is open in that direction. highlight_lines (Set[int]): A set of line numbers to highlight. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width. tab_size (int, optional): Size of tabs. Defaults to 4. word_wrap (bool, optional): Enable word wrapping. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None. indent_guides (bool, optional): Show indent guides. Defaults to False. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding). zType[PygmentsStyle]rsrQ_themenameUnion[str, SyntaxTheme]rTct|tr|S|tvrtt|}nt |}|S)zGet a syntax theme instance.)rqrQRICH_SYNTAX_THEMESrrh)clsrris rY get_themezSyntax.get_themesN dK ( ( K % % %#$6t$<==EE'--E r\Fr%Nr) ridedent line_numbers start_line line_rangehighlight_lines code_widthtab_size word_wraprt indent_guidesrcoderrlexerUnion[Lexer, str]rirrrrintr-Optional[Tuple[Optional[int], Optional[int]]]rOptional[Set[int]]r Optional[int]rrrt Optional[str]rrr$rkc ||_||_||_||_||_||_|p t |_| |_| |_ | |_ | |_ | rt| n t|_ | |_tj||_|||_g|_dS)Nrn)r_lexerrrrrsetrrrrrtrbackground_stylerr#rrrr_stylized_ranges)rXrrrirrrrrrrrrtrrs rYrwzSyntax.__init__s$   ($$.7#%%$  " 0/? LE* + + + +UWW +w// nnU++ =?r\zutf-8pathencodingOptional[Union[Lexer, str]]Optional[Tuple[int, int]]'Syntax'ct||}|s|||}||||||||| | | | | ||S)aConstruct a Syntax object from a file. Args: path (str): Path to file to highlight. encoding (str): Encoding of file. lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs". dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False. start_line (int, optional): Starting number for line numbers. Defaults to 1. line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render. highlight_lines (Set[int]): A set of line numbers to highlight. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width. tab_size (int, optional): Size of tabs. Defaults to 4. word_wrap (bool, optional): Enable word wrapping of code. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None. indent_guides (bool, optional): Show indent guides. Defaults to False. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding). Returns: [Syntax]: A Syntax object that may be printed to the console )r)r) rirrrrrrrrrtrr)r read_text guess_lexer)rrrrrirrrrrrrrrtrrrs rY from_pathzSyntax.from_path<sRDzz##X#66 5OODtO44Es  %!!+!-'    r\cnd}d}|r" t||}n#t$rYnwxYw|sl tj|\}}|r6|d}t|}n#t$rYnwxYw|r|jr|jd}n|j }|S)aGuess the alias of the Pygments lexer to use based on a path and an optional string of code. If code is supplied, it will use a combination of the code and the filename to determine the best lexer to use. For example, if the file is ``index.html`` and the file contains Django templating syntax, then "html+django" will be returned. If the file is ``index.html``, and no templating language is used, the "html" lexer will be used. If no string of code is supplied, the lexer will be chosen based on the file extension.. Args: path (AnyStr): The path to the file containing the code you wish to know the lexer for. code (str, optional): Optional string of code that will be used as a fallback if no lexer is found for the supplied path. Returns: str: The name of the Pygments lexer that best matches the supplied path/code. Nrm.r) rr!osrsplitextlstriplowerraliasesr)rrrr lexer_name_ext extensions rYrzSyntax.guess_lexer{s""&   0t<<       ))$//39 # 3 5 5 7 7I-i88E       (} ("]1- "Z s &&AB BBrcH|j|jz}|S)zGet the base style.)rr_r)rX default_styles rY_get_base_stylezSyntax._get_base_styles# 88::T=RR r\rRrSOptional[Color]cD|j|}|jS)zGet a color (if any) for the given token. Args: token_type (TokenType): A token type tuple from Pygments. Returns: Optional[Color]: Color from theme, or None for no color. )rrZr9)rXrRrs rY_get_token_colorzSyntax._get_token_colors! // ;;{r\Optional[Lexer]ct|jtr|jS t|jdd|jS#t $rYdSwxYw)zThe lexer for this syntax, or None if no lexer was found. Tries to find the lexer by name if a string was passed to the constructor. FTstripnlensurenltabsizeN)rqrrrrr!r^s rYrz Syntax.lexersp dk5 ) ) ;  $       44 sA A Arc2tddd|jS)z;A Pygments Lexer to use if one is not specified or invalid.textFTr)rrr^s rY default_lexerzSyntax.default_lexers)! M     r\r4cB |}|jrdnd}t|||j|j }|jj|jp|j| n|r3|\ d fd d  fd }| |n4| fd  D|j | d |j |jr|||S)aHighlight code and return a Text instance. Args: code (str): Code to highlight. line_range(Tuple[int, int], optional): Optional line range to highlight. Returns: Text: A text instance containing highlighted syntax. rmleft)justifyrrno_wrapNrTIterable[Tuple[Any, str]]c3KsJD]+\}}|r$|d\}}}|||zfV|$,dS)zSplit tokens to one per line. N) get_tokens partition)rRr line_tokennew_linerrs rY line_tokenizez'Syntax.highlight..line_tokenizes LLL-2-=-=d-C-CDD) E#D:?//$:O:O7J%",j8.C"CCCC$DDDr\%Iterable[Tuple[str, Optional[Style]]]c3hKt }d}rdz nd}||krJ t|\}}n#t$rYn*wxYw|dfV|dr|dz }||kJ|D]9\}}||fV|dr|dz }r |krdS:dS)zConvert tokens to spans.rr%Nr)iternext StopIterationendswith) tokensline_no _line_start _token_typerrR_get_theme_styleline_end line_startrs rYtokens_to_spansz)Syntax.highlight..tokens_to_spanss#!--//22FG4>"E*q..AK"K//"15f.K,"""!E"$dm+++ >>$//)#qLG"K//.4&&) E$&6&6z&B&BCCCC >>$//&#qLG'&Gx,?,? % &&sA A  A c38K|]\}}||fVdSrrf).0rRrrs rY z#Syntax.highlight..sL##) E,,Z889######r\zon )rTr)rTr)rtransparent_backgroundr4rrrrZrrappend append_tokensrrtstylizer_apply_stylized_ranges) rXrr base_stylerrrrrrrrs ` @@@@@rY highlightzSyntax.highlights))++ #: FII ]&      ;: 0d0 = KK    + (2$ HDDDDDDD&&&&&&&&&.""??#4#45555""####-2-=-=d-C-C###$0 :4#8::;;;   .  ' ' - - - r\rr3rrrrcZ|jt||||dS)a Adds a custom style on a part of the code, that will be applied to the syntax display when it's rendered. Line numbers are 1-based, while column indexes are 0-based. Args: style (StyleType): The style to apply. start (Tuple[int, int]): The start of the range, in the form `[line number, column index]`. end (Tuple[int, int]): The end of the range, in the form `[line number, column index]`. style_before (bool): Apply the style before any existing styles. N)rrr)rXrrrrs rY stylize_rangezSyntax.stylize_range(s:" $$ !%\ B B     r\333333?blendfloatr)c|j|jz}|j}||jrt jS|tj }||jr|pt jSt| | |}t j |S)N) cross_fade) rr_rrois_system_definedr)rmrrr4r* get_truecolor from_triplet)rXrrrtforeground_color new_colors rY_get_line_numbers_colorzSyntax._get_line_numbers_color=s;;;==@UU+3  #'7'I #=?? "00<<  #'7'I ##6u} 6  * * , ,  * * , ,   !),,,r\cd}|jrDtt|j|jdzt z}|S)z?Get the number of characters used to render the numbers column.rr)rlenrrrrcountNUMBERS_COLUMN_DEFAULT_PADDING)rX column_widths rY_numbers_column_widthzSyntax._numbers_column_widthLsQ   C$)//$*?*??@@AA01 r\consoler+Tuple[Style, Style, Style]c |}|jr5tjtdtjfS|jdvrtj||jtj t| |j }tj||jtj td| d|j }n&|tdz}|tdz}|||fS)z>Get background, number, and highlight styles for line numbers.Tr:)256 truecolorr8g?)rAr9F) rrrr} color_systemchainrrZrr4rr)rXr!r number_stylehighlight_number_styles rY_get_number_styleszSyntax._get_number_stylesWs#//11  2 ?:<<4%*,,> >  #7 7 7 ;  // ;;D88::;;;% L &+[  // ;;4t'C'CC'H'HIII% && " ",eooo=L%5%8H8H8H%H "/EEEr\ 'Console'options'ConsoleOptions' 'Measurement'cJ|j\}}}}||z}|j*|j|jz|zdz}t|j|S|j}|j|z|rt d|Dndz}|jr|dz }t|j|S)Nr%c34K|]}t|VdSrr')rlines rYrz*Syntax.__rich_measure__..zs(44d8D>>444444r\r)rrr r0r splitlinesmaxr) rXr!r,rrightrrwidthliness rY__rich_measure__zSyntax.__rich_measure__ns!L5!T, ? &Od&@@7JQNEt95AA A $$&&  & 8=Ds44e4444441 F     QJE45u===r\r,r.c#Kt|||}t|jr-t |||jVdS|VdS)Nrpad)r2 _get_syntaxanyrr#r)rXr!r,segmentss rY__rich_console__zSyntax.__rich_console__ssD,,Wg>>?? t|   ($*>*>*@*@dlSSS S S S S SNNNNNr\Iterable[Segment]c# K|j}|j\}}}}||z}|j#|jr|j|jz dz n|j|z n|j} td| } ||j \} } | | |j } |js"|j s|j s| s| d||jt zt#dz|jz} |jr*|js#| |j| } d| _| jr2|| ||  Ed{VnB|| || dd |jdd }|D] }|Ed{V dS|j pd\}}d}|rtd|dz }| d| }|j r|t9|krdS|||}|jr|js||jt zt#dz|jz} t;d||j| t#dzdd}|j}|| }|jj }tB}|d}|j"rdnd}|#|\}}}tI||j%|zD]Z\}}|j r0|||dd || }nOtM||d} |j'r| g}n |(| |j|| g}|jr|d|zdz|}!tS|D]\}"}#|"r{tU|+|dz dz}$||r+||t#dV||$|Vn!|d|V||$|Vn|!V|#Ed{V|VI|D]}#|#Ed{V|V\dS)zc Get the Segments for the Syntax object, excluding any vertical/horizontal padding Nr%rrTr:)rcrop)r5)r,r)r5heightr)rr: new_lines)NN) allow_blankF)rzz> u❱ )rBrr9)r rOrBr8z ),rrrrr max_widthr r3 _process_coderr rr remove_suffixrrZrrrr ascii_onlywith_indent_guidesroverflowrenderupdate render_linessplitrr4joinr __contains__r1legacy_windowsr* enumeraterlistradjust_line_lengthr&rrrjust)%rXr!r,r_pad_top pad_right _pad_bottompad_lefthorizontal_paddingr ends_on_nlprocessed_coderr syntax_lines syntax_linerend_line line_offsetr6numbers_column_widthrender_optionshighlight_line_Segmentr line_pointerrr(r)rr1 wrapped_linesr=wrapped_line_left_padfirst wrapped_line line_columns% rYr;zSyntax._get_syntaxs*"&!5!5!7!7!N59\2)[(% 1& $'"T%??!CC&  ! ! J'' %)%7%7 %B%B" N~~ndo>>     )""4((($$&&+11'::;D///"'(  ! ''*< '..t}E.JJ & + +">>'..z."B"B* '33NND&NQQ/" 4   $0++K********* F#>, H  1aa00K*.**Tz**R*R ? 0SZZ''+h./E   g&8 $$&&+11'::;D///"'( T e##DMeATATAT9T#UUt..  $9 j99-:8D>>&5Att6  # #G , ,    "'udo .KLL* #* #MGT~  ' 4 4"))v)FF*22 !5!!   G < <==? %-JMM!33$*4"2$: : 4%M  #(0..46F))%,6m+D+D # #'E< 4&)'ll&8&89MPQ9Q&R&RUX&X )>'22F"*(<U9K9K9K"L"LLLL"*(;8N"O"OOOOO"*(41G"H"HHHH"*(; "E"EEEEE3333++++++++"NNNN #%2##L++++++++"NNNN#Q* #* #r\rc|j}dgdtjd|tjDt |dz}|jD]p}t ||j}t ||j}|B|@|j r| |j ||T| |j ||qdS)z Apply stylized ranges to a text instance, using the given code to determine the right portion to apply the style to. Args: text (Text): Text instance to apply the style to. rc<g|]}|dzS)r%)r)rmatchs rY z1Syntax._apply_stylized_ranges.. s5 !r\r)flagsr%N) plainrefinditer MULTILINErr#_get_code_index_for_syntax_positionrrrstylize_beforerr )rXrrnewlines_offsetsstylized_rangerrs rYr zSyntax._apply_stylized_rangessz   [t2<HHH   IIM  #3 C CN7 ."6E6 ."4C S_!.C''())r\)rrrTrQ)rrrrrrirrrrrrrrrrrrrrrrrrtrrrrr$rTrk) rrrrrrrrrirrrrrrrrrrrrrrrrrrtrrrrr$rTrr)rrrrrrTrrra)rRrSrTr)rTr)rTr)rrrrrrTr4)F) rr3rrrrrrrTrk)r)rrrTr))rTr)r!r+rTr")r!r+r,r-rTr.)r!r+r,r,rTr.)r!r+r,r,rTr?)rr4rTrk)rrrrTrz)rbrcrdrer classmethodr DEFAULT_THEMErwrrrrrrpropertyrrr rrr r*r7r>r;r rHrfr\rYrrs(/...   [  *7"DH.2$(*.#%&!$@$@$@$@$@$@LoG -1)6"04.2$(*.#%&!< < < < [< |''''['R    X$   X EISSSSSt#      * - - - - -XFFFF.>>>>$H#H#H#H#TCCCCB******r\rrx Sequence[int]positionrrTrct|}|\}}||kst||dzkrdS|dz }||dz||z dz }t||}|||zS)a Returns the index of the code string for the given positions. Args: newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet. position (SyntaxPosition): The position to search for. Returns: Optional[int]: The index of the code string for this position, or `None` if the given position's line number is out of range (if it's the column that is out of range we silently clamp its value so that it reaches the end of the line) r%N)rmin)rxr lines_count line_number column_index line_index line_lengths rYrvrvIs&''K (K[  C(8$9$9[1_$M$MtqJ":>25Ej5QQTUUK{L11L J ', 66r\__main__z&Render syntax to the console with Rich) descriptionrPATHzpath to file, or - for stdin)metavarhelpz-cz --force-color force_color store_truezforce color for non-terminals)destactionrmrz-iz--indent-guidesrFzdisplay indent guidesz-lz--line-numbersrzrender line numbers)rrrz-wz--widthr5z*width of output (default will auto-detect))typerrmrz-rz--wraprzword wrap long linesz-sz --soft-wrap soft_wrapzenable soft wrapping mode)rrrmrz-tz--themerizpygments theme)rrmrz-bz--background-colorrtzOverride background colorz-xz--lexerrz Lexer name)rmrrz-pz --paddingrr#)rrmrrz--highlight-linerez)The line number (not index!) to highlight)r+)force_terminalr5-) rrrrrirtrrr)rrrrirtrrr)r)rxrrrrTr) __future__ros.pathrrssysr|abcrrpathlibrtypingrrr r r r r rrrrpygments.lexerrpygments.lexersrrpygments.styler PygmentsStylepygments.stylesrpygments.tokenrrrrrrrrrr pygments.utilr!rich.containersr" rich.paddingr#r$_loopr&cellsr(r9r)r*r!r+r,r-r.jupyterr/measurer0segmentr1r2rr3rr4rrrSplatformWINDOWSrPreprocWordBuiltinFunction NamespaceClass Exception DecoratorVariableConstant AttributeTagDeletedInsertedHeading SubheadingPromptrGrrLrrrQrhrrrrrrrvrbargparseArgumentParserparser add_argument parse_argsargs rich.consolerr5rstdinreadrrrrrirtrrresyntaxrprintrrfr\rYrs """"""" ########                          ! GGGGGGGG111111------                        ('''''!!!!!!33333333########IIIIIIIIIIII!!!!!! &&&&&&&&######## #s(O ,' ! & 5577&G$$$& UUt___& OUU((( &  UU &  L%%f%%% & M55y)))& L%%f%%%& M55w'''& NEE$777& JGt444& NEE'''& NEE 555& M55u%%%& M55u%%%&  NEE'''!&" Hee-(((#&$ EE ! ! ! EE    OUU... ee'*** OUU%%% ID999 NEEt$$$ M55|,,, 55u - - -5&& :% 5577%N+++% UUt___% OUU/// %  UU ' ' ' %  L%%m,,, % M55/000% L%%m,,,% M55~...% NEE >>>% JNd;;;% NEE ...% NEE 0t<<<% M55|,,,% M55|,,,%  NEE ...!%" Hee-(((#%$ EE ! ! ! EE & & & OUU... ee.111 OUU%%% $44@@@ NEEt$$$ M55|,,, 55u - - -5%% :%/YGG!" " " " " "# " " "'&'&'&'&'&+'&'&'&T&&&&&k&&&BsCx     J   ////////V *V *V *V *V *\V *V *V *r77776 zOOOJJJ $X $<F  +     ,     $     "      9     #     (   igy?O    (        kQYY    8      D$$$$$$gT%5TZHHHG yCy~~/*n*!2,L!01    !! I/*n*!2,L!01"    MM&DNM33333kr\