Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to preserve color output when using ListenAsync and StandardOutputCommandEvent ? #257

Closed
4 of 5 tasks
StefH opened this issue Sep 12, 2024 · 1 comment
Closed
4 of 5 tasks
Labels

Comments

@StefH
Copy link

StefH commented Sep 12, 2024

Version

3.6.6

Platform

.NET 8 / Windows 11

Steps to reproduce

1️⃣ Create an .NET executable which uses Serilog to write colored output.

2️⃣ Run this executable using:

var cmd = Cli.Wrap(Path.Combine(path, exe));

await foreach (var cmdEvent in cmd.ListenAsync(cancellationToken))
{
     switch (cmdEvent)
     {
         case StartedCommandEvent started:
             Console.WriteLine($"Process started; ID: {started.ProcessId}");
             break;

         case StandardOutputCommandEvent stdOut:
             Console.WriteLine($"Out> {stdOut.Text}");
             break;

         case StandardErrorCommandEvent stdErr:
             Console.WriteLine($"Err> {stdErr.Text}");
             break;

         case ExitedCommandEvent exited:
             Console.WriteLine($"Process exited; Code: {exited.ExitCode}");
             break;
     }
}

Details

Expected colors like this:
image

Actual it's just text without any color.

Checklist

  • I have looked through existing issues to make sure that this bug has not been reported before
  • I have provided a descriptive title for this issue
  • I have made sure that this bug is reproducible on the latest version of the package
  • I have provided all the information needed to reproduce this bug as efficiently as possible
  • I have sponsored this project
@StefH StefH added the bug label Sep 12, 2024
@Tyrrrz
Copy link
Owner

Tyrrrz commented Sep 12, 2024

The answer is complicated and it depends on how the colors are set in the first place 😁

If the underlying program is running on Windows and is using the WinAPI to set console colors via SetConsoleTextAttribute (or the equivalent framework APIs, such as the Console.SetForegroundColor(...) in .NET, for example), then no, it's not possible to retrieve them. The colors are communicated to the console window itself, not as part of the program's output.

If the underlying program is using ANSI escape sequences to set console colors, then it is possible theoretically, as the codes are part of the standard output/error. However, most programs will most likely choose to not output those codes when they detect that the output streams are redirected (which normally implies that the output needs to be clean). In some cases it's possible to override this behavior, but it depends on how the program itself is implemented. Additionally, even if you do manage to extract those codes, you will still need to be able to parse them and re-render them, which is a separate problem in itself (although solvable). Also, #225 may help in running command-line programs with a pseudo terminal, making the former think they have a real console window to interact with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants