fss-mini-rag-github/mini_rag/windows_console_fix.py
FSSCoding 930f53a0fb Major code quality improvements and structural organization
- Applied Black formatter and isort across entire codebase for professional consistency
- Moved implementation scripts (rag-mini.py, rag-tui.py) to bin/ directory for cleaner root
- Updated shell scripts to reference new bin/ locations maintaining user compatibility
- Added comprehensive linting configuration (.flake8, pyproject.toml) with dedicated .venv-linting
- Removed development artifacts (commit_message.txt, GET_STARTED.md duplicate) from root
- Consolidated documentation and fixed script references across all guides
- Relocated test_fixes.py to proper tests/ directory
- Enhanced project structure following Python packaging standards

All user commands work identically while improving code organization and beginner accessibility.
2025-08-28 15:29:54 +10:00

71 lines
1.8 KiB
Python

"""
Windows Console Unicode/Emoji Fix
Reliable Windows console Unicode/emoji support for 2025.
"""
import io
import os
import sys
def fix_windows_console():
"""
Fix Windows console to properly handle UTF-8 and emojis.
Call this at the start of any script that needs to output Unicode/emojis.
"""
# Set environment variable for UTF-8 mode
os.environ["PYTHONUTF8"] = "1"
# For Python 3.7+
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
sys.stderr.reconfigure(encoding="utf-8")
if hasattr(sys.stdin, "reconfigure"):
sys.stdin.reconfigure(encoding="utf-8")
else:
# For older Python versions
if sys.platform == "win32":
# Replace streams with UTF-8 versions
sys.stdout = io.TextIOWrapper(
sys.stdout.buffer, encoding="utf-8", line_buffering=True
)
sys.stderr = io.TextIOWrapper(
sys.stderr.buffer, encoding="utf-8", line_buffering=True
)
# Also set the console code page to UTF-8 on Windows
if sys.platform == "win32":
import subprocess
try:
# Set console to UTF-8 code page
subprocess.run(["chcp", "65001"], shell=True, capture_output=True)
except (OSError, subprocess.SubprocessError):
pass
# Auto-fix on import
fix_windows_console()
# Test function to verify it works
def test_emojis():
"""Test that emojis work properly."""
print("Testing emoji output:")
print(" Check mark")
print(" Cross mark")
print(" Rocket")
print(" Fire")
print(" Computer")
print(" Python")
print(" Folder")
print(" Search")
print(" Lightning")
print(" Sparkles")
if __name__ == "__main__":
test_emojis()