Skip to content

Commit ea15183

Browse files
committed
Optimize Unicode encoding fix for stdout
- Simplify UTF-8 encoding solution using TextIOWrapper directly - Remove complex fallback logic for more efficient approach - Maintains cross-platform Unicode support for Windows stdout
1 parent 624fe2e commit ea15183

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/treemapper/writer.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,12 @@ def write_tree_content(f: TextIO) -> None:
4343
if output_file is None:
4444
# Write to stdout with proper UTF-8 encoding
4545
# On Windows, sys.stdout might use cp1252 encoding which can't handle Unicode
46-
# We need to ensure UTF-8 encoding for proper Unicode support
47-
if hasattr(sys.stdout, "reconfigure"):
48-
# Python 3.7+ has reconfigure method
49-
sys.stdout.reconfigure(encoding="utf-8")
50-
write_tree_content(sys.stdout)
51-
else:
52-
# Fallback for older Python versions or systems without reconfigure
53-
import io
46+
import io
5447

55-
utf8_stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
56-
write_tree_content(utf8_stdout)
57-
utf8_stdout.flush()
48+
# Use TextIOWrapper to ensure UTF-8 encoding for stdout
49+
utf8_stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", newline="")
50+
write_tree_content(utf8_stdout)
51+
utf8_stdout.flush()
5852
logging.info("Directory tree written to stdout")
5953
else:
6054
# Write to file

0 commit comments

Comments
 (0)