Skip to content

Commit

Permalink
Fix help render when NO_COLOR is specified
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kzu committed Jul 17, 2024
1 parent b61fff0 commit ea6df9e
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/Spectre.Console.Cli/Internal/Composer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ internal sealed class Composer : IRenderable
/// </summary>
private readonly bool _renderMarkup = false;

/// <summary>
/// Whether to avoid all styling in the output.
/// </summary>
private readonly bool _noColor = Environment.GetEnvironmentVariables().Contains("NO_COLOR");

public Composer()
{
_content = new StringBuilder();
Expand All @@ -22,12 +27,24 @@ public Composer(bool renderMarkup)

public Composer Text(string text)
{
if (_noColor && !string.IsNullOrWhiteSpace(text))
{
_content.Append(text.RemoveMarkup());
return this;
}

_content.Append(text);
return this;
}

public Composer Style(Style style, string text)
{
if (_noColor)
{
_content.Append(text.EscapeMarkup());
return this;
}

_content.Append('[').Append(style.ToMarkup()).Append(']');
_content.Append(text.EscapeMarkup());
_content.Append("[/]");
Expand All @@ -37,6 +54,12 @@ public Composer Style(Style style, string text)

public Composer Style(string style, string text)
{
if (_noColor)
{
_content.Append(text.EscapeMarkup());
return this;
}

_content.Append('[').Append(style).Append(']');
_content.Append(text.EscapeMarkup());
_content.Append("[/]");
Expand All @@ -46,6 +69,12 @@ public Composer Style(string style, string text)

public Composer Style(string style, Action<Composer> action)
{
if (_noColor)
{
action(this);
return this;
}

_content.Append('[').Append(style).Append(']');
action(this);
_content.Append("[/]");
Expand Down Expand Up @@ -114,7 +143,7 @@ public Composer Join(string separator, IEnumerable<Composer> composers)

public Measurement Measure(RenderOptions options, int maxWidth)
{
if (_renderMarkup)
if (_renderMarkup || _noColor)
{
return ((IRenderable)new Paragraph(_content.ToString())).Measure(options, maxWidth);
}
Expand All @@ -126,7 +155,7 @@ public Measurement Measure(RenderOptions options, int maxWidth)

public IEnumerable<Segment> Render(RenderOptions options, int maxWidth)
{
if (_renderMarkup)
if (_renderMarkup || _noColor)
{
return ((IRenderable)new Paragraph(_content.ToString())).Render(options, maxWidth);
}
Expand Down

0 comments on commit ea6df9e

Please sign in to comment.