syntax.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. from __future__ import annotations
  2. import os.path
  3. import re
  4. import sys
  5. import textwrap
  6. from abc import ABC, abstractmethod
  7. from pathlib import Path
  8. from typing import (
  9. TYPE_CHECKING,
  10. Any,
  11. Dict,
  12. Iterable,
  13. List,
  14. NamedTuple,
  15. Optional,
  16. Sequence,
  17. Set,
  18. Tuple,
  19. Type,
  20. Union,
  21. )
  22. from pygments.lexer import Lexer
  23. from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename
  24. from pygments.style import Style as PygmentsStyle
  25. from pygments.styles import get_style_by_name
  26. from pygments.token import (
  27. Comment,
  28. Error,
  29. Generic,
  30. Keyword,
  31. Name,
  32. Number,
  33. Operator,
  34. String,
  35. Token,
  36. Whitespace,
  37. )
  38. from pygments.util import ClassNotFound
  39. if TYPE_CHECKING:
  40. from .console import Console, ConsoleOptions, JustifyMethod, RenderResult
  41. from rich.containers import Lines
  42. from rich.padding import Padding, PaddingDimensions
  43. from ._loop import loop_first
  44. from .cells import cell_len
  45. from .color import Color, blend_rgb
  46. from .jupyter import JupyterMixin
  47. from .measure import Measurement
  48. from .segment import Segment, Segments
  49. from .style import Style, StyleType
  50. from .text import Text
  51. TokenType = Tuple[str, ...]
  52. WINDOWS = sys.platform == "win32"
  53. DEFAULT_THEME = "monokai"
  54. # The following styles are based on https://github.com/pygments/pygments/blob/master/pygments/formatters/terminal.py
  55. # A few modifications were made
  56. ANSI_LIGHT: Dict[TokenType, Style] = {
  57. Token: Style(),
  58. Whitespace: Style(color="white"),
  59. Comment: Style(dim=True),
  60. Comment.Preproc: Style(color="cyan"),
  61. Keyword: Style(color="blue"),
  62. Keyword.Type: Style(color="cyan"),
  63. Operator.Word: Style(color="magenta"),
  64. Name.Builtin: Style(color="cyan"),
  65. Name.Function: Style(color="green"),
  66. Name.Namespace: Style(color="cyan", underline=True),
  67. Name.Class: Style(color="green", underline=True),
  68. Name.Exception: Style(color="cyan"),
  69. Name.Decorator: Style(color="magenta", bold=True),
  70. Name.Variable: Style(color="red"),
  71. Name.Constant: Style(color="red"),
  72. Name.Attribute: Style(color="cyan"),
  73. Name.Tag: Style(color="bright_blue"),
  74. String: Style(color="yellow"),
  75. Number: Style(color="blue"),
  76. Generic.Deleted: Style(color="bright_red"),
  77. Generic.Inserted: Style(color="green"),
  78. Generic.Heading: Style(bold=True),
  79. Generic.Subheading: Style(color="magenta", bold=True),
  80. Generic.Prompt: Style(bold=True),
  81. Generic.Error: Style(color="bright_red"),
  82. Error: Style(color="red", underline=True),
  83. }
  84. ANSI_DARK: Dict[TokenType, Style] = {
  85. Token: Style(),
  86. Whitespace: Style(color="bright_black"),
  87. Comment: Style(dim=True),
  88. Comment.Preproc: Style(color="bright_cyan"),
  89. Keyword: Style(color="bright_blue"),
  90. Keyword.Type: Style(color="bright_cyan"),
  91. Operator.Word: Style(color="bright_magenta"),
  92. Name.Builtin: Style(color="bright_cyan"),
  93. Name.Function: Style(color="bright_green"),
  94. Name.Namespace: Style(color="bright_cyan", underline=True),
  95. Name.Class: Style(color="bright_green", underline=True),
  96. Name.Exception: Style(color="bright_cyan"),
  97. Name.Decorator: Style(color="bright_magenta", bold=True),
  98. Name.Variable: Style(color="bright_red"),
  99. Name.Constant: Style(color="bright_red"),
  100. Name.Attribute: Style(color="bright_cyan"),
  101. Name.Tag: Style(color="bright_blue"),
  102. String: Style(color="yellow"),
  103. Number: Style(color="bright_blue"),
  104. Generic.Deleted: Style(color="bright_red"),
  105. Generic.Inserted: Style(color="bright_green"),
  106. Generic.Heading: Style(bold=True),
  107. Generic.Subheading: Style(color="bright_magenta", bold=True),
  108. Generic.Prompt: Style(bold=True),
  109. Generic.Error: Style(color="bright_red"),
  110. Error: Style(color="red", underline=True),
  111. }
  112. RICH_SYNTAX_THEMES = {"ansi_light": ANSI_LIGHT, "ansi_dark": ANSI_DARK}
  113. NUMBERS_COLUMN_DEFAULT_PADDING = 2
  114. class SyntaxTheme(ABC):
  115. """Base class for a syntax theme."""
  116. @abstractmethod
  117. def get_style_for_token(self, token_type: TokenType) -> Style:
  118. """Get a style for a given Pygments token."""
  119. raise NotImplementedError # pragma: no cover
  120. @abstractmethod
  121. def get_background_style(self) -> Style:
  122. """Get the background color."""
  123. raise NotImplementedError # pragma: no cover
  124. class PygmentsSyntaxTheme(SyntaxTheme):
  125. """Syntax theme that delegates to Pygments theme."""
  126. def __init__(self, theme: Union[str, Type[PygmentsStyle]]) -> None:
  127. self._style_cache: Dict[TokenType, Style] = {}
  128. if isinstance(theme, str):
  129. try:
  130. self._pygments_style_class = get_style_by_name(theme)
  131. except ClassNotFound:
  132. self._pygments_style_class = get_style_by_name("default")
  133. else:
  134. self._pygments_style_class = theme
  135. self._background_color = self._pygments_style_class.background_color
  136. self._background_style = Style(bgcolor=self._background_color)
  137. def get_style_for_token(self, token_type: TokenType) -> Style:
  138. """Get a style from a Pygments class."""
  139. try:
  140. return self._style_cache[token_type]
  141. except KeyError:
  142. try:
  143. pygments_style = self._pygments_style_class.style_for_token(token_type)
  144. except KeyError:
  145. style = Style.null()
  146. else:
  147. color = pygments_style["color"]
  148. bgcolor = pygments_style["bgcolor"]
  149. style = Style(
  150. color="#" + color if color else "#000000",
  151. bgcolor="#" + bgcolor if bgcolor else self._background_color,
  152. bold=pygments_style["bold"],
  153. italic=pygments_style["italic"],
  154. underline=pygments_style["underline"],
  155. )
  156. self._style_cache[token_type] = style
  157. return style
  158. def get_background_style(self) -> Style:
  159. return self._background_style
  160. class ANSISyntaxTheme(SyntaxTheme):
  161. """Syntax theme to use standard colors."""
  162. def __init__(self, style_map: Dict[TokenType, Style]) -> None:
  163. self.style_map = style_map
  164. self._missing_style = Style.null()
  165. self._background_style = Style.null()
  166. self._style_cache: Dict[TokenType, Style] = {}
  167. def get_style_for_token(self, token_type: TokenType) -> Style:
  168. """Look up style in the style map."""
  169. try:
  170. return self._style_cache[token_type]
  171. except KeyError:
  172. # Styles form a hierarchy
  173. # We need to go from most to least specific
  174. # e.g. ("foo", "bar", "baz") to ("foo", "bar") to ("foo",)
  175. get_style = self.style_map.get
  176. token = tuple(token_type)
  177. style = self._missing_style
  178. while token:
  179. _style = get_style(token)
  180. if _style is not None:
  181. style = _style
  182. break
  183. token = token[:-1]
  184. self._style_cache[token_type] = style
  185. return style
  186. def get_background_style(self) -> Style:
  187. return self._background_style
  188. SyntaxPosition = Tuple[int, int]
  189. class _SyntaxHighlightRange(NamedTuple):
  190. """
  191. A range to highlight in a Syntax object.
  192. `start` and `end` are 2-integers tuples, where the first integer is the line number
  193. (starting from 1) and the second integer is the column index (starting from 0).
  194. """
  195. style: StyleType
  196. start: SyntaxPosition
  197. end: SyntaxPosition
  198. style_before: bool = False
  199. class PaddingProperty:
  200. """Descriptor to get and set padding."""
  201. def __get__(self, obj: Syntax, objtype: Type[Syntax]) -> Tuple[int, int, int, int]:
  202. """Space around the Syntax."""
  203. return obj._padding
  204. def __set__(self, obj: Syntax, padding: PaddingDimensions) -> None:
  205. obj._padding = Padding.unpack(padding)
  206. class Syntax(JupyterMixin):
  207. """Construct a Syntax object to render syntax highlighted code.
  208. Args:
  209. code (str): Code to highlight.
  210. lexer (Lexer | str): Lexer to use (see https://pygments.org/docs/lexers/)
  211. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "monokai".
  212. dedent (bool, optional): Enable stripping of initial whitespace. Defaults to False.
  213. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
  214. start_line (int, optional): Starting number for line numbers. Defaults to 1.
  215. line_range (Tuple[int | None, int | None], optional): If given should be a tuple of the start and end line to render.
  216. A value of None in the tuple indicates the range is open in that direction.
  217. highlight_lines (Set[int]): A set of line numbers to highlight.
  218. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
  219. tab_size (int, optional): Size of tabs. Defaults to 4.
  220. word_wrap (bool, optional): Enable word wrapping.
  221. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None.
  222. indent_guides (bool, optional): Show indent guides. Defaults to False.
  223. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding).
  224. """
  225. _pygments_style_class: Type[PygmentsStyle]
  226. _theme: SyntaxTheme
  227. @classmethod
  228. def get_theme(cls, name: Union[str, SyntaxTheme]) -> SyntaxTheme:
  229. """Get a syntax theme instance."""
  230. if isinstance(name, SyntaxTheme):
  231. return name
  232. theme: SyntaxTheme
  233. if name in RICH_SYNTAX_THEMES:
  234. theme = ANSISyntaxTheme(RICH_SYNTAX_THEMES[name])
  235. else:
  236. theme = PygmentsSyntaxTheme(name)
  237. return theme
  238. def __init__(
  239. self,
  240. code: str,
  241. lexer: Union[Lexer, str],
  242. *,
  243. theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
  244. dedent: bool = False,
  245. line_numbers: bool = False,
  246. start_line: int = 1,
  247. line_range: Optional[Tuple[Optional[int], Optional[int]]] = None,
  248. highlight_lines: Optional[Set[int]] = None,
  249. code_width: Optional[int] = None,
  250. tab_size: int = 4,
  251. word_wrap: bool = False,
  252. background_color: Optional[str] = None,
  253. indent_guides: bool = False,
  254. padding: PaddingDimensions = 0,
  255. ) -> None:
  256. self.code = code
  257. self._lexer = lexer
  258. self.dedent = dedent
  259. self.line_numbers = line_numbers
  260. self.start_line = start_line
  261. self.line_range = line_range
  262. self.highlight_lines = highlight_lines or set()
  263. self.code_width = code_width
  264. self.tab_size = tab_size
  265. self.word_wrap = word_wrap
  266. self.background_color = background_color
  267. self.background_style = (
  268. Style(bgcolor=background_color) if background_color else Style()
  269. )
  270. self.indent_guides = indent_guides
  271. self._padding = Padding.unpack(padding)
  272. self._theme = self.get_theme(theme)
  273. self._stylized_ranges: List[_SyntaxHighlightRange] = []
  274. padding = PaddingProperty()
  275. @classmethod
  276. def from_path(
  277. cls,
  278. path: str,
  279. encoding: str = "utf-8",
  280. lexer: Optional[Union[Lexer, str]] = None,
  281. theme: Union[str, SyntaxTheme] = DEFAULT_THEME,
  282. dedent: bool = False,
  283. line_numbers: bool = False,
  284. line_range: Optional[Tuple[int, int]] = None,
  285. start_line: int = 1,
  286. highlight_lines: Optional[Set[int]] = None,
  287. code_width: Optional[int] = None,
  288. tab_size: int = 4,
  289. word_wrap: bool = False,
  290. background_color: Optional[str] = None,
  291. indent_guides: bool = False,
  292. padding: PaddingDimensions = 0,
  293. ) -> "Syntax":
  294. """Construct a Syntax object from a file.
  295. Args:
  296. path (str): Path to file to highlight.
  297. encoding (str): Encoding of file.
  298. lexer (str | Lexer, optional): Lexer to use. If None, lexer will be auto-detected from path/file content.
  299. theme (str, optional): Color theme, aka Pygments style (see https://pygments.org/docs/styles/#getting-a-list-of-available-styles). Defaults to "emacs".
  300. dedent (bool, optional): Enable stripping of initial whitespace. Defaults to True.
  301. line_numbers (bool, optional): Enable rendering of line numbers. Defaults to False.
  302. start_line (int, optional): Starting number for line numbers. Defaults to 1.
  303. line_range (Tuple[int, int], optional): If given should be a tuple of the start and end line to render.
  304. highlight_lines (Set[int]): A set of line numbers to highlight.
  305. code_width: Width of code to render (not including line numbers), or ``None`` to use all available width.
  306. tab_size (int, optional): Size of tabs. Defaults to 4.
  307. word_wrap (bool, optional): Enable word wrapping of code.
  308. background_color (str, optional): Optional background color, or None to use theme color. Defaults to None.
  309. indent_guides (bool, optional): Show indent guides. Defaults to False.
  310. padding (PaddingDimensions): Padding to apply around the syntax. Defaults to 0 (no padding).
  311. Returns:
  312. [Syntax]: A Syntax object that may be printed to the console
  313. """
  314. code = Path(path).read_text(encoding=encoding)
  315. if not lexer:
  316. lexer = cls.guess_lexer(path, code=code)
  317. return cls(
  318. code,
  319. lexer,
  320. theme=theme,
  321. dedent=dedent,
  322. line_numbers=line_numbers,
  323. line_range=line_range,
  324. start_line=start_line,
  325. highlight_lines=highlight_lines,
  326. code_width=code_width,
  327. tab_size=tab_size,
  328. word_wrap=word_wrap,
  329. background_color=background_color,
  330. indent_guides=indent_guides,
  331. padding=padding,
  332. )
  333. @classmethod
  334. def guess_lexer(cls, path: str, code: Optional[str] = None) -> str:
  335. """Guess the alias of the Pygments lexer to use based on a path and an optional string of code.
  336. If code is supplied, it will use a combination of the code and the filename to determine the
  337. best lexer to use. For example, if the file is ``index.html`` and the file contains Django
  338. templating syntax, then "html+django" will be returned. If the file is ``index.html``, and no
  339. templating language is used, the "html" lexer will be used. If no string of code
  340. is supplied, the lexer will be chosen based on the file extension..
  341. Args:
  342. path (AnyStr): The path to the file containing the code you wish to know the lexer for.
  343. code (str, optional): Optional string of code that will be used as a fallback if no lexer
  344. is found for the supplied path.
  345. Returns:
  346. str: The name of the Pygments lexer that best matches the supplied path/code.
  347. """
  348. lexer: Optional[Lexer] = None
  349. lexer_name = "default"
  350. if code:
  351. try:
  352. lexer = guess_lexer_for_filename(path, code)
  353. except ClassNotFound:
  354. pass
  355. if not lexer:
  356. try:
  357. _, ext = os.path.splitext(path)
  358. if ext:
  359. extension = ext.lstrip(".").lower()
  360. lexer = get_lexer_by_name(extension)
  361. except ClassNotFound:
  362. pass
  363. if lexer:
  364. if lexer.aliases:
  365. lexer_name = lexer.aliases[0]
  366. else:
  367. lexer_name = lexer.name
  368. return lexer_name
  369. def _get_base_style(self) -> Style:
  370. """Get the base style."""
  371. default_style = self._theme.get_background_style() + self.background_style
  372. return default_style
  373. def _get_token_color(self, token_type: TokenType) -> Optional[Color]:
  374. """Get a color (if any) for the given token.
  375. Args:
  376. token_type (TokenType): A token type tuple from Pygments.
  377. Returns:
  378. Optional[Color]: Color from theme, or None for no color.
  379. """
  380. style = self._theme.get_style_for_token(token_type)
  381. return style.color
  382. @property
  383. def lexer(self) -> Optional[Lexer]:
  384. """The lexer for this syntax, or None if no lexer was found.
  385. Tries to find the lexer by name if a string was passed to the constructor.
  386. """
  387. if isinstance(self._lexer, Lexer):
  388. return self._lexer
  389. try:
  390. return get_lexer_by_name(
  391. self._lexer,
  392. stripnl=False,
  393. ensurenl=True,
  394. tabsize=self.tab_size,
  395. )
  396. except ClassNotFound:
  397. return None
  398. @property
  399. def default_lexer(self) -> Lexer:
  400. """A Pygments Lexer to use if one is not specified or invalid."""
  401. return get_lexer_by_name(
  402. "text",
  403. stripnl=False,
  404. ensurenl=True,
  405. tabsize=self.tab_size,
  406. )
  407. def highlight(
  408. self,
  409. code: str,
  410. line_range: Optional[Tuple[Optional[int], Optional[int]]] = None,
  411. ) -> Text:
  412. """Highlight code and return a Text instance.
  413. Args:
  414. code (str): Code to highlight.
  415. line_range(Tuple[int, int], optional): Optional line range to highlight.
  416. Returns:
  417. Text: A text instance containing highlighted syntax.
  418. """
  419. base_style = self._get_base_style()
  420. justify: JustifyMethod = (
  421. "default" if base_style.transparent_background else "left"
  422. )
  423. text = Text(
  424. justify=justify,
  425. style=base_style,
  426. tab_size=self.tab_size,
  427. no_wrap=not self.word_wrap,
  428. )
  429. _get_theme_style = self._theme.get_style_for_token
  430. lexer = self.lexer or self.default_lexer
  431. if lexer is None:
  432. text.append(code)
  433. else:
  434. if line_range:
  435. # More complicated path to only stylize a portion of the code
  436. # This speeds up further operations as there are less spans to process
  437. line_start, line_end = line_range
  438. def line_tokenize() -> Iterable[Tuple[Any, str]]:
  439. """Split tokens to one per line."""
  440. assert lexer # required to make MyPy happy - we know lexer is not None at this point
  441. for token_type, token in lexer.get_tokens(code):
  442. while token:
  443. line_token, new_line, token = token.partition("\n")
  444. yield token_type, line_token + new_line
  445. def tokens_to_spans() -> Iterable[Tuple[str, Optional[Style]]]:
  446. """Convert tokens to spans."""
  447. tokens = iter(line_tokenize())
  448. line_no = 0
  449. _line_start = line_start - 1 if line_start else 0
  450. # Skip over tokens until line start
  451. while line_no < _line_start:
  452. try:
  453. _token_type, token = next(tokens)
  454. except StopIteration:
  455. break
  456. yield (token, None)
  457. if token.endswith("\n"):
  458. line_no += 1
  459. # Generate spans until line end
  460. for token_type, token in tokens:
  461. yield (token, _get_theme_style(token_type))
  462. if token.endswith("\n"):
  463. line_no += 1
  464. if line_end and line_no >= line_end:
  465. break
  466. text.append_tokens(tokens_to_spans())
  467. else:
  468. text.append_tokens(
  469. (token, _get_theme_style(token_type))
  470. for token_type, token in lexer.get_tokens(code)
  471. )
  472. if self.background_color is not None:
  473. text.stylize(f"on {self.background_color}")
  474. if self._stylized_ranges:
  475. self._apply_stylized_ranges(text)
  476. return text
  477. def stylize_range(
  478. self,
  479. style: StyleType,
  480. start: SyntaxPosition,
  481. end: SyntaxPosition,
  482. style_before: bool = False,
  483. ) -> None:
  484. """
  485. Adds a custom style on a part of the code, that will be applied to the syntax display when it's rendered.
  486. Line numbers are 1-based, while column indexes are 0-based.
  487. Args:
  488. style (StyleType): The style to apply.
  489. start (Tuple[int, int]): The start of the range, in the form `[line number, column index]`.
  490. end (Tuple[int, int]): The end of the range, in the form `[line number, column index]`.
  491. style_before (bool): Apply the style before any existing styles.
  492. """
  493. self._stylized_ranges.append(
  494. _SyntaxHighlightRange(style, start, end, style_before)
  495. )
  496. def _get_line_numbers_color(self, blend: float = 0.3) -> Color:
  497. background_style = self._theme.get_background_style() + self.background_style
  498. background_color = background_style.bgcolor
  499. if background_color is None or background_color.is_system_defined:
  500. return Color.default()
  501. foreground_color = self._get_token_color(Token.Text)
  502. if foreground_color is None or foreground_color.is_system_defined:
  503. return foreground_color or Color.default()
  504. new_color = blend_rgb(
  505. background_color.get_truecolor(),
  506. foreground_color.get_truecolor(),
  507. cross_fade=blend,
  508. )
  509. return Color.from_triplet(new_color)
  510. @property
  511. def _numbers_column_width(self) -> int:
  512. """Get the number of characters used to render the numbers column."""
  513. column_width = 0
  514. if self.line_numbers:
  515. column_width = (
  516. len(str(self.start_line + self.code.count("\n")))
  517. + NUMBERS_COLUMN_DEFAULT_PADDING
  518. )
  519. return column_width
  520. def _get_number_styles(self, console: Console) -> Tuple[Style, Style, Style]:
  521. """Get background, number, and highlight styles for line numbers."""
  522. background_style = self._get_base_style()
  523. if background_style.transparent_background:
  524. return Style.null(), Style(dim=True), Style.null()
  525. if console.color_system in ("256", "truecolor"):
  526. number_style = Style.chain(
  527. background_style,
  528. self._theme.get_style_for_token(Token.Text),
  529. Style(color=self._get_line_numbers_color()),
  530. self.background_style,
  531. )
  532. highlight_number_style = Style.chain(
  533. background_style,
  534. self._theme.get_style_for_token(Token.Text),
  535. Style(bold=True, color=self._get_line_numbers_color(0.9)),
  536. self.background_style,
  537. )
  538. else:
  539. number_style = background_style + Style(dim=True)
  540. highlight_number_style = background_style + Style(dim=False)
  541. return background_style, number_style, highlight_number_style
  542. def __rich_measure__(
  543. self, console: "Console", options: "ConsoleOptions"
  544. ) -> "Measurement":
  545. _, right, _, left = self.padding
  546. padding = left + right
  547. if self.code_width is not None:
  548. width = self.code_width + self._numbers_column_width + padding + 1
  549. return Measurement(self._numbers_column_width, width)
  550. lines = self.code.splitlines()
  551. width = (
  552. self._numbers_column_width
  553. + padding
  554. + (max(cell_len(line) for line in lines) if lines else 0)
  555. )
  556. if self.line_numbers:
  557. width += 1
  558. return Measurement(self._numbers_column_width, width)
  559. def __rich_console__(
  560. self, console: Console, options: ConsoleOptions
  561. ) -> RenderResult:
  562. segments = Segments(self._get_syntax(console, options))
  563. if any(self.padding):
  564. yield Padding(segments, style=self._get_base_style(), pad=self.padding)
  565. else:
  566. yield segments
  567. def _get_syntax(
  568. self,
  569. console: Console,
  570. options: ConsoleOptions,
  571. ) -> Iterable[Segment]:
  572. """
  573. Get the Segments for the Syntax object, excluding any vertical/horizontal padding
  574. """
  575. transparent_background = self._get_base_style().transparent_background
  576. _pad_top, pad_right, _pad_bottom, pad_left = self.padding
  577. horizontal_padding = pad_left + pad_right
  578. code_width = (
  579. (
  580. (options.max_width - self._numbers_column_width - 1)
  581. if self.line_numbers
  582. else options.max_width
  583. )
  584. - horizontal_padding
  585. if self.code_width is None
  586. else self.code_width
  587. )
  588. code_width = max(0, code_width)
  589. ends_on_nl, processed_code = self._process_code(self.code)
  590. text = self.highlight(processed_code, self.line_range)
  591. if not self.line_numbers and not self.word_wrap and not self.line_range:
  592. if not ends_on_nl:
  593. text.remove_suffix("\n")
  594. # Simple case of just rendering text
  595. style = (
  596. self._get_base_style()
  597. + self._theme.get_style_for_token(Comment)
  598. + Style(dim=True)
  599. + self.background_style
  600. )
  601. if self.indent_guides and not options.ascii_only:
  602. text = text.with_indent_guides(self.tab_size, style=style)
  603. text.overflow = "crop"
  604. if style.transparent_background:
  605. yield from console.render(
  606. text, options=options.update(width=code_width)
  607. )
  608. else:
  609. syntax_lines = console.render_lines(
  610. text,
  611. options.update(width=code_width, height=None, justify="left"),
  612. style=self.background_style,
  613. pad=True,
  614. new_lines=True,
  615. )
  616. for syntax_line in syntax_lines:
  617. yield from syntax_line
  618. return
  619. start_line, end_line = self.line_range or (None, None)
  620. line_offset = 0
  621. if start_line:
  622. line_offset = max(0, start_line - 1)
  623. lines: Union[List[Text], Lines] = text.split("\n", allow_blank=ends_on_nl)
  624. if self.line_range:
  625. if line_offset > len(lines):
  626. return
  627. lines = lines[line_offset:end_line]
  628. if self.indent_guides and not options.ascii_only:
  629. style = (
  630. self._get_base_style()
  631. + self._theme.get_style_for_token(Comment)
  632. + Style(dim=True)
  633. + self.background_style
  634. )
  635. lines = (
  636. Text("\n")
  637. .join(lines)
  638. .with_indent_guides(self.tab_size, style=style + Style(italic=False))
  639. .split("\n", allow_blank=True)
  640. )
  641. numbers_column_width = self._numbers_column_width
  642. render_options = options.update(width=code_width)
  643. highlight_line = self.highlight_lines.__contains__
  644. _Segment = Segment
  645. new_line = _Segment("\n")
  646. line_pointer = "> " if options.legacy_windows else "❱ "
  647. (
  648. background_style,
  649. number_style,
  650. highlight_number_style,
  651. ) = self._get_number_styles(console)
  652. for line_no, line in enumerate(lines, self.start_line + line_offset):
  653. if self.word_wrap:
  654. wrapped_lines = console.render_lines(
  655. line,
  656. render_options.update(height=None, justify="left"),
  657. style=background_style,
  658. pad=not transparent_background,
  659. )
  660. else:
  661. segments = list(line.render(console, end=""))
  662. if options.no_wrap:
  663. wrapped_lines = [segments]
  664. else:
  665. wrapped_lines = [
  666. _Segment.adjust_line_length(
  667. segments,
  668. render_options.max_width,
  669. style=background_style,
  670. pad=not transparent_background,
  671. )
  672. ]
  673. if self.line_numbers:
  674. wrapped_line_left_pad = _Segment(
  675. " " * numbers_column_width + " ", background_style
  676. )
  677. for first, wrapped_line in loop_first(wrapped_lines):
  678. if first:
  679. line_column = str(line_no).rjust(numbers_column_width - 2) + " "
  680. if highlight_line(line_no):
  681. yield _Segment(line_pointer, Style(color="red"))
  682. yield _Segment(line_column, highlight_number_style)
  683. else:
  684. yield _Segment(" ", highlight_number_style)
  685. yield _Segment(line_column, number_style)
  686. else:
  687. yield wrapped_line_left_pad
  688. yield from wrapped_line
  689. yield new_line
  690. else:
  691. for wrapped_line in wrapped_lines:
  692. yield from wrapped_line
  693. yield new_line
  694. def _apply_stylized_ranges(self, text: Text) -> None:
  695. """
  696. Apply stylized ranges to a text instance,
  697. using the given code to determine the right portion to apply the style to.
  698. Args:
  699. text (Text): Text instance to apply the style to.
  700. """
  701. code = text.plain
  702. newlines_offsets = [
  703. # Let's add outer boundaries at each side of the list:
  704. 0,
  705. # N.B. using "\n" here is much faster than using metacharacters such as "^" or "\Z":
  706. *[
  707. match.start() + 1
  708. for match in re.finditer("\n", code, flags=re.MULTILINE)
  709. ],
  710. len(code) + 1,
  711. ]
  712. for stylized_range in self._stylized_ranges:
  713. start = _get_code_index_for_syntax_position(
  714. newlines_offsets, stylized_range.start
  715. )
  716. end = _get_code_index_for_syntax_position(
  717. newlines_offsets, stylized_range.end
  718. )
  719. if start is not None and end is not None:
  720. if stylized_range.style_before:
  721. text.stylize_before(stylized_range.style, start, end)
  722. else:
  723. text.stylize(stylized_range.style, start, end)
  724. def _process_code(self, code: str) -> Tuple[bool, str]:
  725. """
  726. Applies various processing to a raw code string
  727. (normalises it so it always ends with a line return, dedents it if necessary, etc.)
  728. Args:
  729. code (str): The raw code string to process
  730. Returns:
  731. Tuple[bool, str]: the boolean indicates whether the raw code ends with a line return,
  732. while the string is the processed code.
  733. """
  734. ends_on_nl = code.endswith("\n")
  735. processed_code = code if ends_on_nl else code + "\n"
  736. processed_code = (
  737. textwrap.dedent(processed_code) if self.dedent else processed_code
  738. )
  739. processed_code = processed_code.expandtabs(self.tab_size)
  740. return ends_on_nl, processed_code
  741. def _get_code_index_for_syntax_position(
  742. newlines_offsets: Sequence[int], position: SyntaxPosition
  743. ) -> Optional[int]:
  744. """
  745. Returns the index of the code string for the given positions.
  746. Args:
  747. newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet.
  748. position (SyntaxPosition): The position to search for.
  749. Returns:
  750. Optional[int]: The index of the code string for this position, or `None`
  751. if the given position's line number is out of range (if it's the column that is out of range
  752. we silently clamp its value so that it reaches the end of the line)
  753. """
  754. lines_count = len(newlines_offsets)
  755. line_number, column_index = position
  756. if line_number > lines_count or len(newlines_offsets) < (line_number + 1):
  757. return None # `line_number` is out of range
  758. line_index = line_number - 1
  759. line_length = newlines_offsets[line_index + 1] - newlines_offsets[line_index] - 1
  760. # If `column_index` is out of range: let's silently clamp it:
  761. column_index = min(line_length, column_index)
  762. return newlines_offsets[line_index] + column_index
  763. if __name__ == "__main__": # pragma: no cover
  764. import argparse
  765. import sys
  766. parser = argparse.ArgumentParser(
  767. description="Render syntax to the console with Rich"
  768. )
  769. parser.add_argument(
  770. "path",
  771. metavar="PATH",
  772. help="path to file, or - for stdin",
  773. )
  774. parser.add_argument(
  775. "-c",
  776. "--force-color",
  777. dest="force_color",
  778. action="store_true",
  779. default=None,
  780. help="force color for non-terminals",
  781. )
  782. parser.add_argument(
  783. "-i",
  784. "--indent-guides",
  785. dest="indent_guides",
  786. action="store_true",
  787. default=False,
  788. help="display indent guides",
  789. )
  790. parser.add_argument(
  791. "-l",
  792. "--line-numbers",
  793. dest="line_numbers",
  794. action="store_true",
  795. help="render line numbers",
  796. )
  797. parser.add_argument(
  798. "-w",
  799. "--width",
  800. type=int,
  801. dest="width",
  802. default=None,
  803. help="width of output (default will auto-detect)",
  804. )
  805. parser.add_argument(
  806. "-r",
  807. "--wrap",
  808. dest="word_wrap",
  809. action="store_true",
  810. default=False,
  811. help="word wrap long lines",
  812. )
  813. parser.add_argument(
  814. "-s",
  815. "--soft-wrap",
  816. action="store_true",
  817. dest="soft_wrap",
  818. default=False,
  819. help="enable soft wrapping mode",
  820. )
  821. parser.add_argument(
  822. "-t", "--theme", dest="theme", default="monokai", help="pygments theme"
  823. )
  824. parser.add_argument(
  825. "-b",
  826. "--background-color",
  827. dest="background_color",
  828. default=None,
  829. help="Override background color",
  830. )
  831. parser.add_argument(
  832. "-x",
  833. "--lexer",
  834. default=None,
  835. dest="lexer_name",
  836. help="Lexer name",
  837. )
  838. parser.add_argument(
  839. "-p", "--padding", type=int, default=0, dest="padding", help="Padding"
  840. )
  841. parser.add_argument(
  842. "--highlight-line",
  843. type=int,
  844. default=None,
  845. dest="highlight_line",
  846. help="The line number (not index!) to highlight",
  847. )
  848. args = parser.parse_args()
  849. from rich.console import Console
  850. console = Console(force_terminal=args.force_color, width=args.width)
  851. if args.path == "-":
  852. code = sys.stdin.read()
  853. syntax = Syntax(
  854. code=code,
  855. lexer=args.lexer_name,
  856. line_numbers=args.line_numbers,
  857. word_wrap=args.word_wrap,
  858. theme=args.theme,
  859. background_color=args.background_color,
  860. indent_guides=args.indent_guides,
  861. padding=args.padding,
  862. highlight_lines={args.highlight_line},
  863. )
  864. else:
  865. syntax = Syntax.from_path(
  866. args.path,
  867. lexer=args.lexer_name,
  868. line_numbers=args.line_numbers,
  869. word_wrap=args.word_wrap,
  870. theme=args.theme,
  871. background_color=args.background_color,
  872. indent_guides=args.indent_guides,
  873. padding=args.padding,
  874. highlight_lines={args.highlight_line},
  875. )
  876. console.print(syntax, soft_wrap=args.soft_wrap)