Add color and improved formatting to the output of LottieGen. (#272)

This commit is contained in:
Simeon 2020-05-29 17:09:55 -07:00 коммит произвёл GitHub
Родитель 616df9abc3
Коммит efc73205a5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 131 добавлений и 25 удалений

32
LottieGen/InfoType.cs Normal file
Просмотреть файл

@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/// <summary>
/// Categories of information that are displayed to the user on the console. These
/// categories are used to color the output.
/// </summary>
enum InfoType
{
Default = 0,
/// <summary>
/// Advice to the user about a follow up action they should take.
/// </summary>
Advice,
/// <summary>
/// A file path or file name.
/// </summary>
FilePath,
/// <summary>
/// An issue that may prevent the result from working as expected.
/// </summary>
Issue,
/// <summary>
/// An announcement about the tool name and version.
/// </summary>
Signon,
}

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

@ -108,7 +108,7 @@ sealed class LottieFileProcessor
foreach (var issue in _readerIssues)
{
_reporter.WriteInfo(IssueToString(_file, issue));
_reporter.WriteInfo(InfoType.Issue, IssueToString(_file, issue));
}
if (_lottieComposition is null)
@ -362,7 +362,8 @@ sealed class LottieFileProcessor
outputFilePath,
LottieCompositionXmlSerializer.ToXml(_lottieComposition).ToString());
_reporter.WriteInfo($"Lottie XML written to {outputFilePath}");
_reporter.WriteInfo("Lottie XML written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputFilePath}");
return result;
}
@ -380,7 +381,8 @@ sealed class LottieFileProcessor
if (result)
{
_reporter.WriteInfo($"Lottie YAML written to {outputFilePath}");
_reporter.WriteInfo("Lottie YAML written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputFilePath}");
}
return result;
@ -401,7 +403,8 @@ sealed class LottieFileProcessor
if (result)
{
_reporter.WriteInfo($"WinComp XML written to {outputFilePath}");
_reporter.WriteInfo("WinComp XML written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputFilePath}");
}
return result;
@ -421,7 +424,8 @@ sealed class LottieFileProcessor
if (result)
{
_reporter.WriteInfo($"WinComp DGML written to {outputFilePath}");
_reporter.WriteInfo("WinComp DGML written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputFilePath}");
}
return result;
@ -447,7 +451,8 @@ sealed class LottieFileProcessor
if (result)
{
_reporter.WriteInfo($"C# code for class {_className} written to {outputFilePath}");
_reporter.WriteInfo($"C# code for class {_className} written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputFilePath}");
if (assetList != null)
{
@ -495,8 +500,11 @@ sealed class LottieFileProcessor
return false;
}
_reporter.WriteInfo($"Cppwinrt header for class {_className} written to {outputHeaderFilePath}");
_reporter.WriteInfo($"Cppwinrt source for class {_className} written to {outputCppFilePath}");
_reporter.WriteInfo($"Cppwinrt header for class {_className} written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputHeaderFilePath}");
_reporter.WriteInfo($"Cppwinrt source for class {_className} written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputCppFilePath}");
if (assetList != null)
{
@ -543,8 +551,11 @@ sealed class LottieFileProcessor
return false;
}
_reporter.WriteInfo($"CX header for class {_className} written to {outputHeaderFilePath}");
_reporter.WriteInfo($"CX source for class {_className} written to {outputCppFilePath}");
_reporter.WriteInfo($"CX header for class {_className} written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputHeaderFilePath}");
_reporter.WriteInfo($"CX source for class {_className} written to:");
_reporter.WriteInfo(InfoType.FilePath, $" {outputCppFilePath}");
if (assetList != null)
{
@ -609,7 +620,8 @@ sealed class LottieFileProcessor
Stream TryReadTextFile(string filePath)
{
_reporter.WriteInfo($"Reading file: {_file}");
_reporter.WriteInfo($"Reading file:");
_reporter.WriteInfo(InfoType.FilePath, $" {_file}");
try
{
@ -787,7 +799,7 @@ sealed class LottieFileProcessor
foreach (var (issue, uapVersionRange) in _translationIssues)
{
_reporter.WriteInfo(IssueToString(_file, issue, uapVersionRange));
_reporter.WriteInfo(InfoType.Issue, IssueToString(_file, issue, uapVersionRange));
}
// NOTE: this is only reporting on the latest version in a multi-version translation.
@ -815,7 +827,7 @@ sealed class LottieFileProcessor
{
foreach (var a in assetList)
{
_reporter.WriteInfo($"Generated code references {a}. Make sure your app can access this file.");
_reporter.WriteInfo(InfoType.Advice, $"Generated code references {a}. Make sure your app can access this file.");
}
}

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

@ -57,7 +57,7 @@ sealed class Program
var assemblyVersion = ThisAssembly.AssemblyInformationalVersion;
var toolNameAndVersion = $"Lottie for Windows Code Generator version {assemblyVersion}";
_reporter.WriteInfo(toolNameAndVersion);
_reporter.WriteInfo(InfoType.Signon, toolNameAndVersion);
_reporter.WriteInfoNewLine();
if (_options.ErrorDescription != null)
@ -180,7 +180,8 @@ sealed class Program
foreach (var (dataTableName, columnNames, rows) in _reporter.GetDataTables())
{
var tsvFilePath = System.IO.Path.Combine(outputFolder, $"LottieGen_{dataTableName}.tsv");
_reporter.WriteInfo($"Writing stats to {tsvFilePath}");
_reporter.WriteInfo("Writing stats to:");
_reporter.WriteInfo(InfoType.FilePath, $" {tsvFilePath}");
using (var tsvFile = File.CreateText(tsvFilePath))
{
tsvFile.WriteLine(string.Join("\t", columnNames));

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

@ -6,7 +6,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
sealed class Reporter
{
@ -28,15 +27,40 @@ sealed class Reporter
internal Writer ErrorStream { get; }
// Helper for writing errors to the error stream with a standard format.
internal void WriteError(string errorMessage)
{
ErrorStream.WriteLine($"Error: {errorMessage}");
}
internal void WriteError(string errorMessage) =>
WriteError(errorMessage, ConsoleColor.Red, ConsoleColor.Black);
// Helper for writing info lines to the info stream.
internal void WriteInfo(string infoMessage)
internal void WriteInfo(string infoMessage) =>
WriteInfo(InfoType.Default, infoMessage);
// Helper for writing info lines to the info stream.
internal void WriteInfo(InfoType type, string infoMessage)
{
InfoStream.WriteLine(infoMessage);
ConsoleColor foreground, background = ConsoleColor.Black;
switch (type)
{
case InfoType.Default:
foreground = ConsoleColor.Gray;
break;
case InfoType.Advice:
foreground = ConsoleColor.Green;
break;
case InfoType.FilePath:
foreground = ConsoleColor.Cyan;
break;
case InfoType.Issue:
foreground = ConsoleColor.Yellow;
break;
case InfoType.Signon:
foreground = ConsoleColor.White;
break;
default:
throw new ArgumentException();
}
WriteInfo(infoMessage, foreground, background);
}
// Writes a new line to the info stream.
@ -74,6 +98,26 @@ sealed class Reporter
}
}
// Helper for writing errors to the error stream with a standard format.
void WriteError(
string errorMessage,
ConsoleColor foregroundColor,
ConsoleColor backgroundColor)
{
ErrorStream.Color(foregroundColor, backgroundColor);
ErrorStream.WriteLine($"Error: {errorMessage}");
}
// Helper for writing info lines to the info stream.
void WriteInfo(
string infoMessage,
ConsoleColor foregroundColor,
ConsoleColor backgroundColor)
{
InfoStream.Color(foregroundColor, backgroundColor);
InfoStream.WriteLine(infoMessage);
}
internal sealed class Writer
{
readonly TextWriter _wrapped;
@ -83,8 +127,25 @@ sealed class Reporter
_wrapped = wrapped;
}
public void WriteLine() => _wrapped.WriteLine();
public void WriteLine()
{
_wrapped.WriteLine();
Console.ResetColor();
}
public void WriteLine(string value) => _wrapped.WriteLine(value);
}
public void WriteLine(string value)
{
_wrapped.WriteLine(value);
Console.ResetColor();
}
/// <summary>
/// Sets the color until the next line is output.
/// </summary>
public void Color(ConsoleColor foregroundColor, ConsoleColor backgroundColor)
{
Console.ForegroundColor = foregroundColor;
Console.BackgroundColor = backgroundColor;
}
}
}