-
I keep feeling a need for expandable content (text folding or tooltips) in non-interactive CLIs, as a way to bury extra detail. This is usually a For example, iTerm2 has an Annotations feature that can be used for tooltips. From their documentation:
It works as expected: It's not exactly a tooltip: annotations are limited to a line and do not wrap with text, but it's good enough. What's the best way to use this in Rich? Syntax like this would be most excellent: rich.print("An [annotate=The annotation]annotated[/] word in text") I do not know if Rich allows adding to the console markup vocabulary. If it doesn't, maybe an object like this: from typing import Self
import rich
class Annotated(str):
def __new__(cls, string: str, annotation: str) -> Self:
return str.__new__(cls, string)
def __init__(self, string: str, annotation: str) -> None:
self.annotation = annotation
def __rich__(self) -> str:
esc_ann = self.annotation.replace('[', r'\[')
length = len(self.split('\n', 1)[0]) # Not very smart: misses markup and other break chars
return f'\x1b]1337;AddHiddenAnnotation={length}|{esc_ann}\a{self}'
rich.print("An", Annotated("annotated", "The annotation"), "word in text") But this is relatively verbose and breaks i18n as the string is no longer one object to pass to gettext. It also doesn't work because Rich strips the How do I get this working with Rich? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
A working solution! I'm calling it "hinted" instead of "annotated" because it's fewer chars in markup:
Markup:
"[@hint='This is the hint']Hinted text[/]"