diff --git a/CHANGELOG.md b/CHANGELOG.md index cac4412d..5bd6afa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/), 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 ### Fixed diff --git a/pyproject.toml b/pyproject.toml index 91bf86bb..8a39bfcc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "rich" homepage = "https://github.com/willmcgugan/rich" 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" authors = ["Will McGugan "] license = "MIT" diff --git a/rich/style.py b/rich/style.py index fcaab8cd..7d4604f8 100644 --- a/rich/style.py +++ b/rich/style.py @@ -1,11 +1,12 @@ +import sys from functools import lru_cache from random import randint -import sys +from time import time from typing import Any, Dict, Iterable, List, Optional, Type, Union from . import errors -from .color import blend_rgb, Color, ColorParseError, ColorSystem -from .terminal_theme import TerminalTheme, DEFAULT_TERMINAL_THEME +from .color import Color, ColorParseError, ColorSystem, blend_rgb +from .terminal_theme import DEFAULT_TERMINAL_THEME, TerminalTheme # Style instances and style definitions are often interchangable StyleType = Union[str, "Style"] @@ -63,6 +64,7 @@ class Style: "_attributes", "_set_attributes", "_link", + "_link_id", "_ansi", "_style_definition", ] @@ -147,6 +149,7 @@ class Style: ) ) self._link = link + self._link_id = f"{time()}-{randint(0, 999999)}" if link else "" bold = _Bit(0) dim = _Bit(1) @@ -162,6 +165,11 @@ class Style: encircle = _Bit(11) 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: """Re-generate style definition from attributes.""" if self._style_definition is None: @@ -475,6 +483,7 @@ class Style: style._attributes = self._attributes style._set_attributes = self._set_attributes style._link = self._link + style._link_id = self._link_id return style def render( @@ -497,7 +506,9 @@ class Style: attrs = self._make_ansi_codes(color_system) rendered = f"\x1b[{attrs}m{text}\x1b[0m" if attrs else text 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 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._link = style._link or self._link + new_style._link_id = style._link_id or self._link_id return new_style diff --git a/tests/render.py b/tests/render.py index 53fd169c..f2335ec7 100644 --- a/tests/render.py +++ b/tests/render.py @@ -4,11 +4,14 @@ import re 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: - """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)