-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Fix RichProgressBar visibility #17532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
for more information, see https://pre-commit.ci
console = Console() | ||
if console.color_system == "truecolor": | ||
# Check if the terminal background color environment variable is set | ||
bg_color = os.environ.get("TERMINAL_BG_COLOR", "dark").lower() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to know if this environment variable is set by the Terminal. My color system is 256 and I don't have this environment variable.
My idea is whether it's possible to have one solution that can adapt to two backgrounds, for example, not setting the colors of default_description
and default_batch_progress
, will they be consistent with the default font color in different backgrounds, i.e., black in light theme and white in dark theme?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it may not be perfect in all cases, right? Especially if the user has a custom terminal theme with non-standard colors.
Also, what if we create a dictionary to store the default colors for each supported color system, so it makes it easy to add or modify the color settings accordingly?
Something like this,
from dataclasses import dataclass
from typing import Union
from rich.console import Console
from rich.style import Style
@dataclass
class RichProgressBarTheme:
@staticmethod
def detect_color_system() -> str:
console = Console()
return console.color_system
color_system = detect_color_system.__func__()
# Default colors for each color system
default_colors = {
"truecolor": {
"progress_bar": "bright_blue",
"time": "bright_cyan",
"processing_speed": "bright_yellow",
},
"256": {
"progress_bar": "color51",
"time": "color45",
"processing_speed": "color227",
},
"default": {
"progress_bar": "blue",
"time": "cyan",
"processing_speed": "yellow",
},
}
# Apply specific colors based on the detected color system
colors = default_colors.get(color_system, default_colors["default"])
description: Union[str, Style] = "default"
progress_bar: Union[str, Style] = colors["progress_bar"]
progress_bar_finished: Union[str, Style] = colors["progress_bar"]
progress_bar_pulse: Union[str, Style] = colors["progress_bar"]
batch_progress: Union[str, Style] = "default"
time: Union[str, Style] = colors["time"]
processing_speed: Union[str, Style] = colors["processing_speed"]
metrics: Union[str, Style] = "default"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, what I mean is to use the default
style for description
and batch_progress
, which can avoid the problem of being invisible in light themes as mentioned in issue #17532.
But it may not be perfect in all cases, right? Especially if the user has a custom terminal theme with non-standard colors.
As I mentioned in #17532, from my perspective, the first 16 standard colors of most themes are carefully selected and suitable for the theme. So my idea is to only use them.
Try to use the first 16 standard colors combined with ansi styles in
RichProgressBarTheme
, which are usually defined by the terminal theme.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the above code works?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so :), although I haven't tested it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
I will update the code and run some test cases, then make a PR again.
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. If you need further help see our docs: https://lightning.ai/docs/pytorch/latest/generated/CONTRIBUTING.html#pull-request or ask the assistance of a core contributor here or on Discord. Thank you for your contributions. |
This pull request is going to be closed. Please feel free to reopen it or create a new one based on top of the 'master' branch. |
What does this PR do?
Implemented RichProgressBarTheme with a new feature to detect the terminal background color and adjust the styles based on it. The terminal background color is detected using the Console class from the rich library. If the terminal supports truecolor, the TERMINAL_BG_COLOR environment variable is checked, otherwise, it is assumed to be a dark theme.
This implementation allows the RichProgressBar to be visible by default in both light and dark themes without requiring users to modify the theme.
Changes Made
Note: This is my first pull request in a big code base, if I did something wrong please do let me know thats why I made a draft pull request instead of the actual one. Thanks for your time!.
Fixes #17118
Before submitting
PR review
Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:
Reviewer checklist