Skip to content

Fix garbled output when terminal isn't already UTF-8 at startup (#204)#205

Closed
logical-intent wants to merge 1 commit into
Aaronontheweb:devfrom
logical-intent:fix/Issue204
Closed

Fix garbled output when terminal isn't already UTF-8 at startup (#204)#205
logical-intent wants to merge 1 commit into
Aaronontheweb:devfrom
logical-intent:fix/Issue204

Conversation

@logical-intent

Copy link
Copy Markdown

Setting Console.OutputEncoding replaces Console.Out with a new TextWriter. We need to save the new TextWriter if we're going to write through it later - otherwise, if the old one was not UTF-8, any characters written through it that are out of range for its encoding will be replaced with a replacement character (e.g. U+FFFD).

Fixes #204

Changes

Re-jig AnsiConsole ctors so that we don't capture Console.Out before we change the console encoding (and hence replace Console.Out)

Checklist

(Akka.NET template ignored - I did make an effort to follow relevant guidance in the CONTRIBUTORS file but will double-check on that since I moved the code a little since then)

Latest dev Benchmarks / This PR's Benchmarks

(More from the Akka.NET template here but happy to follow up if there's also benchmark guidelines here)

…nontheweb#204)

* Fix garbled output when terminal isn't already UTF-8 at startup (Aaronontheweb#204)

Setting Console.OutputEncoding replaces Console.Out with a new
TextWriter.  We need to save the new TextWriter if we're going to write
through it later - otherwise, if the old one was not UTF-8, any
characters written through it that are out of range for its encoding
will be replaced with a replacement character (e.g. U+FFFD).
@Aaronontheweb

Copy link
Copy Markdown
Owner

Latest dev Benchmarks / This PR's Benchmarks
(More from the Akka.NET template here but happy to follow up if there's also benchmark guidelines here)

lol yeah don't worry about that - that's the .github repo from Akka.NET leaking into my stuff since I Have a fork of it.

Aaronontheweb added a commit that referenced this pull request May 18, 2026
Setting Console.OutputEncoding replaces Console.Out with a new TextWriter.
AnsiTerminal captured Console.Out at construction, so on a console that did
not start in UTF-8 the cached writer stayed bound to the old encoding and
rendered Unicode box-drawing characters as U+FFFD.

- AnsiTerminal no longer caches Console.Out; the output writer is resolved
  on every flush via the Output property, so it always reflects the current
  Console.Out regardless of when the encoding was changed.
- Consolidated UTF-8 encoding setup into ConsoleEnvironment.EnsureUtf8Output,
  invoked by the platform console, replacing three separate call sites.
- Added regression tests covering writer resolution.

Diagnosis and original fix by @logical-intent in #204 / #205.

Closes #204
@Aaronontheweb

Copy link
Copy Markdown
Owner

Superseded via #208

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Corrupt borders when launching from the VS Debugger

2 participants