o 7]hri@sUddlZddlZddlmZddlZddlmZmZddl m Z m Z m Z m Z mZmZmZmZmZddlmZddlmZmZddlmZddlmZdd lmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'dd l(m)Z)d d l*m+Z+d d l,m-Z-m.Z.d dl/m0Z0m1Z1m2Z2m3Z3d dl4m5Z5d dl6m7Z7d dl8m9Z9d dl:mZd dl;me?dkZ@dZAie&ee'eddeeddejBedde!edde!jedde$jCedde"jDedde"jEedde"jFeddde"jGeddde"jHedde"jIeddde"jJed de"jKed de"jLedde"jMed!de%ed"de#edde jNed#de jOedde jPedd$e jQeddde jRedd$e jed#deed ddi ZSe e>efeTd%<ie&ee'ed&deeddejBed'de!ed!de!jed'de$jCed(de"jDed'de"jEed)de"jFed'dde"jGed)dde"jHed'de"jIed(dde"jJed#de"jKed#de"jLed'de"jMed!de%ed"de#ed!de jNed#de jOed)de jPedd$e jQed(dde jRedd$e jed#deed ddi ZUe e>efeTd*<eSeUd+ZVGd,d-d-eZWGd.d/d/eWZXGd0d1d1eWZYGd2d3d3e5ZZe[d4krddl\Z\ddl]Z]e\j^d5d6Z_e_j`d7d8d9d:e_j`d;ddd?d@e_j`dAdBdCd>dDdEd@e_j`dFdGdHd>dIdJe_j`dKdLeadMddNdOe_j`dPdQdRd>dDdSd@e_j`dTdUd>dVdDdWdXe_j`dYdZd[dd\d]e_j`d^d_d`ddad]e_j`dbdcdddedfdge_bZcddhldm0Z0e0ecjeecjfdiZ/ecjgdjkre]jhiZjeZejecjkecjlecjmecjnecjoecjpdkZqneZjrecjgecjlecjmecjnecjoecjpdlZqe/jseqecjtdmdSdS)nN)Lines)ABCabstractmethod) AnyDictIterableListOptionalSetTupleTypeUnion)Lexer)get_lexer_by_nameguess_lexer_for_filename)Style)get_style_by_name) CommentErrorGenericKeywordNameNumberOperatorStringToken Whitespace) ClassNotFound) loop_first)Color blend_rgb)ConsoleConsoleOptions JustifyMethod RenderResult) JupyterMixin) Measurement)Segment)Text.WindowsmonokaiwhitecolorTdimcyanbluemagentagreen)r. underline)r.boldred bright_blueyellow bright_red)r6 ANSI_LIGHT bright_black bright_cyanbright_magenta bright_green ANSI_DARK) ansi_light ansi_darkc@s8eZdZdZededefddZedefddZdS) SyntaxThemezBase class for a syntax theme. token_typereturncCt)z'Get a style for a given Pygments token.NotImplementedError)selfrDrJ9/usr/lib/python3/dist-packages/pip/_vendor/rich/syntax.pyget_style_for_tokenkzSyntaxTheme.get_style_for_tokencCrF)zGet the background color.rGrIrJrJrKget_background_styleprMz SyntaxTheme.get_background_styleN) __name__ __module__ __qualname____doc__r TokenTyperrLrOrJrJrJrKrChs rCc@sNeZdZdZdeeeefddfddZde de fdd Z de fd d Z dS) PygmentsSyntaxThemez.Syntax theme that delegates to Pygments theme.themerENcCs^i|_t|trzt||_Wntytd|_Ynw||_|jj|_t|jd|_ dS)Ndefaultbgcolor) _style_cache isinstancestrr_pygments_style_classrbackground_color_background_colorr_background_style)rIrVrJrJrK__init__ys   zPygmentsSyntaxTheme.__init__rDc Csz|j|WStyOz|j|}Wn ty"t}Yn%w|d}|d}t|r2d|nd|r9d|n|j|d|d|dd}||j|<Y|Sw) z"Get a style from a Pygments class.r.rY#z#000000r6italicr5)r.rYr6rcr5)rZKeyErrorr]style_for_tokenrnullr_)rIrDpygments_stylestyler.rYrJrJrKrLs(     z'PygmentsSyntaxTheme.get_style_for_tokencC|jSNr`rNrJrJrKrOz(PygmentsSyntaxTheme.get_background_style) rPrQrRrSr r\r PygmentsStylerarTrrLrOrJrJrJrKrUvs  rUc@sJeZdZdZdeeefddfddZdedefdd Zdefd d Z dS) ANSISyntaxThemez$Syntax theme to use standard colors. style_maprENcCs$||_t|_t|_i|_dSrj)rorrf_missing_styler`rZ)rIrorJrJrKras   zANSISyntaxTheme.__init__rDcCsnz|j|WSty6|jj}t|}|j}|r-||}|dur%|}n|dd}|s||j|<|YSw)zLook up style in the style map.N)rZrdrogettuplerp)rIrD get_styletokenrh_stylerJrJrKrLs     z#ANSISyntaxTheme.get_style_for_tokencCrirjrkrNrJrJrKrOrlz$ANSISyntaxTheme.get_background_style) rPrQrRrSrrTrrarLrOrJrJrJrKrns rnc@seZdZUdZeeed<eed<ede e efdefddZ e ddd d d d d dd dd d e de e e fde e efdedededeeeefdeeedeedededee dedd fddZede ddd d d d d dd df de de de e efdededeeeefdedeeedeedededee deddfdd Zdefd!d"Zd#edeefd$d%Zedee fd&d'Z d;d e deeeefdefd(d)ZdSyntaxaConstruct 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, 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. 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. r]_themenamerEcCs2t|tr|S|tvrtt|}|St|}|S)zGet a syntax theme instance.)r[rCRICH_SYNTAX_THEMESrnrU)clsryrVrJrJrK get_themes  zSyntax.get_themeFrN) rVdedent line_numbers start_line line_rangehighlight_lines code_widthtab_size word_wrapr^ indent_guidescodelexerrVr~rrrrrrrr^rc Cst||_||_||_||_||_||_|pt|_| |_| |_ | |_ | |_ | r+t | dnt |_ | |_|||_dS)NrX)r_lexerr~rrrsetrrrrr^rbackground_stylerr|rx)rIrrrVr~rrrrrrrr^rrJrJrKras zSyntax.__init__zutf-8pathencodingcCst|d|d }|}Wdn1swYd}d}ztj|\}}|r8|d}t|}|j}Wn t yBYnw|durYzt ||j}Wn t yXYnw|||||||||| | | | | d S)a?Construct a Syntax object from a file. Args: path (str): Path to file to highlight. encoding (str): Encoding of file. 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. Returns: [Syntax]: A Syntax object that may be printed to the console rt)rNrW.) rVr~rrrrrrrr^r) openreadosrsplitextlstriplowerrryrr)r{rrrVr~rrrrrrrr^r code_filerr lexer_name_ext extensionrJrJrK from_pathsH%   zSyntax.from_pathcCs|j|j}|S)zGet the base style.)rxrOr)rI default_stylerJrJrK_get_base_styleOszSyntax._get_base_stylerDcCs|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. )rxrLr.)rIrDrhrJrJrK_get_token_colorTs zSyntax._get_token_colorcCs>t|jtr |jSz t|jdd|jdWStyYdSw)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. FT)stripnlensurenltabsizeN)r[rrrrrrNrJrJrKr`s  z Syntax.lexercs|}|jr dnd}t|||j|j d}|jj|jdur(||S|rY|\dt t t t fffdd dt t t t tfffdd }||n|fd d D|jdurv|d |j|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. rWleft)justifyrhrno_wrapNrEc3sHsJD]\}}|r!|d\}}}|||fV|sq dS)zSplit tokens to one per line. N) get_tokens partition)rDru line_tokennew_line)rrrJrK line_tokenizesz'Syntax.highlight..line_tokenizec3st}d}d}||kr(t|\}}|dfV|dr$|d7}||ks|D]\}}||fV|drE|d7}|krEdSq*dS)zConvert tokens to spans.rrNr)iternextendswith)tokensline_no _line_start _token_typerurD)_get_theme_styleline_end line_startrrJrKtokens_to_spanss$      z)Syntax.highlight..tokens_to_spansc3s |] \}}||fVqdSrjrJ).0rDru)rrJrK s  z#Syntax.highlight..zon )rtransparent_backgroundr)rrrxrLrappendrr rr\r r append_tokensrr^stylize)rIrr base_stylertextrrJ)rrrrrrrK highlightss2  - (  zSyntax.highlight333333?blendcCsr|j|j}|j}|dus|jrtS|tj }|dus#|jr)|p(tSt | | |d}t |S)N) cross_fade) rxrOrrYis_system_definedr rWrrr)r! get_truecolor from_triplet)rIrrr^foreground_color new_colorrJrJrK_get_line_numbers_colors   zSyntax._get_line_numbers_colorcCs,d}|jrtt|j|jdd}|S)z?Get the number of characters used to render the numbers column.rr)rlenr\rrcount)rI column_widthrJrJrK_numbers_column_widthszSyntax._numbers_column_widthconsolec Cs|}|jrttddtfS|jdvrAt||jtj t| d|j }t||jtj td| dd|j }n|tdd}|tdd}|||fS)z>Get background, number, and highlight styles for line numbers.Tr/)256 truecolorr-g?)r6r.F) rrrrf color_systemchainrxrLrr)rr)rIrr number_stylehighlight_number_stylerJrJrK_get_number_styless&     zSyntax._get_number_stylesr"optionsr#r'cCs0|jdur|j|j}t|j|St|j|jSrj)rrr' max_width)rIrrwidthrJrJrK__rich_measure__s   zSyntax.__rich_measure__c csj|j}|jdur|jr|j|jdn|jn|j}d}|jr-|j\}}td|d}|j d}|r8|jn|jd} |j rEt | n| } | |j } || |j} ||\} } } |js|js|js|sl| d||jttdd|j}|jr|js| j|j |d} d| _|jr|j| |j|dd EdHdS|j| |j|dd |jddd }|D]}|EdHqdS| jd|d }|jr|||}|jr|js||jttdd|j}td |j|j |djddd }|j}|j|d}|j!j"}t#}|d |d | }|d}|j$rdnd}t%||j&|D]\}}|jr;|j||jdd| | d}nt'|j|dd}|j(rL|g}n |j)||j| | dg}|jrt*|D]@\}}|rt+|,|dd }||r||tddV||| Vn|d| V||| Vn|V|EdH|Vq`q$|D] }|EdH|Vqq$dS)NrrrTr/)rhcrop)r)r)rheight)rhpad new_lines) allow_blank z> u❱ )r)rhr)endrr7r-z )-rrrrrrrmaxrrr~textwrap expandtabsrrrr remove_suffixrxrLrrrr ascii_onlywith_indent_guidesoverflowrenderupdate render_linessplitr)joinr __contains__r(legacy_windows enumeraterlistradjust_line_lengthrr\rjust) rIrrrr line_offsetrend_line ends_on_nlrrrrrrh syntax_lines syntax_linelinesnumbers_column_widthrender_optionshighlight_line_Segmentpaddingr line_pointerrline wrapped_linessegmentsfirst wrapped_line line_columnrJrJrK__rich_console__s                         zSyntax.__rich_console__rj)r)rr"rr#rEr')'rPrQrRrSr rm__annotations__rC classmethodr r\r| DEFAULT_THEMErboolintr r r rarrrrTr rpropertyrr)rfloatrrr"rrr#r%rrJrJrJrKrws          #       H  L rw__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)destactionrWrz-iz--indent-guidesrFzdisplay indent guidesz-lz--line-numbersrzrender line numbers)rrrz-wz--widthrz*width of output (default will auto-detect))typerrWrz-rz--wraprzword wrap long linesz-sz --soft-wrap soft_wrapzenable soft wrapping mode)rrrWrz-tz--themerVzpygments theme)rrWrz-bz--background-colorr^zOverride background colorz-xz--lexerrWrz Lexer name)rWrr)r")force_terminalr-)rrrrrVr^r)rrrVr^r)r)uos.pathrplatformpip._vendor.rich.containersrrabcrrtypingrrrrr r r r r pip._vendor.pygments.lexerrpip._vendor.pygments.lexersrrpip._vendor.pygments.stylerrmpip._vendor.pygments.stylesrpip._vendor.pygments.tokenrrrrrrrrrrpip._vendor.pygments.utilr_looprr.r r!rr"r#r$r%jupyterr&measurer'segmentr(rhrr)r\rTsystemWINDOWSrPreprocWordBuiltinFunction NamespaceClass Exception DecoratorVariableConstant AttributeTagDeletedInsertedHeading SubheadingPromptr;rr@rzrCrUrnrwrPargparsesysArgumentParserparser add_argumentr  parse_argsargspip._vendor.rich.consolerrrstdinrrrrrrVr^rsyntaxrprintrrJrJrJrKs  ,   0                                                         *! :