new link ids

This commit is contained in:
Will McGugan 2020-06-24 20:53:50 +01:00
parent ff6358ed95
commit bc04eeae19
4 changed files with 28 additions and 7 deletions

View File

@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [2.2.6] - 2020-06-24
### Changed
- Store a "link id" on Style instance, so links containing different styles are highlighted together.
## [2.2.5] - 2020-06-23 ## [2.2.5] - 2020-06-23
### Fixed ### Fixed

View File

@ -2,7 +2,7 @@
name = "rich" name = "rich"
homepage = "https://github.com/willmcgugan/rich" homepage = "https://github.com/willmcgugan/rich"
documentation = "https://rich.readthedocs.io/en/latest/" documentation = "https://rich.readthedocs.io/en/latest/"
version = "2.2.5" version = "2.2.6"
description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal" description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
authors = ["Will McGugan <willmcgugan@gmail.com>"] authors = ["Will McGugan <willmcgugan@gmail.com>"]
license = "MIT" license = "MIT"

View File

@ -1,11 +1,12 @@
import sys
from functools import lru_cache from functools import lru_cache
from random import randint from random import randint
import sys from time import time
from typing import Any, Dict, Iterable, List, Optional, Type, Union from typing import Any, Dict, Iterable, List, Optional, Type, Union
from . import errors from . import errors
from .color import blend_rgb, Color, ColorParseError, ColorSystem from .color import Color, ColorParseError, ColorSystem, blend_rgb
from .terminal_theme import TerminalTheme, DEFAULT_TERMINAL_THEME from .terminal_theme import DEFAULT_TERMINAL_THEME, TerminalTheme
# Style instances and style definitions are often interchangable # Style instances and style definitions are often interchangable
StyleType = Union[str, "Style"] StyleType = Union[str, "Style"]
@ -63,6 +64,7 @@ class Style:
"_attributes", "_attributes",
"_set_attributes", "_set_attributes",
"_link", "_link",
"_link_id",
"_ansi", "_ansi",
"_style_definition", "_style_definition",
] ]
@ -147,6 +149,7 @@ class Style:
) )
) )
self._link = link self._link = link
self._link_id = f"{time()}-{randint(0, 999999)}" if link else ""
bold = _Bit(0) bold = _Bit(0)
dim = _Bit(1) dim = _Bit(1)
@ -162,6 +165,11 @@ class Style:
encircle = _Bit(11) encircle = _Bit(11)
overline = _Bit(12) overline = _Bit(12)
@property
def link_id(self) -> str:
"""Get a link id, used in ansi code for links."""
return self._link_id
def __str__(self) -> str: def __str__(self) -> str:
"""Re-generate style definition from attributes.""" """Re-generate style definition from attributes."""
if self._style_definition is None: if self._style_definition is None:
@ -475,6 +483,7 @@ class Style:
style._attributes = self._attributes style._attributes = self._attributes
style._set_attributes = self._set_attributes style._set_attributes = self._set_attributes
style._link = self._link style._link = self._link
style._link_id = self._link_id
return style return style
def render( def render(
@ -497,7 +506,9 @@ class Style:
attrs = self._make_ansi_codes(color_system) attrs = self._make_ansi_codes(color_system)
rendered = f"\x1b[{attrs}m{text}\x1b[0m" if attrs else text rendered = f"\x1b[{attrs}m{text}\x1b[0m" if attrs else text
if self._link: if self._link:
rendered = f"\x1b]8;id={randint(0, 10 ** 10)};{self._link}\x1b\\{rendered}\x1b]8;;\x1b\\" rendered = (
f"\x1b]8;id={self._link_id};{self._link}\x1b\\{rendered}\x1b]8;;\x1b\\"
)
return rendered return rendered
def test(self, text: Optional[str] = None) -> None: def test(self, text: Optional[str] = None) -> None:
@ -529,6 +540,7 @@ class Style:
) )
new_style._set_attributes = self._set_attributes | style._set_attributes new_style._set_attributes = self._set_attributes | style._set_attributes
new_style._link = style._link or self._link new_style._link = style._link or self._link
new_style._link_id = style._link_id or self._link_id
return new_style return new_style

View File

@ -4,11 +4,14 @@ import re
from rich.console import Console, RenderableType from rich.console import Console, RenderableType
re_link_ids = re.compile(r"id=\d*?;.*?\x1b") re_link_ids = re.compile(r"id=[\d\.\-]*?;.*?\x1b")
def replace_link_ids(render: str) -> str: def replace_link_ids(render: str) -> str:
"""Link IDs have a random ID and system path which is a problem for reproducable tests.""" """Link IDs have a random ID and system path which is a problem for
reproducible tests.
"""
return re_link_ids.sub("id=0;foo\x1b", render) return re_link_ids.sub("id=0;foo\x1b", render)