// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Tools.DotNETCommon { /// /// Utility functions for showing help for objects /// [Obsolete("Functionality in the Tools.DotNETCommon namespace is deprecated. Please reference the EpicGames.Core namespace and assembly instead.")] public static class HelpUtils { /// /// Gets the width of the window for formatting purposes /// public static int WindowWidth { get { // Get the window width, using a default value if there's no console attached to this process. int NewWindowWidth; try { NewWindowWidth = Console.WindowWidth; } catch { NewWindowWidth = 240; } if (NewWindowWidth <= 0) { NewWindowWidth = 240; } return NewWindowWidth; } } /// /// Prints help for the given object type /// /// Type to print help for public static void PrintHelp(string Title, Type Type) { PrintHelp(Title, GetDescription(Type), CommandLineArguments.GetParameters(Type)); } /// /// Prints help for a command /// /// Title for the help text /// Description for the command /// List of parameters public static void PrintHelp(string Title, string Description, List> Parameters) { int MaxWidth = WindowWidth; bool bFirstLine = true; if (!String.IsNullOrEmpty(Title)) { PrintParagraph(Title, MaxWidth); bFirstLine = false; } if (!String.IsNullOrEmpty(Description)) { if (!bFirstLine) { Log.TraceInformation(""); } PrintParagraph(Description, MaxWidth); bFirstLine = false; } if (Parameters.Count > 0) { if (!bFirstLine) { Log.TraceInformation(""); } Log.TraceInformation("Parameters:"); PrintTable(Parameters, 4, 24, MaxWidth); } } /// /// 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 /// Maximum width for each line public static void PrintParagraph(string Text) { PrintParagraph(Text, WindowWidth - 1); } /// /// Prints a paragraph of text using word wrapping /// /// Text to print /// Maximum width for each line public static void PrintParagraph(string Text, int MaxWidth) { List Lines = StringUtils.WordWrap(Text, MaxWidth); foreach (string Line in Lines) { Log.TraceInformation("{0}", 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) /// public static void PrintTable(List> Items, int Indent, int MinFirstColumnWidth) { PrintTable(Items, Indent, MinFirstColumnWidth, WindowWidth - 1); } /// /// 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) /// public static void PrintTable(List> Items, int Indent, int MinFirstColumnWidth, int MaxWidth) { 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. List DescriptionLines = StringUtils.WordWrap(Item.Value, MaxWidth - ParamString.Length); foreach(string DescriptionLine in DescriptionLines) { // Formatting as following: // -paramDescription Log.TraceInformation("{0}{1}", ParamString, DescriptionLine); // we replace the param string on subsequent lines with white space of the same length ParamString = string.Empty.PadRight(IndentString.Length + RightPadding); } } } } } }