// Copyright Epic Games, Inc. All Rights Reserved.
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EpicGames.Core
{
///
/// Utility functions for showing help for objects
///
public static class HelpUtils
{
///
/// Gets the width of the window for formatting purposes
///
public static int WindowWidth => ConsoleUtils.WindowWidth;
///
/// Prints help for the given object type
///
/// Type to print help for
public static void PrintHelp(string Title, Type Type, ILogger Logger)
{
PrintHelp(Title, GetDescription(Type), CommandLineArguments.GetParameters(Type), WindowWidth, Logger);
}
///
/// Prints help for a command
///
/// Title for the help text
/// Description for the command
/// List of parameters
/// Maximum width for each line
/// The output logger
public static void PrintHelp(string Title, string Description, List> Parameters, int MaxWidth, ILogger Logger)
{
bool bFirstLine = true;
if (!String.IsNullOrEmpty(Title))
{
PrintParagraph(Title, MaxWidth, Logger);
bFirstLine = false;
}
if (!String.IsNullOrEmpty(Description))
{
if (!bFirstLine)
{
Logger.LogInformation("");
}
PrintParagraph(Description, MaxWidth, Logger);
bFirstLine = false;
}
if (Parameters.Count > 0)
{
if (!bFirstLine)
{
Logger.LogInformation("");
}
Logger.LogInformation("Parameters:");
PrintTable(Parameters, 4, 24, MaxWidth, Logger);
}
}
///
/// Gets the description from a type
///
/// The type to get a description for
/// The description text
public static string GetDescription(Type Type)
{
StringBuilder DescriptionText = new StringBuilder();
foreach (DescriptionAttribute Attribute in Type.GetCustomAttributes(typeof(DescriptionAttribute), false))
{
if (DescriptionText.Length > 0)
{
DescriptionText.AppendLine();
}
DescriptionText.AppendLine(Attribute.Description);
}
return DescriptionText.ToString();
}
///
/// Prints a paragraph of text using word wrapping
///
/// Text to print
/// Logger implementation to write to
public static void PrintParagraph(string Text, ILogger Logger)
{
PrintParagraph(Text, WindowWidth - 1, Logger);
}
///
/// Prints a paragraph of text using word wrapping
///
/// Text to print
/// Maximum width for each line
/// Logger implementation to write to
public static void PrintParagraph(string Text, int MaxWidth, ILogger Logger)
{
IEnumerable Lines = StringUtils.WordWrap(Text, MaxWidth);
foreach (string Line in Lines)
{
Logger.LogInformation(Line);
}
}
///
/// Formats the given parameters as so:
/// -Param1 Param1 Description
///
/// -Param2 Param2 Description, this description is
/// longer and splits onto a separate line.
///
/// -Param3 Param3 Description continues as before.
///
/// List of parameters arranged as "-ParamName Param Description"
/// Indent from the left hand side
/// The minimum padding from the start of the param name to the start of the description (resizes with larger param names)
/// Logger implementation to write to
///
public static void PrintTable(List> Items, int Indent, int MinFirstColumnWidth, ILogger Logger)
{
PrintTable(Items, Indent, MinFirstColumnWidth, WindowWidth - 1, Logger);
}
///
/// Formats the given parameters as so:
/// -Param1 Param1 Description
///
/// -Param2 Param2 Description, this description is
/// longer and splits onto a separate line.
///
/// -Param3 Param3 Description continues as before.
///
/// List of parameters arranged as "-ParamName Param Description"
/// Indent from the left hand side
/// The minimum padding from the start of the param name to the start of the description (resizes with larger param names)
/// Logger implementation to write to
///
public static void PrintTable(List> Items, int Indent, int MinFirstColumnWidth, int MaxWidth, ILogger Logger)
{
if(Items.Count > 0)
{
// string used to intent the param
string IndentString = new string(' ', Indent);
// default the padding value
int RightPadding = Math.Max(MinFirstColumnWidth, Items.Max(x => x.Key.Length + 1));
// Build the formatted params
foreach(KeyValuePair Item in Items)
{
// build the param first, including intend and padding on the rights size
string ParamString = IndentString + Item.Key.PadRight(RightPadding);
// Build the description line by line, adding the same amount of intending each time.
IEnumerable DescriptionLines = StringUtils.WordWrap(Item.Value, MaxWidth - ParamString.Length);
foreach(string DescriptionLine in DescriptionLines)
{
// Formatting as following:
// -paramDescription
Logger.LogInformation(ParamString + DescriptionLine);
// we replace the param string on subsequent lines with white space of the same length
ParamString = string.Empty.PadRight(IndentString.Length + RightPadding);
}
}
}
}
}
}