Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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