spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.
https://spectreconsole.net
MIT License
9.17k stars 472 forks source link

HelpProvider renders some formatting markup even with NO_COLOR #1583

Open kzu opened 1 month ago

kzu commented 1 month ago

To Reproduce

dotnet tool update -g dotnet-trx
$env:NO_COLOR=1
trx --help

Raw output:

USAGE:
    trx [OPTIONS]

OPTIONS:
                          DEFAULT                                               
    -h, --help                       Prints help information                    
    -p, --path                       Optional base directory for *.trx files    
                                     discovery. Defaults to current directory   
    -o, --output                     Include test output                        
    -r, --recursive       �[1mTrue�[0m       Recursively search for *.trx files         
        --skipped         �[1mTrue�[0m       Include skipped tests                      
        --no-exit-code               Do not return a -1 exit code on test       
                                     failures                                   
        --version                    Show version information                   
        --gh-comment      �[1mTrue�[0m       Report as GitHub PR comment                
        --gh-summary      �[1mTrue�[0m       Report as GitHub step summary              

Expected behavior Renders text only, with no bold or any other styles applied.

Screenshots Default values have bold style applied:

image

The importance of the issue is that it prevents having an automated way to self-document a CLI tool via the package readme, automatically updated after compile with up-to-date usage info, as shown in https://www.cazzulino.com/auto-doc-cli.html

kzu commented 1 month ago

I got a draft PR just to explore what the changes might be. Feel free to suggest alternatives. All existing tests pass fine, but I could not find unit tests specifically exercising the HelpProvider itself, to add a failing test and make it pass :). But the new output after the PR looks as I expected, without any formatting whatesoever:

USAGE:
    trx [OPTIONS]

OPTIONS:
                          DEFAULT                                               
    -h, --help                       Prints help information                    
    -p, --path                       Optional base directory for *.trx files    
                                     discovery. Defaults to current directory   
    -o, --output                     Include test output                        
    -r, --recursive       True       Recursively search for *.trx files         
        --skipped         True       Include skipped tests                      
        --no-exit-code               Do not return a -1 exit code on test       
                                     failures                                   
        --version                    Show version information                   
        --gh-comment      True       Report as GitHub PR comment                
        --gh-summary      True       Report as GitHub step summary        
patriksvensson commented 1 month ago

NO_COLOR only prevents outputting colors, not ANSI control codes or CSI sequences related to style.

patriksvensson commented 1 month ago

To automate output and get rid of ANSI sequences, you should disable ANSI completely using the console profile and add a switch in your application. NO_COLOR is not the way to go here.

You can also use a console recorder (https://spectreconsole.net/api/spectre.console/recorder/) to record the output.

kzu commented 1 month ago

Do you think there's an opportunity to provide something like this at least for the restricted use case of rendering text-only versions of help for the purposes of self-updating static docs for a tool?

kzu commented 1 month ago

Got a workaround for now in my app:

var app = new CommandApp();
// ...
app.Configure(config =>
{
  if (Environment.GetEnvironmentVariables().Contains("NO_COLOR") &&
      config.Settings.HelpProviderStyles?.Options is { } options)
      options.DefaultValue = Style.Plain;
});
FrankRay78 commented 1 month ago

Hello @kzu,

FYI. there is a unit test demonstrating a completely unstyled help output, see:

https://github.com/spectreconsole/spectre.console/blob/42fd801876398aea555dc93fcfe4bc0525bd2df4/src/Tests/Spectre.Console.Cli.Tests/Unit/CommandAppTests.Help.cs#L413

That results in Style.Plain being used throughout the help provider, eg:

https://github.com/spectreconsole/spectre.console/blob/42fd801876398aea555dc93fcfe4bc0525bd2df4/src/Spectre.Console.Cli/Help/HelpProvider.cs#L173

I would probably recommend you follow this approach instead, which is very similar to yours, but would prevent any future issues with styling being added to other sections in the help writer output in future releases (nb. no plans to do so, just saying).

kzu commented 1 week ago

@FrankRay78 that didn't work. It seems it's being re-set by someone else. The default values are bold even though I set the help styles to null:

image

FrankRay78 commented 1 week ago

Thanks for testing @kzu, I'll bump this up my list of things to fix.