[LiveLogger] Fix error and warning message alignment (#8719)

Context
The error and warning symbols may be rendered with different width on some terminals, resulting in misaligned output.

Changes Made
To make sure that the message text is always aligned we

Print the symbol.
Move back to the start of the line.
Move forward to the desired column.
Print the message text.
Testing
Windows terminal:

image

Windows cmd:

image

Fedora terminal:

image

Notes
I've also tried saving & restoring cursor position (VT100 functions 7 and 8) but that didn't fully work on Windows. The red X was still off.
This commit is contained in:
Ladi Prosek 2023-05-05 05:24:28 +02:00 коммит произвёл GitHub
Родитель 90f4271f28
Коммит ab6959506d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 24 добавлений и 3 удалений

Просмотреть файл

@ -60,6 +60,14 @@ internal static class AnsiCodes
/// </remarks>
public const string MoveForward = "C";
/// <summary>
/// Moves backward (to the left) the specified number of characters.
/// </summary>
/// <remarks>
/// Print <see cref="CSI"/>N<see cref="MoveBackward"/> to move N characters backward.
/// </remarks>
public const string MoveBackward = "D";
/// <summary>
/// Clears everything from cursor to end of screen.
/// </summary>

Просмотреть файл

@ -442,7 +442,20 @@ internal sealed class LiveLogger : INodeLogger
MessageSeverity.Error => TerminalColor.Red,
_ => TerminalColor.Default,
};
Terminal.WriteColorLine(color, $"{Indentation}{Indentation}{buildMessage.Message}");
char symbol = buildMessage.Severity switch
{
MessageSeverity.Warning => '⚠',
MessageSeverity.Error => '❌',
_ => ' ',
};
// The error and warning symbols may be rendered with different width on some terminals. To make sure that the message text
// is always aligned, we print the symbol, move back to the start of the line, then move forward to the desired column, and
// finally print the message text.
int maxSymbolWidth = 2;
int messageStartColumn = Indentation.Length + Indentation.Length + maxSymbolWidth;
Terminal.WriteColorLine(color, $"{Indentation}{Indentation}{symbol}\uFE0E{AnsiCodes.CSI}{messageStartColumn + 1}{AnsiCodes.MoveBackward}" +
$"{AnsiCodes.CSI}{messageStartColumn}{AnsiCodes.MoveForward} {buildMessage.Message}");
}
}
@ -546,7 +559,7 @@ internal sealed class LiveLogger : INodeLogger
if (buildEventContext is not null && _projects.TryGetValue(new ProjectContext(buildEventContext), out Project? project))
{
string message = EventArgsFormatting.FormatEventMessage(e, false);
project.AddBuildMessage(MessageSeverity.Warning, $"⚠\uFE0E {message}");
project.AddBuildMessage(MessageSeverity.Warning, message);
}
}
@ -559,7 +572,7 @@ internal sealed class LiveLogger : INodeLogger
if (buildEventContext is not null && _projects.TryGetValue(new ProjectContext(buildEventContext), out Project? project))
{
string message = EventArgsFormatting.FormatEventMessage(e, false);
project.AddBuildMessage(MessageSeverity.Error, $"❌\uFE0E {message}");
project.AddBuildMessage(MessageSeverity.Error, message);
}
}