text.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. import re
  2. from functools import partial, reduce
  3. from math import gcd
  4. from operator import itemgetter
  5. from typing import (
  6. TYPE_CHECKING,
  7. Any,
  8. Callable,
  9. Dict,
  10. Iterable,
  11. List,
  12. NamedTuple,
  13. Optional,
  14. Tuple,
  15. Union,
  16. )
  17. from ._loop import loop_last
  18. from ._pick import pick_bool
  19. from ._wrap import divide_line
  20. from .align import AlignMethod
  21. from .cells import cell_len, set_cell_size
  22. from .containers import Lines
  23. from .control import strip_control_codes
  24. from .emoji import EmojiVariant
  25. from .jupyter import JupyterMixin
  26. from .measure import Measurement
  27. from .segment import Segment
  28. from .style import Style, StyleType
  29. if TYPE_CHECKING: # pragma: no cover
  30. from .console import Console, ConsoleOptions, JustifyMethod, OverflowMethod
  31. DEFAULT_JUSTIFY: "JustifyMethod" = "default"
  32. DEFAULT_OVERFLOW: "OverflowMethod" = "fold"
  33. _re_whitespace = re.compile(r"\s+$")
  34. TextType = Union[str, "Text"]
  35. GetStyleCallable = Callable[[str], Optional[StyleType]]
  36. class Span(NamedTuple):
  37. """A marked up region in some text."""
  38. start: int
  39. """Span start index."""
  40. end: int
  41. """Span end index."""
  42. style: Union[str, Style]
  43. """Style associated with the span."""
  44. def __repr__(self) -> str:
  45. return f"Span({self.start}, {self.end}, {self.style!r})"
  46. def __bool__(self) -> bool:
  47. return self.end > self.start
  48. def split(self, offset: int) -> Tuple["Span", Optional["Span"]]:
  49. """Split a span in to 2 from a given offset."""
  50. if offset < self.start:
  51. return self, None
  52. if offset >= self.end:
  53. return self, None
  54. start, end, style = self
  55. span1 = Span(start, min(end, offset), style)
  56. span2 = Span(span1.end, end, style)
  57. return span1, span2
  58. def move(self, offset: int) -> "Span":
  59. """Move start and end by a given offset.
  60. Args:
  61. offset (int): Number of characters to add to start and end.
  62. Returns:
  63. TextSpan: A new TextSpan with adjusted position.
  64. """
  65. start, end, style = self
  66. return Span(start + offset, end + offset, style)
  67. def right_crop(self, offset: int) -> "Span":
  68. """Crop the span at the given offset.
  69. Args:
  70. offset (int): A value between start and end.
  71. Returns:
  72. Span: A new (possibly smaller) span.
  73. """
  74. start, end, style = self
  75. if offset >= end:
  76. return self
  77. return Span(start, min(offset, end), style)
  78. class Text(JupyterMixin):
  79. """Text with color / style.
  80. Args:
  81. text (str, optional): Default unstyled text. Defaults to "".
  82. style (Union[str, Style], optional): Base style for text. Defaults to "".
  83. justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
  84. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
  85. no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
  86. end (str, optional): Character to end text with. Defaults to "\\\\n".
  87. tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to 8.
  88. spans (List[Span], optional). A list of predefined style spans. Defaults to None.
  89. """
  90. __slots__ = [
  91. "_text",
  92. "style",
  93. "justify",
  94. "overflow",
  95. "no_wrap",
  96. "end",
  97. "tab_size",
  98. "_spans",
  99. "_length",
  100. ]
  101. def __init__(
  102. self,
  103. text: str = "",
  104. style: Union[str, Style] = "",
  105. *,
  106. justify: Optional["JustifyMethod"] = None,
  107. overflow: Optional["OverflowMethod"] = None,
  108. no_wrap: Optional[bool] = None,
  109. end: str = "\n",
  110. tab_size: Optional[int] = 8,
  111. spans: Optional[List[Span]] = None,
  112. ) -> None:
  113. sanitized_text = strip_control_codes(text)
  114. self._text = [sanitized_text]
  115. self.style = style
  116. self.justify: Optional["JustifyMethod"] = justify
  117. self.overflow: Optional["OverflowMethod"] = overflow
  118. self.no_wrap = no_wrap
  119. self.end = end
  120. self.tab_size = tab_size
  121. self._spans: List[Span] = spans or []
  122. self._length: int = len(sanitized_text)
  123. def __len__(self) -> int:
  124. return self._length
  125. def __bool__(self) -> bool:
  126. return bool(self._length)
  127. def __str__(self) -> str:
  128. return self.plain
  129. def __repr__(self) -> str:
  130. return f"<text {self.plain!r} {self._spans!r}>"
  131. def __add__(self, other: Any) -> "Text":
  132. if isinstance(other, (str, Text)):
  133. result = self.copy()
  134. result.append(other)
  135. return result
  136. return NotImplemented
  137. def __eq__(self, other: object) -> bool:
  138. if not isinstance(other, Text):
  139. return NotImplemented
  140. return self.plain == other.plain and self._spans == other._spans
  141. def __contains__(self, other: object) -> bool:
  142. if isinstance(other, str):
  143. return other in self.plain
  144. elif isinstance(other, Text):
  145. return other.plain in self.plain
  146. return False
  147. def __getitem__(self, slice: Union[int, slice]) -> "Text":
  148. def get_text_at(offset: int) -> "Text":
  149. _Span = Span
  150. text = Text(
  151. self.plain[offset],
  152. spans=[
  153. _Span(0, 1, style)
  154. for start, end, style in self._spans
  155. if end > offset >= start
  156. ],
  157. end="",
  158. )
  159. return text
  160. if isinstance(slice, int):
  161. return get_text_at(slice)
  162. else:
  163. start, stop, step = slice.indices(len(self.plain))
  164. if step == 1:
  165. lines = self.divide([start, stop])
  166. return lines[1]
  167. else:
  168. # This would be a bit of work to implement efficiently
  169. # For now, its not required
  170. raise TypeError("slices with step!=1 are not supported")
  171. @property
  172. def cell_len(self) -> int:
  173. """Get the number of cells required to render this text."""
  174. return cell_len(self.plain)
  175. @property
  176. def markup(self) -> str:
  177. """Get console markup to render this Text.
  178. Returns:
  179. str: A string potentially creating markup tags.
  180. """
  181. from .markup import escape
  182. output: List[str] = []
  183. plain = self.plain
  184. markup_spans = [
  185. (0, False, self.style),
  186. *((span.start, False, span.style) for span in self._spans),
  187. *((span.end, True, span.style) for span in self._spans),
  188. (len(plain), True, self.style),
  189. ]
  190. markup_spans.sort(key=itemgetter(0, 1))
  191. position = 0
  192. append = output.append
  193. for offset, closing, style in markup_spans:
  194. if offset > position:
  195. append(escape(plain[position:offset]))
  196. position = offset
  197. if style:
  198. append(f"[/{style}]" if closing else f"[{style}]")
  199. markup = "".join(output)
  200. return markup
  201. @classmethod
  202. def from_markup(
  203. cls,
  204. text: str,
  205. *,
  206. style: Union[str, Style] = "",
  207. emoji: bool = True,
  208. emoji_variant: Optional[EmojiVariant] = None,
  209. justify: Optional["JustifyMethod"] = None,
  210. overflow: Optional["OverflowMethod"] = None,
  211. end: str = "\n",
  212. ) -> "Text":
  213. """Create Text instance from markup.
  214. Args:
  215. text (str): A string containing console markup.
  216. emoji (bool, optional): Also render emoji code. Defaults to True.
  217. justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
  218. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
  219. end (str, optional): Character to end text with. Defaults to "\\\\n".
  220. Returns:
  221. Text: A Text instance with markup rendered.
  222. """
  223. from .markup import render
  224. rendered_text = render(text, style, emoji=emoji, emoji_variant=emoji_variant)
  225. rendered_text.justify = justify
  226. rendered_text.overflow = overflow
  227. rendered_text.end = end
  228. return rendered_text
  229. @classmethod
  230. def from_ansi(
  231. cls,
  232. text: str,
  233. *,
  234. style: Union[str, Style] = "",
  235. justify: Optional["JustifyMethod"] = None,
  236. overflow: Optional["OverflowMethod"] = None,
  237. no_wrap: Optional[bool] = None,
  238. end: str = "\n",
  239. tab_size: Optional[int] = 8,
  240. ) -> "Text":
  241. """Create a Text object from a string containing ANSI escape codes.
  242. Args:
  243. text (str): A string containing escape codes.
  244. style (Union[str, Style], optional): Base style for text. Defaults to "".
  245. justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
  246. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
  247. no_wrap (bool, optional): Disable text wrapping, or None for default. Defaults to None.
  248. end (str, optional): Character to end text with. Defaults to "\\\\n".
  249. tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to 8.
  250. """
  251. from .ansi import AnsiDecoder
  252. joiner = Text(
  253. "\n",
  254. justify=justify,
  255. overflow=overflow,
  256. no_wrap=no_wrap,
  257. end=end,
  258. tab_size=tab_size,
  259. style=style,
  260. )
  261. decoder = AnsiDecoder()
  262. result = joiner.join(line for line in decoder.decode(text))
  263. return result
  264. @classmethod
  265. def styled(
  266. cls,
  267. text: str,
  268. style: StyleType = "",
  269. *,
  270. justify: Optional["JustifyMethod"] = None,
  271. overflow: Optional["OverflowMethod"] = None,
  272. ) -> "Text":
  273. """Construct a Text instance with a pre-applied styled. A style applied in this way won't be used
  274. to pad the text when it is justified.
  275. Args:
  276. text (str): A string containing console markup.
  277. style (Union[str, Style]): Style to apply to the text. Defaults to "".
  278. justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
  279. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
  280. Returns:
  281. Text: A text instance with a style applied to the entire string.
  282. """
  283. styled_text = cls(text, justify=justify, overflow=overflow)
  284. styled_text.stylize(style)
  285. return styled_text
  286. @classmethod
  287. def assemble(
  288. cls,
  289. *parts: Union[str, "Text", Tuple[str, StyleType]],
  290. style: Union[str, Style] = "",
  291. justify: Optional["JustifyMethod"] = None,
  292. overflow: Optional["OverflowMethod"] = None,
  293. no_wrap: Optional[bool] = None,
  294. end: str = "\n",
  295. tab_size: int = 8,
  296. meta: Optional[Dict[str, Any]] = None,
  297. ) -> "Text":
  298. """Construct a text instance by combining a sequence of strings with optional styles.
  299. The positional arguments should be either strings, or a tuple of string + style.
  300. Args:
  301. style (Union[str, Style], optional): Base style for text. Defaults to "".
  302. justify (str, optional): Justify method: "left", "center", "full", "right". Defaults to None.
  303. overflow (str, optional): Overflow method: "crop", "fold", "ellipsis". Defaults to None.
  304. end (str, optional): Character to end text with. Defaults to "\\\\n".
  305. tab_size (int): Number of spaces per tab, or ``None`` to use ``console.tab_size``. Defaults to 8.
  306. meta (Dict[str, Any], optional). Meta data to apply to text, or None for no meta data. Default to None
  307. Returns:
  308. Text: A new text instance.
  309. """
  310. text = cls(
  311. style=style,
  312. justify=justify,
  313. overflow=overflow,
  314. no_wrap=no_wrap,
  315. end=end,
  316. tab_size=tab_size,
  317. )
  318. append = text.append
  319. _Text = Text
  320. for part in parts:
  321. if isinstance(part, (_Text, str)):
  322. append(part)
  323. else:
  324. append(*part)
  325. if meta:
  326. text.apply_meta(meta)
  327. return text
  328. @property
  329. def plain(self) -> str:
  330. """Get the text as a single string."""
  331. if len(self._text) != 1:
  332. self._text[:] = ["".join(self._text)]
  333. return self._text[0]
  334. @plain.setter
  335. def plain(self, new_text: str) -> None:
  336. """Set the text to a new value."""
  337. if new_text != self.plain:
  338. sanitized_text = strip_control_codes(new_text)
  339. self._text[:] = [sanitized_text]
  340. old_length = self._length
  341. self._length = len(sanitized_text)
  342. if old_length > self._length:
  343. self._trim_spans()
  344. @property
  345. def spans(self) -> List[Span]:
  346. """Get a reference to the internal list of spans."""
  347. return self._spans
  348. @spans.setter
  349. def spans(self, spans: List[Span]) -> None:
  350. """Set spans."""
  351. self._spans = spans[:]
  352. def blank_copy(self, plain: str = "") -> "Text":
  353. """Return a new Text instance with copied meta data (but not the string or spans)."""
  354. copy_self = Text(
  355. plain,
  356. style=self.style,
  357. justify=self.justify,
  358. overflow=self.overflow,
  359. no_wrap=self.no_wrap,
  360. end=self.end,
  361. tab_size=self.tab_size,
  362. )
  363. return copy_self
  364. def copy(self) -> "Text":
  365. """Return a copy of this instance."""
  366. copy_self = Text(
  367. self.plain,
  368. style=self.style,
  369. justify=self.justify,
  370. overflow=self.overflow,
  371. no_wrap=self.no_wrap,
  372. end=self.end,
  373. tab_size=self.tab_size,
  374. )
  375. copy_self._spans[:] = self._spans
  376. return copy_self
  377. def stylize(
  378. self,
  379. style: Union[str, Style],
  380. start: int = 0,
  381. end: Optional[int] = None,
  382. ) -> None:
  383. """Apply a style to the text, or a portion of the text.
  384. Args:
  385. style (Union[str, Style]): Style instance or style definition to apply.
  386. start (int): Start offset (negative indexing is supported). Defaults to 0.
  387. end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None.
  388. """
  389. if style:
  390. length = len(self)
  391. if start < 0:
  392. start = length + start
  393. if end is None:
  394. end = length
  395. if end < 0:
  396. end = length + end
  397. if start >= length or end <= start:
  398. # Span not in text or not valid
  399. return
  400. self._spans.append(Span(start, min(length, end), style))
  401. def stylize_before(
  402. self,
  403. style: Union[str, Style],
  404. start: int = 0,
  405. end: Optional[int] = None,
  406. ) -> None:
  407. """Apply a style to the text, or a portion of the text. Styles will be applied before other styles already present.
  408. Args:
  409. style (Union[str, Style]): Style instance or style definition to apply.
  410. start (int): Start offset (negative indexing is supported). Defaults to 0.
  411. end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None.
  412. """
  413. if style:
  414. length = len(self)
  415. if start < 0:
  416. start = length + start
  417. if end is None:
  418. end = length
  419. if end < 0:
  420. end = length + end
  421. if start >= length or end <= start:
  422. # Span not in text or not valid
  423. return
  424. self._spans.insert(0, Span(start, min(length, end), style))
  425. def apply_meta(
  426. self, meta: Dict[str, Any], start: int = 0, end: Optional[int] = None
  427. ) -> None:
  428. """Apply meta data to the text, or a portion of the text.
  429. Args:
  430. meta (Dict[str, Any]): A dict of meta information.
  431. start (int): Start offset (negative indexing is supported). Defaults to 0.
  432. end (Optional[int], optional): End offset (negative indexing is supported), or None for end of text. Defaults to None.
  433. """
  434. style = Style.from_meta(meta)
  435. self.stylize(style, start=start, end=end)
  436. def on(self, meta: Optional[Dict[str, Any]] = None, **handlers: Any) -> "Text":
  437. """Apply event handlers (used by Textual project).
  438. Example:
  439. >>> from rich.text import Text
  440. >>> text = Text("hello world")
  441. >>> text.on(click="view.toggle('world')")
  442. Args:
  443. meta (Dict[str, Any]): Mapping of meta information.
  444. **handlers: Keyword args are prefixed with "@" to defined handlers.
  445. Returns:
  446. Text: Self is returned to method may be chained.
  447. """
  448. meta = {} if meta is None else meta
  449. meta.update({f"@{key}": value for key, value in handlers.items()})
  450. self.stylize(Style.from_meta(meta))
  451. return self
  452. def remove_suffix(self, suffix: str) -> None:
  453. """Remove a suffix if it exists.
  454. Args:
  455. suffix (str): Suffix to remove.
  456. """
  457. if self.plain.endswith(suffix):
  458. self.right_crop(len(suffix))
  459. def get_style_at_offset(self, console: "Console", offset: int) -> Style:
  460. """Get the style of a character at give offset.
  461. Args:
  462. console (~Console): Console where text will be rendered.
  463. offset (int): Offset in to text (negative indexing supported)
  464. Returns:
  465. Style: A Style instance.
  466. """
  467. # TODO: This is a little inefficient, it is only used by full justify
  468. if offset < 0:
  469. offset = len(self) + offset
  470. get_style = console.get_style
  471. style = get_style(self.style).copy()
  472. for start, end, span_style in self._spans:
  473. if end > offset >= start:
  474. style += get_style(span_style, default="")
  475. return style
  476. def highlight_regex(
  477. self,
  478. re_highlight: str,
  479. style: Optional[Union[GetStyleCallable, StyleType]] = None,
  480. *,
  481. style_prefix: str = "",
  482. ) -> int:
  483. """Highlight text with a regular expression, where group names are
  484. translated to styles.
  485. Args:
  486. re_highlight (str): A regular expression.
  487. style (Union[GetStyleCallable, StyleType]): Optional style to apply to whole match, or a callable
  488. which accepts the matched text and returns a style. Defaults to None.
  489. style_prefix (str, optional): Optional prefix to add to style group names.
  490. Returns:
  491. int: Number of regex matches
  492. """
  493. count = 0
  494. append_span = self._spans.append
  495. _Span = Span
  496. plain = self.plain
  497. for match in re.finditer(re_highlight, plain):
  498. get_span = match.span
  499. if style:
  500. start, end = get_span()
  501. match_style = style(plain[start:end]) if callable(style) else style
  502. if match_style is not None and end > start:
  503. append_span(_Span(start, end, match_style))
  504. count += 1
  505. for name in match.groupdict().keys():
  506. start, end = get_span(name)
  507. if start != -1 and end > start:
  508. append_span(_Span(start, end, f"{style_prefix}{name}"))
  509. return count
  510. def highlight_words(
  511. self,
  512. words: Iterable[str],
  513. style: Union[str, Style],
  514. *,
  515. case_sensitive: bool = True,
  516. ) -> int:
  517. """Highlight words with a style.
  518. Args:
  519. words (Iterable[str]): Worlds to highlight.
  520. style (Union[str, Style]): Style to apply.
  521. case_sensitive (bool, optional): Enable case sensitive matchings. Defaults to True.
  522. Returns:
  523. int: Number of words highlighted.
  524. """
  525. re_words = "|".join(re.escape(word) for word in words)
  526. add_span = self._spans.append
  527. count = 0
  528. _Span = Span
  529. for match in re.finditer(
  530. re_words, self.plain, flags=0 if case_sensitive else re.IGNORECASE
  531. ):
  532. start, end = match.span(0)
  533. add_span(_Span(start, end, style))
  534. count += 1
  535. return count
  536. def rstrip(self) -> None:
  537. """Strip whitespace from end of text."""
  538. self.plain = self.plain.rstrip()
  539. def rstrip_end(self, size: int) -> None:
  540. """Remove whitespace beyond a certain width at the end of the text.
  541. Args:
  542. size (int): The desired size of the text.
  543. """
  544. text_length = len(self)
  545. if text_length > size:
  546. excess = text_length - size
  547. whitespace_match = _re_whitespace.search(self.plain)
  548. if whitespace_match is not None:
  549. whitespace_count = len(whitespace_match.group(0))
  550. self.right_crop(min(whitespace_count, excess))
  551. def set_length(self, new_length: int) -> None:
  552. """Set new length of the text, clipping or padding is required."""
  553. length = len(self)
  554. if length != new_length:
  555. if length < new_length:
  556. self.pad_right(new_length - length)
  557. else:
  558. self.right_crop(length - new_length)
  559. def __rich_console__(
  560. self, console: "Console", options: "ConsoleOptions"
  561. ) -> Iterable[Segment]:
  562. tab_size: int = console.tab_size or self.tab_size or 8
  563. justify = self.justify or options.justify or DEFAULT_JUSTIFY
  564. overflow = self.overflow or options.overflow or DEFAULT_OVERFLOW
  565. lines = self.wrap(
  566. console,
  567. options.max_width,
  568. justify=justify,
  569. overflow=overflow,
  570. tab_size=tab_size or 8,
  571. no_wrap=pick_bool(self.no_wrap, options.no_wrap, False),
  572. )
  573. all_lines = Text("\n").join(lines)
  574. yield from all_lines.render(console, end=self.end)
  575. def __rich_measure__(
  576. self, console: "Console", options: "ConsoleOptions"
  577. ) -> Measurement:
  578. text = self.plain
  579. lines = text.splitlines()
  580. max_text_width = max(cell_len(line) for line in lines) if lines else 0
  581. words = text.split()
  582. min_text_width = (
  583. max(cell_len(word) for word in words) if words else max_text_width
  584. )
  585. return Measurement(min_text_width, max_text_width)
  586. def render(self, console: "Console", end: str = "") -> Iterable["Segment"]:
  587. """Render the text as Segments.
  588. Args:
  589. console (Console): Console instance.
  590. end (Optional[str], optional): Optional end character.
  591. Returns:
  592. Iterable[Segment]: Result of render that may be written to the console.
  593. """
  594. _Segment = Segment
  595. text = self.plain
  596. if not self._spans:
  597. yield Segment(text)
  598. if end:
  599. yield _Segment(end)
  600. return
  601. get_style = partial(console.get_style, default=Style.null())
  602. enumerated_spans = list(enumerate(self._spans, 1))
  603. style_map = {index: get_style(span.style) for index, span in enumerated_spans}
  604. style_map[0] = get_style(self.style)
  605. spans = [
  606. (0, False, 0),
  607. *((span.start, False, index) for index, span in enumerated_spans),
  608. *((span.end, True, index) for index, span in enumerated_spans),
  609. (len(text), True, 0),
  610. ]
  611. spans.sort(key=itemgetter(0, 1))
  612. stack: List[int] = []
  613. stack_append = stack.append
  614. stack_pop = stack.remove
  615. style_cache: Dict[Tuple[Style, ...], Style] = {}
  616. style_cache_get = style_cache.get
  617. combine = Style.combine
  618. def get_current_style() -> Style:
  619. """Construct current style from stack."""
  620. styles = tuple(style_map[_style_id] for _style_id in sorted(stack))
  621. cached_style = style_cache_get(styles)
  622. if cached_style is not None:
  623. return cached_style
  624. current_style = combine(styles)
  625. style_cache[styles] = current_style
  626. return current_style
  627. for (offset, leaving, style_id), (next_offset, _, _) in zip(spans, spans[1:]):
  628. if leaving:
  629. stack_pop(style_id)
  630. else:
  631. stack_append(style_id)
  632. if next_offset > offset:
  633. yield _Segment(text[offset:next_offset], get_current_style())
  634. if end:
  635. yield _Segment(end)
  636. def join(self, lines: Iterable["Text"]) -> "Text":
  637. """Join text together with this instance as the separator.
  638. Args:
  639. lines (Iterable[Text]): An iterable of Text instances to join.
  640. Returns:
  641. Text: A new text instance containing join text.
  642. """
  643. new_text = self.blank_copy()
  644. def iter_text() -> Iterable["Text"]:
  645. if self.plain:
  646. for last, line in loop_last(lines):
  647. yield line
  648. if not last:
  649. yield self
  650. else:
  651. yield from lines
  652. extend_text = new_text._text.extend
  653. append_span = new_text._spans.append
  654. extend_spans = new_text._spans.extend
  655. offset = 0
  656. _Span = Span
  657. for text in iter_text():
  658. extend_text(text._text)
  659. if text.style:
  660. append_span(_Span(offset, offset + len(text), text.style))
  661. extend_spans(
  662. _Span(offset + start, offset + end, style)
  663. for start, end, style in text._spans
  664. )
  665. offset += len(text)
  666. new_text._length = offset
  667. return new_text
  668. def expand_tabs(self, tab_size: Optional[int] = None) -> None:
  669. """Converts tabs to spaces.
  670. Args:
  671. tab_size (int, optional): Size of tabs. Defaults to 8.
  672. """
  673. if "\t" not in self.plain:
  674. return
  675. pos = 0
  676. if tab_size is None:
  677. tab_size = self.tab_size
  678. assert tab_size is not None
  679. result = self.blank_copy()
  680. append = result.append
  681. _style = self.style
  682. for line in self.split("\n", include_separator=True):
  683. parts = line.split("\t", include_separator=True)
  684. for part in parts:
  685. if part.plain.endswith("\t"):
  686. part._text = [part.plain[:-1] + " "]
  687. append(part)
  688. pos += len(part)
  689. spaces = tab_size - ((pos - 1) % tab_size) - 1
  690. if spaces:
  691. append(" " * spaces, _style)
  692. pos += spaces
  693. else:
  694. append(part)
  695. self._text = [result.plain]
  696. self._length = len(self.plain)
  697. self._spans[:] = result._spans
  698. def truncate(
  699. self,
  700. max_width: int,
  701. *,
  702. overflow: Optional["OverflowMethod"] = None,
  703. pad: bool = False,
  704. ) -> None:
  705. """Truncate text if it is longer that a given width.
  706. Args:
  707. max_width (int): Maximum number of characters in text.
  708. overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow.
  709. pad (bool, optional): Pad with spaces if the length is less than max_width. Defaults to False.
  710. """
  711. _overflow = overflow or self.overflow or DEFAULT_OVERFLOW
  712. if _overflow != "ignore":
  713. length = cell_len(self.plain)
  714. if length > max_width:
  715. if _overflow == "ellipsis":
  716. self.plain = set_cell_size(self.plain, max_width - 1) + "…"
  717. else:
  718. self.plain = set_cell_size(self.plain, max_width)
  719. if pad and length < max_width:
  720. spaces = max_width - length
  721. self._text = [f"{self.plain}{' ' * spaces}"]
  722. self._length = len(self.plain)
  723. def _trim_spans(self) -> None:
  724. """Remove or modify any spans that are over the end of the text."""
  725. max_offset = len(self.plain)
  726. _Span = Span
  727. self._spans[:] = [
  728. (
  729. span
  730. if span.end < max_offset
  731. else _Span(span.start, min(max_offset, span.end), span.style)
  732. )
  733. for span in self._spans
  734. if span.start < max_offset
  735. ]
  736. def pad(self, count: int, character: str = " ") -> None:
  737. """Pad left and right with a given number of characters.
  738. Args:
  739. count (int): Width of padding.
  740. """
  741. assert len(character) == 1, "Character must be a string of length 1"
  742. if count:
  743. pad_characters = character * count
  744. self.plain = f"{pad_characters}{self.plain}{pad_characters}"
  745. _Span = Span
  746. self._spans[:] = [
  747. _Span(start + count, end + count, style)
  748. for start, end, style in self._spans
  749. ]
  750. def pad_left(self, count: int, character: str = " ") -> None:
  751. """Pad the left with a given character.
  752. Args:
  753. count (int): Number of characters to pad.
  754. character (str, optional): Character to pad with. Defaults to " ".
  755. """
  756. assert len(character) == 1, "Character must be a string of length 1"
  757. if count:
  758. self.plain = f"{character * count}{self.plain}"
  759. _Span = Span
  760. self._spans[:] = [
  761. _Span(start + count, end + count, style)
  762. for start, end, style in self._spans
  763. ]
  764. def pad_right(self, count: int, character: str = " ") -> None:
  765. """Pad the right with a given character.
  766. Args:
  767. count (int): Number of characters to pad.
  768. character (str, optional): Character to pad with. Defaults to " ".
  769. """
  770. assert len(character) == 1, "Character must be a string of length 1"
  771. if count:
  772. self.plain = f"{self.plain}{character * count}"
  773. def align(self, align: AlignMethod, width: int, character: str = " ") -> None:
  774. """Align text to a given width.
  775. Args:
  776. align (AlignMethod): One of "left", "center", or "right".
  777. width (int): Desired width.
  778. character (str, optional): Character to pad with. Defaults to " ".
  779. """
  780. self.truncate(width)
  781. excess_space = width - cell_len(self.plain)
  782. if excess_space:
  783. if align == "left":
  784. self.pad_right(excess_space, character)
  785. elif align == "center":
  786. left = excess_space // 2
  787. self.pad_left(left, character)
  788. self.pad_right(excess_space - left, character)
  789. else:
  790. self.pad_left(excess_space, character)
  791. def append(
  792. self, text: Union["Text", str], style: Optional[Union[str, "Style"]] = None
  793. ) -> "Text":
  794. """Add text with an optional style.
  795. Args:
  796. text (Union[Text, str]): A str or Text to append.
  797. style (str, optional): A style name. Defaults to None.
  798. Returns:
  799. Text: Returns self for chaining.
  800. """
  801. if not isinstance(text, (str, Text)):
  802. raise TypeError("Only str or Text can be appended to Text")
  803. if len(text):
  804. if isinstance(text, str):
  805. sanitized_text = strip_control_codes(text)
  806. self._text.append(sanitized_text)
  807. offset = len(self)
  808. text_length = len(sanitized_text)
  809. if style is not None:
  810. self._spans.append(Span(offset, offset + text_length, style))
  811. self._length += text_length
  812. elif isinstance(text, Text):
  813. _Span = Span
  814. if style is not None:
  815. raise ValueError(
  816. "style must not be set when appending Text instance"
  817. )
  818. text_length = self._length
  819. if text.style is not None:
  820. self._spans.append(
  821. _Span(text_length, text_length + len(text), text.style)
  822. )
  823. self._text.append(text.plain)
  824. self._spans.extend(
  825. _Span(start + text_length, end + text_length, style)
  826. for start, end, style in text._spans
  827. )
  828. self._length += len(text)
  829. return self
  830. def append_text(self, text: "Text") -> "Text":
  831. """Append another Text instance. This method is more performant that Text.append, but
  832. only works for Text.
  833. Returns:
  834. Text: Returns self for chaining.
  835. """
  836. _Span = Span
  837. text_length = self._length
  838. if text.style is not None:
  839. self._spans.append(_Span(text_length, text_length + len(text), text.style))
  840. self._text.append(text.plain)
  841. self._spans.extend(
  842. _Span(start + text_length, end + text_length, style)
  843. for start, end, style in text._spans
  844. )
  845. self._length += len(text)
  846. return self
  847. def append_tokens(
  848. self, tokens: Iterable[Tuple[str, Optional[StyleType]]]
  849. ) -> "Text":
  850. """Append iterable of str and style. Style may be a Style instance or a str style definition.
  851. Args:
  852. pairs (Iterable[Tuple[str, Optional[StyleType]]]): An iterable of tuples containing str content and style.
  853. Returns:
  854. Text: Returns self for chaining.
  855. """
  856. append_text = self._text.append
  857. append_span = self._spans.append
  858. _Span = Span
  859. offset = len(self)
  860. for content, style in tokens:
  861. append_text(content)
  862. if style is not None:
  863. append_span(_Span(offset, offset + len(content), style))
  864. offset += len(content)
  865. self._length = offset
  866. return self
  867. def copy_styles(self, text: "Text") -> None:
  868. """Copy styles from another Text instance.
  869. Args:
  870. text (Text): A Text instance to copy styles from, must be the same length.
  871. """
  872. self._spans.extend(text._spans)
  873. def split(
  874. self,
  875. separator: str = "\n",
  876. *,
  877. include_separator: bool = False,
  878. allow_blank: bool = False,
  879. ) -> Lines:
  880. """Split rich text in to lines, preserving styles.
  881. Args:
  882. separator (str, optional): String to split on. Defaults to "\\\\n".
  883. include_separator (bool, optional): Include the separator in the lines. Defaults to False.
  884. allow_blank (bool, optional): Return a blank line if the text ends with a separator. Defaults to False.
  885. Returns:
  886. List[RichText]: A list of rich text, one per line of the original.
  887. """
  888. assert separator, "separator must not be empty"
  889. text = self.plain
  890. if separator not in text:
  891. return Lines([self.copy()])
  892. if include_separator:
  893. lines = self.divide(
  894. match.end() for match in re.finditer(re.escape(separator), text)
  895. )
  896. else:
  897. def flatten_spans() -> Iterable[int]:
  898. for match in re.finditer(re.escape(separator), text):
  899. start, end = match.span()
  900. yield start
  901. yield end
  902. lines = Lines(
  903. line for line in self.divide(flatten_spans()) if line.plain != separator
  904. )
  905. if not allow_blank and text.endswith(separator):
  906. lines.pop()
  907. return lines
  908. def divide(self, offsets: Iterable[int]) -> Lines:
  909. """Divide text in to a number of lines at given offsets.
  910. Args:
  911. offsets (Iterable[int]): Offsets used to divide text.
  912. Returns:
  913. Lines: New RichText instances between offsets.
  914. """
  915. _offsets = list(offsets)
  916. if not _offsets:
  917. return Lines([self.copy()])
  918. text = self.plain
  919. text_length = len(text)
  920. divide_offsets = [0, *_offsets, text_length]
  921. line_ranges = list(zip(divide_offsets, divide_offsets[1:]))
  922. style = self.style
  923. justify = self.justify
  924. overflow = self.overflow
  925. _Text = Text
  926. new_lines = Lines(
  927. _Text(
  928. text[start:end],
  929. style=style,
  930. justify=justify,
  931. overflow=overflow,
  932. )
  933. for start, end in line_ranges
  934. )
  935. if not self._spans:
  936. return new_lines
  937. _line_appends = [line._spans.append for line in new_lines._lines]
  938. line_count = len(line_ranges)
  939. _Span = Span
  940. for span_start, span_end, style in self._spans:
  941. lower_bound = 0
  942. upper_bound = line_count
  943. start_line_no = (lower_bound + upper_bound) // 2
  944. while True:
  945. line_start, line_end = line_ranges[start_line_no]
  946. if span_start < line_start:
  947. upper_bound = start_line_no - 1
  948. elif span_start > line_end:
  949. lower_bound = start_line_no + 1
  950. else:
  951. break
  952. start_line_no = (lower_bound + upper_bound) // 2
  953. if span_end < line_end:
  954. end_line_no = start_line_no
  955. else:
  956. end_line_no = lower_bound = start_line_no
  957. upper_bound = line_count
  958. while True:
  959. line_start, line_end = line_ranges[end_line_no]
  960. if span_end < line_start:
  961. upper_bound = end_line_no - 1
  962. elif span_end > line_end:
  963. lower_bound = end_line_no + 1
  964. else:
  965. break
  966. end_line_no = (lower_bound + upper_bound) // 2
  967. for line_no in range(start_line_no, end_line_no + 1):
  968. line_start, line_end = line_ranges[line_no]
  969. new_start = max(0, span_start - line_start)
  970. new_end = min(span_end - line_start, line_end - line_start)
  971. if new_end > new_start:
  972. _line_appends[line_no](_Span(new_start, new_end, style))
  973. return new_lines
  974. def right_crop(self, amount: int = 1) -> None:
  975. """Remove a number of characters from the end of the text."""
  976. max_offset = len(self.plain) - amount
  977. _Span = Span
  978. self._spans[:] = [
  979. (
  980. span
  981. if span.end < max_offset
  982. else _Span(span.start, min(max_offset, span.end), span.style)
  983. )
  984. for span in self._spans
  985. if span.start < max_offset
  986. ]
  987. self._text = [self.plain[:-amount]]
  988. self._length -= amount
  989. def wrap(
  990. self,
  991. console: "Console",
  992. width: int,
  993. *,
  994. justify: Optional["JustifyMethod"] = None,
  995. overflow: Optional["OverflowMethod"] = None,
  996. tab_size: int = 8,
  997. no_wrap: Optional[bool] = None,
  998. ) -> Lines:
  999. """Word wrap the text.
  1000. Args:
  1001. console (Console): Console instance.
  1002. width (int): Number of characters per line.
  1003. emoji (bool, optional): Also render emoji code. Defaults to True.
  1004. justify (str, optional): Justify method: "default", "left", "center", "full", "right". Defaults to "default".
  1005. overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None.
  1006. tab_size (int, optional): Default tab size. Defaults to 8.
  1007. no_wrap (bool, optional): Disable wrapping, Defaults to False.
  1008. Returns:
  1009. Lines: Number of lines.
  1010. """
  1011. wrap_justify = justify or self.justify or DEFAULT_JUSTIFY
  1012. wrap_overflow = overflow or self.overflow or DEFAULT_OVERFLOW
  1013. no_wrap = pick_bool(no_wrap, self.no_wrap, False) or overflow == "ignore"
  1014. lines = Lines()
  1015. for line in self.split(allow_blank=True):
  1016. if "\t" in line:
  1017. line.expand_tabs(tab_size)
  1018. if no_wrap:
  1019. new_lines = Lines([line])
  1020. else:
  1021. offsets = divide_line(str(line), width, fold=wrap_overflow == "fold")
  1022. new_lines = line.divide(offsets)
  1023. for line in new_lines:
  1024. line.rstrip_end(width)
  1025. if wrap_justify:
  1026. new_lines.justify(
  1027. console, width, justify=wrap_justify, overflow=wrap_overflow
  1028. )
  1029. for line in new_lines:
  1030. line.truncate(width, overflow=wrap_overflow)
  1031. lines.extend(new_lines)
  1032. return lines
  1033. def fit(self, width: int) -> Lines:
  1034. """Fit the text in to given width by chopping in to lines.
  1035. Args:
  1036. width (int): Maximum characters in a line.
  1037. Returns:
  1038. Lines: Lines container.
  1039. """
  1040. lines: Lines = Lines()
  1041. append = lines.append
  1042. for line in self.split():
  1043. line.set_length(width)
  1044. append(line)
  1045. return lines
  1046. def detect_indentation(self) -> int:
  1047. """Auto-detect indentation of code.
  1048. Returns:
  1049. int: Number of spaces used to indent code.
  1050. """
  1051. _indentations = {
  1052. len(match.group(1))
  1053. for match in re.finditer(r"^( *)(.*)$", self.plain, flags=re.MULTILINE)
  1054. }
  1055. try:
  1056. indentation = (
  1057. reduce(gcd, [indent for indent in _indentations if not indent % 2]) or 1
  1058. )
  1059. except TypeError:
  1060. indentation = 1
  1061. return indentation
  1062. def with_indent_guides(
  1063. self,
  1064. indent_size: Optional[int] = None,
  1065. *,
  1066. character: str = "│",
  1067. style: StyleType = "dim green",
  1068. ) -> "Text":
  1069. """Adds indent guide lines to text.
  1070. Args:
  1071. indent_size (Optional[int]): Size of indentation, or None to auto detect. Defaults to None.
  1072. character (str, optional): Character to use for indentation. Defaults to "│".
  1073. style (Union[Style, str], optional): Style of indent guides.
  1074. Returns:
  1075. Text: New text with indentation guides.
  1076. """
  1077. _indent_size = self.detect_indentation() if indent_size is None else indent_size
  1078. text = self.copy()
  1079. text.expand_tabs()
  1080. indent_line = f"{character}{' ' * (_indent_size - 1)}"
  1081. re_indent = re.compile(r"^( *)(.*)$")
  1082. new_lines: List[Text] = []
  1083. add_line = new_lines.append
  1084. blank_lines = 0
  1085. for line in text.split(allow_blank=True):
  1086. match = re_indent.match(line.plain)
  1087. if not match or not match.group(2):
  1088. blank_lines += 1
  1089. continue
  1090. indent = match.group(1)
  1091. full_indents, remaining_space = divmod(len(indent), _indent_size)
  1092. new_indent = f"{indent_line * full_indents}{' ' * remaining_space}"
  1093. line.plain = new_indent + line.plain[len(new_indent) :]
  1094. line.stylize(style, 0, len(new_indent))
  1095. if blank_lines:
  1096. new_lines.extend([Text(new_indent, style=style)] * blank_lines)
  1097. blank_lines = 0
  1098. add_line(line)
  1099. if blank_lines:
  1100. new_lines.extend([Text("", style=style)] * blank_lines)
  1101. new_text = text.blank_copy("\n").join(new_lines)
  1102. return new_text
  1103. if __name__ == "__main__": # pragma: no cover
  1104. from pip._vendor.rich.console import Console
  1105. text = Text(
  1106. """\nLorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"""
  1107. )
  1108. text.highlight_words(["Lorem"], "bold")
  1109. text.highlight_words(["ipsum"], "italic")
  1110. console = Console()
  1111. console.rule("justify='left'")
  1112. console.print(text, style="red")
  1113. console.print()
  1114. console.rule("justify='center'")
  1115. console.print(text, style="green", justify="center")
  1116. console.print()
  1117. console.rule("justify='right'")
  1118. console.print(text, style="blue", justify="right")
  1119. console.print()
  1120. console.rule("justify='full'")
  1121. console.print(text, style="magenta", justify="full")
  1122. console.print()