Skip to content

Commit

Permalink
Merge pull request #123 from willmcgugan/style-id
Browse files Browse the repository at this point in the history
new link ids
  • Loading branch information
willmcgugan authored Jun 24, 2020
2 parents ff6358e + 23d0d44 commit be9ebcb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions examples/link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from rich import print

print("If your terminal supports links, the following text should be clickable:")
print("[link=https://www.willmcgugan.com][i]Visit [red]my[/red][/i] [yellow]Blog[/]")

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion rich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def get_style(

try:
style = self._styles.get(name)
return style if style is not None else Style.parse(name)
return style.copy() if style is not None else Style.parse(name).copy()
except errors.StyleSyntaxError as error:
if default is not None:
return self.get_style(default)
Expand Down
21 changes: 17 additions & 4 deletions rich/style.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -63,6 +64,7 @@ class Style:
"_attributes",
"_set_attributes",
"_link",
"_link_id",
"_ansi",
"_style_definition",
]
Expand Down Expand Up @@ -147,6 +149,7 @@ def _make_color(color: Union[Color, str]) -> Color:
)
)
self._link = link
self._link_id = f"{time()}-{randint(0, 999999)}" if link else ""

bold = _Bit(0)
dim = _Bit(1)
Expand All @@ -162,6 +165,11 @@ def _make_color(color: Union[Color, str]) -> Color:
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:
Expand Down Expand Up @@ -475,6 +483,7 @@ def copy(self) -> "Style":
style._attributes = self._attributes
style._set_attributes = self._set_attributes
style._link = self._link
style._link_id = f"{time()}-{randint(0, 999999)}" if self._link else ""
return style

def render(
Expand All @@ -497,7 +506,10 @@ def render(
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:
Expand Down Expand Up @@ -529,6 +541,7 @@ def __add__(self, style: Optional["Style"]) -> "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


Expand Down
7 changes: 5 additions & 2 deletions tests/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
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)


Expand Down

0 comments on commit be9ebcb

Please sign in to comment.