-
Notifications
You must be signed in to change notification settings - Fork 95
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
common: general improvements #713
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
import logging | ||
|
||
|
||
def is_locale_utf8(): | ||
"""Check if the system locale is UTF-8.""" | ||
return 'UTF-8' in os.getenv('LC_CTYPE', '') or 'UTF-8' in os.getenv('LANG', '') | ||
|
||
|
||
def supports_emoji(): | ||
"""Detect if the terminal supports emoji output.""" | ||
term = os.getenv("TERM") | ||
if not term or term in ("dumb", "linux"): | ||
return False | ||
|
||
return is_locale_utf8() | ||
|
||
|
||
# Allow users to override emoji support via an environment variable | ||
# If RAMALAMA_FORCE_EMOJI is not set, it defaults to checking supports_emoji() | ||
RAMALAMA_FORCE_EMOJI = os.getenv("RAMALAMA_FORCE_EMOJI") | ||
FORCE_EMOJI = RAMALAMA_FORCE_EMOJI.lower() == "true" if RAMALAMA_FORCE_EMOJI else None | ||
EMOJI = FORCE_EMOJI if FORCE_EMOJI is not None else supports_emoji() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines 21-23 are long-winded it took me a while to read this and try and understand if it was correct, lets simplify:
|
||
|
||
|
||
# Define emoji-aware logging messages | ||
def error(msg): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The spacing isn't consistent in the printed output in these three functions, some are double-space some are single-space. If you are pushing new commits you might as well catch it here, we can also do in a follow on PR. |
||
formatted_msg = f"❌ {msg}" if EMOJI else f"[ERROR] {msg}" | ||
logging.error(formatted_msg) | ||
|
||
|
||
def warning(msg): | ||
formatted_msg = f"⚠️ {msg}" if EMOJI else f"[WARNING] {msg}" | ||
logging.warning(formatted_msg) | ||
|
||
|
||
def info(msg): | ||
formatted_msg = f"ℹ️ {msg}" if EMOJI else f"[INFO] {msg}" | ||
logging.info(formatted_msg) |
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 tested this on macOS with alacritty it needs one more check to work, because this is the only env var (LC_ALL) of the 3 env vars populated on this platform:
So we need to add one more or:
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.
Could also do:
to be less long-winded, as long as it works I don't mind too much. And it just checks these three env vars for UTF-8, I don't care after that point.