Skip to content

Expose the Utils.GetOutputString as a public method for C# developers to work with OutputRendering #14387

@daxian-dbw

Description

@daxian-dbw

This issue is used to track the work to address the review comment #13758 (comment).

This API may need to be public, so that other host implementations can strip off ANSI sequences the same way PowerShell does. All existing powershell hosts will need it when moving to PS 7.2, otherwise, their users will find $PSStyle.OutputRendering doesn't take effect. But I'm fine to expose the API in a separate PR. Let's open an issue to track it.

There is a need, for sure! I have developed my own versions of this as there has been no API.
I even think we should consider an API that doesn't just expose the implementation details but to think about what devs would like to do with it.
Otherwise, everyone will have to invent that wheel over and over again.

Another opportunity if you use methods is that you can determine if ANSI-output isn't enabled and then just bypass the decorations.

I think maybe the following 2 APIs need to be made public

  • Utils.GetOutputString (overloads may be needed)
    • The isOutputRedirected parameter needs to be revisited, at least its name. For a different host implementation, Console.IsOutputRedirected == true doesn't necessarily means it wants plain text. Think about the host of PS jupyter kernel in .NET Interactive, the process's output/error streams are always redirected to send to the frontend -- jupyter notebook, which is capable of handling ANSI sequences. So, in this case, even thought the std out/err are redirected, the ANSI sequences shouldn't be stripped off.
  • Utils.GetFormatStyleString
    • This method currently directly check on Console.IsErrorRedirected and Console.IsOutputRedirected. I think we shouldn't directly check on those in PowerShell engine, but should instead have separate parameters for the caller to provide that information. The engine should not assume there is a console available, and it's up to the host implementation to pass in whether there is error/output redirection when calling this method.
  • Utils.ShouldOutputPlainText (not sure if this one needs to be public too, but should evaluate it) probably not needed to be public.

Metadata

Metadata

Assignees

Labels

Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions