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

HelpProvider renders some formatting markup even with NO_COLOR #1583

Open
kzu opened this issue Jul 17, 2024 · 8 comments
Open

HelpProvider renders some formatting markup even with NO_COLOR #1583

kzu opened this issue Jul 17, 2024 · 8 comments
Labels
area-CLI Command-Line Interface bug Something isn't working

Comments

@kzu
Copy link
Contributor

kzu commented Jul 17, 2024

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 kzu added bug Something isn't working needs triage labels Jul 17, 2024
kzu added a commit to kzu/spectre.console that referenced this issue Jul 17, 2024
Currently, running `--help` with `NO_COLOR` set, results in formatting still being applied via markup to the options and default values.

This complements the previous fix for spectreconsole#211 for that scenario and is related to spectreconsole#1408.

Fixes spectreconsole#1583
@kzu
Copy link
Contributor Author

kzu commented Jul 17, 2024

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
Copy link
Contributor

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

@patriksvensson
Copy link
Contributor

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
Copy link
Contributor Author

kzu commented Jul 17, 2024

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
Copy link
Contributor Author

kzu commented Jul 17, 2024

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 FrankRay78 added the area-CLI Command-Line Interface label Aug 8, 2024
@FrankRay78
Copy link
Contributor

FrankRay78 commented Aug 8, 2024

Hello @kzu,

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

configurator.Settings.HelpProviderStyles = null;

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

composer.Style(helpStyles?.Description?.Header ?? Style.Plain, $"{resources.Description}:").LineBreak();

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 added a commit to devlooped/dotnet-trx that referenced this issue Sep 2, 2024
Also improve IDE speed by not regenerating help.md in design-time builds.

See also spectreconsole/spectre.console#1583 (comment)
kzu added a commit to devlooped/dotnet-trx that referenced this issue Sep 2, 2024
Also improve IDE speed by not regenerating help.md in design-time builds.

See also spectreconsole/spectre.console#1583 (comment)
kzu added a commit to devlooped/dotnet-retest that referenced this issue Sep 2, 2024
Also improve IDE speed by not regenerating help.md in design-time builds.

See also spectreconsole/spectre.console#1583 (comment)
kzu added a commit to devlooped/dotnet-retest that referenced this issue Sep 2, 2024
Also improve IDE speed by not regenerating help.md in design-time builds.

See also spectreconsole/spectre.console#1583 (comment)
@kzu
Copy link
Contributor Author

kzu commented Sep 2, 2024

@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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CLI Command-Line Interface bug Something isn't working
Projects
Status: Todo 🕑
Development

Successfully merging a pull request may close this issue.

3 participants