Skip to content

Commit

Permalink
Fix code page issue on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Nov 14, 2023
1 parent 2a52f2a commit 157487a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ Not all terminals will actually offer a pleasant experience. The main criteria a

- **Support for UTF-8 and good rendering of unicode block elements**
More specifically the following characters `▄ █ ▀`.
Changing the code page might be necessary on windows, using `chcp 65001`.
Also, the alignement might be off (e.g small spaces between pixels)
This is not always well supported.

Expand Down Expand Up @@ -136,8 +135,8 @@ About Windows:

| Windows | Status | Colors | Unicode rendering | Performance | Comments |
|------------------|------------|---------------|------------------------|-------------|--------------------------|
| Windows terminal | Unpleasant | 24-bit colors | Good (`chcp 65001`) | 30 FPS | Buggy display |
| Cmder | Unplayable | 24-bit colors | Good (`chcp 65001`) | 2 FPS | No window title |
| Windows terminal | Unpleasant | 24-bit colors | Good | 30 FPS | Buggy display |
| Cmder | Unplayable | 24-bit colors | Good | 2 FPS | No window title |
| Terminus | Unplayable | 24-bit colors | Misalignments | 10 FPS | |
| Command prompt | Broken | N/A | N/A | N/A | No ANSI code support |
| Git bash | Broken | N/A | N/A | N/A | Doesn't work with winpty |
Expand Down
17 changes: 16 additions & 1 deletion gambaterm/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import sys
import time
import contextlib
from itertools import count
Expand Down Expand Up @@ -31,6 +32,20 @@ def get_ref(width: int, height: int, console: Console) -> tuple[int, int]:
return refx, refy


def write_bytes(app_session: AppSession, video_data: bytes) -> None:
# Fix code page issue on windows:
# `sys.stdout.buffer.raw` is a `WindowsConsoleIO` that always support UTF-8
# regardless of the configured codepage
if (
sys.platform == "win32"
and app_session.output.fileno() == sys.stdout.fileno()
and hasattr(sys.stdout.buffer, "raw")
):
sys.stdout.buffer.raw.write(video_data)
else:
os.write(app_session.output.fileno(), video_data)


def run(
console: Console,
get_input: InputGetter,
Expand Down Expand Up @@ -140,7 +155,7 @@ def run(
# Video sync
if video_data:
# Write video frame, might block
os.write(app_session.output.fileno(), video_data)
write_bytes(app_session, video_data)
# Send CPR request
if use_cpr_sync:
app_session.output.ask_for_cpr()
Expand Down

0 comments on commit 157487a

Please sign in to comment.