Skip to content

Commit

Permalink
write all informational messages to stderr (#499)
Browse files Browse the repository at this point in the history
2d7f2fb
/ #497 broke `bmx print` by
writing all these message to stdout.

Only the environment variable setting commands outputted by `bmx print`
can go to stdout. All other messages should to stderr.

https://desire2learn.atlassian.net/browse/VUL-628
  • Loading branch information
cfbao authored Jan 7, 2025
1 parent 159e178 commit ae2b49a
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/D2L.Bmx/ConsoleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ internal class ConsoleWriter : IConsoleWriter {
// https://github.com/dotnet/runtime/blob/v9.0.0-preview.6.24327.7/src/libraries/Common/src/System/Console/ConsoleUtils.cs#L32-L34
private readonly bool _noColor
= Environment.GetEnvironmentVariable( "NO_COLOR" ) == "1" || !VirtualTerminal.TryEnableOnStderr();
private readonly IAnsiConsole _ansiConsole = AnsiConsole.Create( new() {
Out = new AnsiConsoleOutput( Console.Error ),
} );

void IConsoleWriter.WriteParameter( string description, string value, ParameterSource source ) {
string valueColor = _noColor ? "default" : "cyan2";
string sourceColor = _noColor ? "default" : "grey";
AnsiConsole.MarkupLine( $"[default]{description}:[/] [{valueColor}]{value}[/] [{sourceColor}](from {source})[/]" );
_ansiConsole.MarkupLine( $"[default]{description}:[/] [{valueColor}]{value}[/] [{sourceColor}](from {source})[/]" );
}

void IConsoleWriter.WriteUpdateMessage( string text ) {
Expand All @@ -33,18 +36,18 @@ void IConsoleWriter.WriteUpdateMessage( string text ) {
string color = _noColor ? "default" : "black on white";
foreach( string line in lines ) {
string paddedLine = line.PadRight( maxLineLength );
AnsiConsole.MarkupLine( $"[{color}]{paddedLine}[/]" );
_ansiConsole.MarkupLine( $"[{color}]{paddedLine}[/]" );
}
Console.Error.WriteLine();
}

void IConsoleWriter.WriteWarning( string text ) {
string color = _noColor ? "default" : "yellow";
AnsiConsole.MarkupLine( $"[{color}]{text}[/]" );
_ansiConsole.MarkupLine( $"[{color}]{text}[/]" );
}

void IConsoleWriter.WriteError( string text ) {
string color = _noColor ? "default" : "red";
AnsiConsole.MarkupLine( $"[{color}]{text}[/]" );
_ansiConsole.MarkupLine( $"[{color}]{text}[/]" );
}
}

0 comments on commit ae2b49a

Please sign in to comment.