Files
SabreTools/SabreTools.Help/Feature.cs

430 lines
16 KiB
C#
Raw Normal View History

2020-12-07 12:33:24 -08:00
using System;
using System.Collections.Generic;
using System.Text;
2020-06-11 11:44:46 -07:00
2020-12-07 13:57:26 -08:00
namespace SabreTools.Help
{
public abstract class Feature
2019-02-08 13:47:44 -08:00
{
#region Fields
2019-02-08 13:47:44 -08:00
// <summary>
/// Indicates if the feature has been seen already
/// </summary>
protected bool _foundOnce = false;
2019-02-08 13:47:44 -08:00
#endregion
#region Properties
/// <summary>
/// Display name for the feature
/// </summary>
public string Name { get; protected set; }
2019-02-08 13:47:44 -08:00
/// <summary>
/// Set of flags associated with the feature
/// </summary>
2024-07-18 00:32:41 -04:00
public readonly List<string> Flags = [];
/// <summary>
/// Short description of the feature
/// </summary>
public string Description { get; protected set; }
/// <summary>
/// Optional long description of the feature
/// </summary>
2024-02-28 19:19:50 -05:00
public string? LongDescription { get; protected set; }
/// <summary>
/// Set of subfeatures associated with this feature
/// </summary>
2024-07-18 00:32:41 -04:00
public readonly Dictionary<string, Feature?> Features = [];
2019-02-08 13:47:44 -08:00
#endregion
#region Constructors
internal Feature(string name, string flag, string description, string? longDescription = null)
2019-02-08 13:47:44 -08:00
{
2024-07-18 00:23:16 -04:00
Name = name;
2024-07-18 00:32:41 -04:00
Flags.Add(flag);
2024-07-18 00:23:16 -04:00
Description = description;
LongDescription = longDescription;
2019-02-08 13:47:44 -08:00
}
internal Feature(string name, string[] flags, string description, string? longDescription = null)
2019-02-08 13:47:44 -08:00
{
2024-07-18 00:23:16 -04:00
Name = name;
2024-07-18 00:32:41 -04:00
Flags.AddRange(flags);
2024-07-18 00:23:16 -04:00
Description = description;
LongDescription = longDescription;
2019-02-08 13:47:44 -08:00
}
#endregion
#region Accessors
/// <summary>
/// Directly address a given subfeature
/// </summary>
2024-02-28 19:19:50 -05:00
public Feature? this[string name]
2019-02-08 13:47:44 -08:00
{
2024-07-18 00:23:16 -04:00
get { return Features.ContainsKey(name) ? Features[name] : null; }
set { Features[name] = value; }
2019-02-08 13:47:44 -08:00
}
/// <summary>
/// Directly address a given subfeature
/// </summary>
public Feature? this[Feature subfeature]
2019-02-08 13:47:44 -08:00
{
get { return Features.ContainsKey(subfeature.Name) ? Features[subfeature.Name] : null; }
2024-07-18 00:23:16 -04:00
set { Features[subfeature?.Name ?? string.Empty] = value; }
2019-02-08 13:47:44 -08:00
}
/// <summary>
/// Add a new feature for this feature
/// </summary>
/// <param name="feature"></param>
public void AddFeature(Feature feature)
{
2024-07-18 00:23:16 -04:00
lock (Features)
2019-02-08 13:47:44 -08:00
{
2024-07-18 00:23:16 -04:00
Features[feature.Name ?? string.Empty] = feature;
2019-02-08 13:47:44 -08:00
}
}
/// <summary>
/// Returns if a flag exists for the current feature
/// </summary>
/// <param name="name">Name of the flag to check</param>
/// <returns>True if the flag was found, false otherwise</returns>
public bool ContainsFlag(string name)
{
2024-11-12 21:12:06 -05:00
return Flags.Exists(f => f == name || f.TrimStart('-') == name);
2019-02-08 13:47:44 -08:00
}
/// <summary>
/// Returns if the feature contains a flag that starts with the given character
/// </summary>
/// <param name="c">Character to check against</param>
/// <returns>True if the flag was found, false otherwise</returns>
public bool StartsWith(char c)
{
2024-11-12 21:12:06 -05:00
return Flags.Exists(f => f.TrimStart('-').ToLowerInvariant()[0] == c);
2019-02-08 13:47:44 -08:00
}
#endregion
#region Instance Methods
/// <summary>
/// Output this feature only
/// </summary>
/// <param name="pre">Positive number representing number of spaces to put in front of the feature</param>
/// <param name="midpoint">Positive number representing the column where the description should start</param>
/// <param name="includeLongDescription">True if the long description should be formatted and output, false otherwise</param>
public List<string> Output(int pre = 0, int midpoint = 0, bool includeLongDescription = false)
{
// Create the output list
2024-02-28 19:19:50 -05:00
List<string> outputList = [];
2019-02-08 13:47:44 -08:00
// Build the output string first
var output = new StringBuilder();
2019-02-08 13:47:44 -08:00
// Add the pre-space first
output.Append(CreatePadding(pre));
2019-02-08 13:47:44 -08:00
// Preprocess and add the flags
output.Append(FormatFlags());
2019-02-08 13:47:44 -08:00
// If we have a midpoint set, check to see if the string needs padding
if (midpoint > 0 && output.ToString().Length < midpoint)
output.Append(CreatePadding(midpoint - output.ToString().Length));
2019-02-08 13:47:44 -08:00
else
output.Append(" ");
2019-02-08 13:47:44 -08:00
// Append the description
output.Append(Description);
2019-02-08 13:47:44 -08:00
// Now append it to the list
outputList.Add(output.ToString());
2019-02-08 13:47:44 -08:00
// If we are outputting the long description, format it and then add it as well
if (includeLongDescription)
{
// Get the width of the console for wrapping reference
int width = (Console.WindowWidth == 0 ? 80 : Console.WindowWidth) - 1;
2019-02-08 13:47:44 -08:00
// Prepare the output string
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(pre + 4));
2019-02-08 13:47:44 -08:00
// Now split the input description and start processing
2024-07-18 00:23:16 -04:00
string[]? split = LongDescription?.Split(' ');
2024-02-28 19:19:50 -05:00
if (split == null)
return outputList;
2019-02-08 13:47:44 -08:00
for (int i = 0; i < split.Length; i++)
{
// If we have a newline character, reset the line and continue
2024-11-12 21:12:06 -05:00
if (split[i].Contains("\n"))
2019-02-08 13:47:44 -08:00
{
string[] subsplit = split[i].Replace("\r", string.Empty).Split('\n');
2019-02-08 13:47:44 -08:00
for (int j = 0; j < subsplit.Length - 1; j++)
{
// Add the next word only if the total length doesn't go above the width of the screen
if (output.ToString().Length + subsplit[j].Length < width)
2019-02-08 13:47:44 -08:00
{
output.Append((output.ToString().Length == pre + 4 ? string.Empty : " ") + subsplit[j]);
2019-02-08 13:47:44 -08:00
}
// Otherwise, we want to cache the line to output and create a new blank string
else
{
outputList.Add(output.ToString());
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(pre + 4));
output.Append((output.ToString().Length == pre + 4 ? string.Empty : " ") + subsplit[j]);
2019-02-08 13:47:44 -08:00
}
outputList.Add(output.ToString());
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(pre + 4));
2019-02-08 13:47:44 -08:00
}
output.Append(subsplit[subsplit.Length - 1]);
2019-02-08 13:47:44 -08:00
continue;
}
// Add the next word only if the total length doesn't go above the width of the screen
if (output.ToString().Length + split[i].Length < width)
2019-02-08 13:47:44 -08:00
{
output.Append((output.ToString().Length == pre + 4 ? string.Empty : " ") + split[i]);
2019-02-08 13:47:44 -08:00
}
// Otherwise, we want to cache the line to output and create a new blank string
else
{
outputList.Add(output.ToString());
output.Append(CreatePadding(pre + 4));
output.Append((output.ToString().Length == pre + 4 ? string.Empty : " ") + split[i]);
2019-02-08 13:47:44 -08:00
}
}
// Add the last created output and a blank line for clarity
outputList.Add(output.ToString());
outputList.Add(string.Empty);
2019-02-08 13:47:44 -08:00
}
return outputList;
}
/// <summary>
/// Output this feature and all subfeatures
/// </summary>
/// <param name="tabLevel">Level of indentation for this feature</param>
/// <param name="pre">Positive number representing number of spaces to put in front of the feature</param>
/// <param name="midpoint">Positive number representing the column where the description should start</param>
/// <param name="includeLongDescription">True if the long description should be formatted and output, false otherwise</param>
public List<string> OutputRecursive(int tabLevel, int pre = 0, int midpoint = 0, bool includeLongDescription = false)
{
// Create the output list
2024-07-18 00:32:41 -04:00
List<string> outputList = [];
2019-02-08 13:47:44 -08:00
// Build the output string first
var output = new StringBuilder();
2019-02-08 13:47:44 -08:00
// Normalize based on the tab level
int preAdjusted = pre;
int midpointAdjusted = midpoint;
if (tabLevel > 0)
{
preAdjusted += 4 * tabLevel;
midpointAdjusted += 4 * tabLevel;
}
// Add the pre-space first
output.Append(CreatePadding(preAdjusted));
2019-02-08 13:47:44 -08:00
// Preprocess and add the flags
output.Append(FormatFlags());
2019-02-08 13:47:44 -08:00
// If we have a midpoint set, check to see if the string needs padding
if (midpoint > 0 && output.ToString().Length < midpointAdjusted)
output.Append(CreatePadding(midpointAdjusted - output.ToString().Length));
2019-02-08 13:47:44 -08:00
else
output.Append(" ");
2019-02-08 13:47:44 -08:00
// Append the description
output.Append(Description);
2019-02-08 13:47:44 -08:00
// Now append it to the list
outputList.Add(output.ToString());
2019-02-08 13:47:44 -08:00
// If we are outputting the long description, format it and then add it as well
if (includeLongDescription)
{
// Get the width of the console for wrapping reference
int width = (Console.WindowWidth == 0 ? 80 : Console.WindowWidth) - 1;
2019-02-08 13:47:44 -08:00
// Prepare the output string
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(preAdjusted + 4));
2019-02-08 13:47:44 -08:00
// Now split the input description and start processing
2024-07-18 00:23:16 -04:00
string[]? split = LongDescription?.Split(' ');
2024-02-28 19:19:50 -05:00
if (split == null)
return outputList;
2019-02-08 13:47:44 -08:00
for (int i = 0; i < split.Length; i++)
{
// If we have a newline character, reset the line and continue
2024-11-12 21:12:06 -05:00
if (split[i].Contains("\n"))
2019-02-08 13:47:44 -08:00
{
string[] subsplit = split[i].Replace("\r", string.Empty).Split('\n');
2019-02-08 13:47:44 -08:00
for (int j = 0; j < subsplit.Length - 1; j++)
{
// Add the next word only if the total length doesn't go above the width of the screen
if (output.ToString().Length + subsplit[j].Length < width)
2019-02-08 13:47:44 -08:00
{
output.Append((output.ToString().Length == preAdjusted + 4 ? string.Empty : " ") + subsplit[j]);
2019-02-08 13:47:44 -08:00
}
// Otherwise, we want to cache the line to output and create a new blank string
else
{
outputList.Add(output.ToString());
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(preAdjusted + 4));
output.Append((output.ToString().Length == preAdjusted + 4 ? string.Empty : " ") + subsplit[j]);
2019-02-08 13:47:44 -08:00
}
outputList.Add(output.ToString());
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(preAdjusted + 4));
2019-02-08 13:47:44 -08:00
}
output.Append(subsplit[subsplit.Length - 1]);
2019-02-08 13:47:44 -08:00
continue;
}
// Add the next word only if the total length doesn't go above the width of the screen
if (output.ToString().Length + split[i].Length < width)
2019-02-08 13:47:44 -08:00
{
output.Append((output.ToString().Length == preAdjusted + 4 ? string.Empty : " ") + split[i]);
2019-02-08 13:47:44 -08:00
}
// Otherwise, we want to cache the line to output and create a new blank string
else
{
outputList.Add(output.ToString());
#if NET20 || NET35
output = new();
#else
output.Clear();
#endif
output.Append(CreatePadding(preAdjusted + 4));
output.Append((output.ToString().Length == preAdjusted + 4 ? string.Empty : " ") + split[i]);
2019-02-08 13:47:44 -08:00
}
}
// Add the last created output and a blank line for clarity
outputList.Add(output.ToString());
outputList.Add(string.Empty);
2019-02-08 13:47:44 -08:00
}
// Now let's append all subfeatures
2024-07-18 00:23:16 -04:00
foreach (string feature in Features.Keys)
2019-02-08 13:47:44 -08:00
{
2024-07-18 00:23:16 -04:00
outputList.AddRange(Features[feature]!.OutputRecursive(tabLevel + 1, pre, midpoint, includeLongDescription));
2019-02-08 13:47:44 -08:00
}
return outputList;
}
/// <summary>
/// Validate whether a flag is valid for this feature or not
/// </summary>
/// <param name="input">Input to check against</param>
/// <param name="exact">True if just this feature should be checked, false if all subfeatures are checked as well</param>
/// <param name="ignore">True if the existing flag should be ignored, false otherwise</param>
/// <returns>True if the flag was valid, false otherwise</returns>
public abstract bool ValidateInput(string input, bool exact = false, bool ignore = false);
2019-02-08 13:47:44 -08:00
/// <summary>
/// Returns if this feature has a valid value or not
2019-02-08 13:47:44 -08:00
/// </summary>
/// <returns>True if the feature is enabled, false otherwise</returns>
public abstract bool IsEnabled();
/// <summary>
/// Pre-format the flags for output
/// </summary>
protected abstract string FormatFlags();
/// <summary>
/// Create a padding space based on the given length
/// </summary>
/// <param name="spaces">Number of padding spaces to add</param>
/// <returns>String with requested number of blank spaces</returns>
private static string CreatePadding(int spaces) => string.Empty.PadRight(spaces);
#endregion
}
public abstract class Feature<T> : Feature
{
public T? Value { get; protected set; }
#region Constructors
internal Feature(string name, string flag, string description, string? longDescription = null)
: base(name, flag, description, longDescription)
{
2019-02-08 13:47:44 -08:00
}
internal Feature(string name, string[] flags, string description, string? longDescription = null)
: base(name, flags, description, longDescription)
2019-02-08 13:47:44 -08:00
{
}
#endregion
#region Instance Methods
/// <inheritdoc/>
public override abstract bool ValidateInput(string input, bool exact = false, bool ignore = false);
/// <inheritdoc/>
public override abstract bool IsEnabled();
/// <inheritdoc/>
protected override abstract string FormatFlags();
#endregion
2019-02-08 13:47:44 -08:00
}
}