Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions rich/style.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import sys
from functools import lru_cache
from itertools import count
from marshal import dumps, loads
from random import randint
from time import time
from typing import Any, Dict, Iterable, List, Optional, Type, Union, cast

from . import errors
Expand All @@ -13,6 +14,9 @@
StyleType = Union[str, "Style"]


_id_generator = count(int(time()) >> 4)


class _Bit:
"""A descriptor to get/set a style attribute bit."""

Expand Down Expand Up @@ -190,7 +194,7 @@ def _make_color(color: Union[Color, str]) -> Color:
self._link = link
self._meta = None if meta is None else dumps(meta)
self._link_id = (
f"{randint(0, 999999)}{hash(self._meta)}" if (link or meta) else ""
f"{next(_id_generator)}{hash(self._meta)}" if (link or meta) else ""
)
self._hash: Optional[int] = None
self._null = not (self._set_attributes or color or bgcolor or link or meta)
Expand Down Expand Up @@ -240,7 +244,7 @@ def from_meta(cls, meta: Optional[Dict[str, Any]]) -> "Style":
style._attributes = 0
style._link = None
style._meta = dumps(meta)
style._link_id = f"{randint(0, 999999)}{hash(style._meta)}"
style._link_id = f"{next(_id_generator)}{hash(style._meta)}"
style._hash = None
style._null = not (meta)
return style
Expand Down Expand Up @@ -487,7 +491,7 @@ def without_color(self) -> "Style":
style._attributes = self._attributes
style._set_attributes = self._set_attributes
style._link = self._link
style._link_id = f"{randint(0, 999999)}" if self._link else ""
style._link_id = f"{next(_id_generator)}" if self._link else ""
style._null = False
style._meta = None
style._hash = None
Expand Down Expand Up @@ -639,7 +643,7 @@ def copy(self) -> "Style":
style._attributes = self._attributes
style._set_attributes = self._set_attributes
style._link = self._link
style._link_id = f"{randint(0, 999999)}" if self._link else ""
style._link_id = f"{next(_id_generator)}" if self._link else ""
style._hash = self._hash
style._null = False
style._meta = self._meta
Expand Down Expand Up @@ -685,7 +689,7 @@ def update_link(self, link: Optional[str] = None) -> "Style":
style._attributes = self._attributes
style._set_attributes = self._set_attributes
style._link = link
style._link_id = f"{randint(0, 999999)}" if link else ""
style._link_id = f"{next(_id_generator)}" if link else ""
style._hash = None
style._null = False
style._meta = self._meta
Expand Down
Loading