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.

Width detection fails when running Windows apps in WSL

See original GitHub issue

Information

  • OS: Windows 10
  • Version: WSL 1
  • Terminal: WSL Shell

Describe the bug Spectre.Console gets the terminal width by getting System.Console.BufferWidth however, when running a Windows app in WSL, this throws an IOException, leaving Spectre.Console using the default of 80 columns wide whatever the actual size of the terminal.

To Reproduce Run a Spectre.Console app running using the Windows .net core runtime in WSL, log out IAnsiConsole.Width.

Expected behavior It’s not straightforward what the best solution to this is. For my purposes, as a workaround, I’ve just implemented IAnsiConsole, passing through all implementations to AnsiBackend aside from Width which I’ve hardcoded to a large value - clearly this won’t produce desirable behaviour in case of output overflow.

Clearly, it would be possible to do the classic “try moving the cursor by some large amount, check where it is, return it to initial position” trick using escape codes (https://en.wikipedia.org/wiki/ANSI_escape_code#Terminal_output_sequences), however if you’re redirecting the output of a Spectre.Console app to a file then this approach isn’t going to work well, as Spectre.Console would have to wait for the terminal dimensions to be returned on stdin, which wouldn’t happen in the case of a redirect.

It would be possible to time out waiting for terminal dimensions, however that doesn’t feel ideal either, as programs would then take longer to run when redirected to file. One “solution” would be to allow the user to hard code the width more easily (e.g. in capabilities), without having to resort to implementing IAnsiConsole boilerplate.

Crucially, the best approach depends on the Spectre.Console philosophy: if the library is meant for applications that only run in the terminal, then terminal escape codes with timeout is probably the way to go, otherwise perhaps overriding Width is better?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
patriksvenssoncommented, Jan 9, 2021

@mcon Yes, it’s not optimal. I’m currently reworking the capabilities API which will make it possible to override things without creating a new console (as well as implementing specific profiles for known environments). I would suggest that we add some specific (opt-in) logic for more advanced detection once that is in place.

0reactions
jaredthirskcommented, Sep 10, 2022

My scenario: Linux csx script writing stdout to a log file in a cron job.

My workaround to get a custom width: https://gist.github.com/jaredthirsk/4bffdb3fdc3a71dae4515145420c43f2

Read more comments on GitHub >

github_iconTop Results From Across the Web

Width detection fails when running Windows apps in WSL
Information OS: Windows 10 Version: WSL 1 Terminal: WSL Shell Describe the bug Spectre.Console gets the terminal width by getting System.
Read more >
Run windows apps (like VSCode) in wsl - ubuntu
I've installed several apps in Windows only to later find out that they can't be called from WSL terminal. I don't want to...
Read more >
Troubleshooting Windows Subsystem for Linux
Provides detailed information about common errors and issues people run into while running Linux on the Windows Subsystem for Linux.
Read more >
Running GUI apps under WSL - gedit
While there are multiple options for running GUI apps under WSL, the only one that is "integrated with the system" is WSLg. While...
Read more >
WSL installation error
Hi all, I'm running Windows 11 and I tried to install WSL. Everything seemed to be going well until I got this message"...
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