question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A way to to use System.Console.WindowWidth for redirected TextWriters.

See original GitHub issue

Is your feature request related to a problem? Please describe. In the current implementation if one uses a custom TextWriter/Output Stream the Tty property returns true and this automatically leads to a situation where GetHeight() and GetWidth() return some default constants that can’t be controlled.

A user of the library can’t even prevent that because Profile and Capabilities are both sealed.

I currently use a custom TextWriter that later down the line ultimately writes to the real console out TextWriter, but the way Tty is implemented breaks the width and height detection an causes the output to wrap after 80 characters.

Describe the solution you’d like I’m pretty open to ideas. The end result should be a situation where I can use a custom TextWriter and can somehow hook GetWidth and GetHeight. Manually setting width and height is not a good option because for a real console (like in my case) there are no good options to get a resize event so I would have to constantly poll System.Console.WindowWidth and set the width for the profile.

Things I would try/consider:

  • A way to overwrite the Tty flag (introduce a setter with a nullable backing field, like width and height in Profile) Probably the simplest solution?
  • Unseal Capabilities so users can change the tty behaviour
  • Unseal Profile so it can be derived from
  • Introduce an IProfile interface so IAnsiConsole implementations can provide a different profile implementation

Describe alternatives you’ve considered Polling System.Console.WindowWidth and set the width for the profile (nothing I would seriously consider) or somehow fix it in a custom fork. Another possibility is to set Profile.Width = int.MaxValue; and let the terminal do the line breaks, but this of course breaks almost all widgets.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
patriksvenssoncommented, Apr 13, 2021

@Frassle Yes, that would work for Paragraph i.e. Write/WriteLine/Markup/MarkupLine, but not for other kinds of widgets. For example, a table/grid/progress, etc would always need to pad with line breaks.

0reactions
Frasslecommented, Apr 13, 2021

To help match against some current logging behavior. The current code just writes out to console and lets the console do the wrapping which with a decent console means it will reflow word wrap as the terminal is resized. If we switch to writing out with spectre then we get hard breaks at whatever the console width is when we run.

I think it would also help with writing to things that really don’t really have a width like files. If you writing to a file your probably happy for your tables to just width expand to however much they need, but right justification doesn’t really make sense. Passing null instead of some large number would let the layout engines do something sensible there.

It would end up pretty much the same as soft_wrap in rich.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Console.WindowWidth Property (System)
Attempting to set the value of the WindowWidth property when output is redirected throws either an ArgumentOutOfRangeException or an IOException exception. To ...
Read more >
c# - How can I determine whether Console.Out has been ...
WindowWidth by inserting newlines - and this works perfectly. However if the output of the program is redirected to a file or another...
Read more >
C# | How to change the WindowWidth of the Console
Approach: This can be done using the WindowWidth property in the Console class of the System package in C#. The WindowWidth refers to...
Read more >
Redirect Console Output in C# – adamprescott.net
Redirecting output from your console application is as simple as creating a TextWriter and calling that method. Here's a quick example (taken ...
Read more >
Console
The console is an operating system window where users interact with the operating system or with a text-based console application by entering text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found