You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Underline colors are supported in at least Kitty, Ghostty and Mintty and are a requirement for a complete implementation of the terminal graphics protocol. Since you can't simply add the ANSI codes part of your Segment text, it's much more work to implement this part of the protocol in Rich (and might not be possible using public APIs?). This affects libraries such as textual-image.
For the protocol to be implemented cleanly, it would also need to be possible to set a highlight color without actually enabling an underline/specifying an underline style.
In case this helps someone, I eventually managed to get an implementation that works in both Rich and Textual (and could be extended for other unimplemented/unsupported ANSI codes):
defmonkeypatch_style_add(self: Style, style: Style|None) ->Style:
# https://github.com/Textualize/textual/blob/eed9aa0ccc3ae263e56b38f9763e556572c0496d/src/textual/widget.py#L191-L195# `Style.__add__` resets `_ansi` which breaks our custom ANSI codesifstyleandstyle._ansiandstyle._ansi.startswith("58;2;"):
returnstyle# https://github.com/Textualize/rich/blob/4d6d631a3d2deddf8405522d4b8c976a6d35726c/rich/style.py#L753-L755combined_style=self._add(style)
returncombined_style.copy() ifcombined_style.linkelsecombined_stylerich.style.Style.__add__=monkeypatch_style_adddefmonkeypatch_style_hash(self: Style) ->int:
# Styles cached based on hash but excludes our custom ANSI codes# This causes all our styles to become the first style we return# https://github.com/Textualize/rich/blob/4d6d631a3d2deddf8405522d4b8c976a6d35726c/rich/style.py#L437-L441ifself._hashisnotNone:
returnself._hashifself._ansiandself._ansi.startswith("58;2;"):
self._hash=hash(self._ansi)
else:
self._hash=hash(rich.style._hash_getter(self))
returnself._hashrich.style.Style.__hash__=monkeypatch_style_hashdefstyle_with_foreground_and_underline_color(
fg_color: tuple[int, int, int], underline_color: tuple[int, int, int]
) ->Style:
style=Style()
fg_color_ansi= [
"38",
"2",
str(fg_color[0]),
str(fg_color[1]),
str(fg_color[2]),
]
underline_color_ansi= [
"58",
"2",
str(underline_color[0]),
str(underline_color[1]),
str(underline_color[2]),
]
style._ansi=";".join(underline_color_ansi+fg_color_ansi)
# https://github.com/Textualize/rich/blob/4d6d631a3d2deddf8405522d4b8c976a6d35726c/rich/style.py#L336-L338style._null=Falsereturnstyle
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Underline colors are supported in at least Kitty, Ghostty and Mintty and are a requirement for a complete implementation of the terminal graphics protocol. Since you can't simply add the ANSI codes part of your
Segmenttext, it's much more work to implement this part of the protocol in Rich (and might not be possible using public APIs?). This affects libraries such as textual-image.For the protocol to be implemented cleanly, it would also need to be possible to set a highlight color without actually enabling an underline/specifying an underline style.
In case this helps someone, I eventually managed to get an implementation that works in both Rich and Textual (and could be extended for other unimplemented/unsupported ANSI codes):
Beta Was this translation helpful? Give feedback.
All reactions