Files
SabreTools/SabreTools.Help/FeatureSet.cs

368 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-12-07 13:57:26 -08:00
namespace SabreTools.Help
{
2020-12-08 17:05:08 -08:00
public class FeatureSet
2019-02-08 13:51:10 -08:00
{
#region Private variables
2024-07-18 00:32:41 -04:00
private readonly List<string> _header = [];
private readonly Dictionary<string, Feature?> _features = [];
private const string _barrier = "-----------------------------------------";
2019-02-08 13:51:10 -08:00
#endregion
#region Constructors
2020-12-08 17:05:08 -08:00
public FeatureSet(List<string> header)
2019-02-08 13:51:10 -08:00
{
2024-07-18 00:32:41 -04:00
_header.AddRange(header);
2019-02-08 13:51:10 -08:00
}
#endregion
#region Accessors
2024-02-28 19:19:50 -05:00
public Feature? this[string name]
2019-02-08 13:51:10 -08:00
{
get
{
if (!_features.ContainsKey(name))
return null;
return _features[name];
}
set
{
_features[name] = value;
}
}
2024-02-28 19:19:50 -05:00
public Feature? this[Feature subfeature]
2019-02-08 13:51:10 -08:00
{
get
{
2024-02-28 19:19:50 -05:00
if (subfeature.Name == null)
return null;
2019-02-08 13:51:10 -08:00
if (!_features.ContainsKey(subfeature.Name))
return null;
return _features[subfeature.Name];
}
set
{
2024-02-28 19:19:50 -05:00
if (subfeature.Name != null)
_features[subfeature.Name] = value;
2019-02-08 13:51:10 -08:00
}
}
/// <summary>
/// Add a new feature to the help
/// </summary>
/// <param name="feature">Feature object to map to</param>
public void Add(Feature feature)
{
2024-02-28 19:19:50 -05:00
if (feature.Name == null)
return;
2019-02-08 13:51:10 -08:00
lock (_features)
{
_features.Add(feature.Name, feature);
}
}
#endregion
#region Instance Methods
/// <summary>
/// Get the feature name for a given flag or short name
/// </summary>
/// <returns>Feature name</returns>
public string GetFeatureName(string name)
{
2024-11-12 21:12:06 -05:00
foreach (var key in _features.Keys)
{
// Skip invalid features
var feature = _features[key];
if (feature == null)
continue;
// If validation passes
if (feature.ValidateInput(name, exact: true, ignore: true))
return key;
}
// No feature could be found
return string.Empty;
2019-02-08 13:51:10 -08:00
}
/// <summary>
/// Output top-level features only
/// </summary>
public void OutputGenericHelp()
{
// Start building the output list
2024-02-28 19:19:50 -05:00
List<string> output = [];
2019-02-08 13:51:10 -08:00
// Append the header first
output.AddRange(_header);
// Now append all available top-level flags
output.Add("Available options:");
foreach (string feature in _features.Keys)
{
2024-02-28 19:19:50 -05:00
var outputs = _features[feature]?.Output(pre: 2, midpoint: 30);
if (outputs != null)
output.AddRange(outputs);
2019-02-08 13:51:10 -08:00
}
// And append the generic ending
output.Add(string.Empty);
2019-02-08 13:51:10 -08:00
output.Add("For information on available flags, put the option name after help");
// Now write out everything in a staged manner
WriteOutWithPauses(output);
}
/// <summary>
/// Output all features recursively
/// </summary>
public void OutputAllHelp()
{
// Start building the output list
2024-02-28 19:19:50 -05:00
List<string> output = [];
2019-02-08 13:51:10 -08:00
// Append the header first
output.AddRange(_header);
// Now append all available flags recursively
output.Add("Available options:");
foreach (string feature in _features.Keys)
{
2024-02-28 19:19:50 -05:00
var outputs = _features[feature]?.OutputRecursive(0, pre: 2, midpoint: 30, includeLongDescription: true);
if (outputs != null)
output.AddRange(outputs);
2019-02-08 13:51:10 -08:00
}
// Now write out everything in a staged manner
WriteOutWithPauses(output);
}
/// <summary>
/// Output the SabreTools suite credits
/// </summary>
public static void OutputCredits()
2019-02-08 13:51:10 -08:00
{
2024-02-28 19:19:50 -05:00
List<string> credits =
[
_barrier,
"Credits",
_barrier,
string.Empty,
"Programmer / Lead: Matt Nadareski (darksabre76)",
"Additional code: emuLOAD, @tractivo, motoschifo",
"Testing: emuLOAD, @tractivo, Kludge, Obiwantje, edc",
"Suggestions: edc, AcidX, Amiga12, EliUmniCk",
"Based on work by: The Wizard of DATz"
2024-02-28 19:19:50 -05:00
];
2019-02-08 13:51:10 -08:00
WriteOutWithPauses(credits);
}
/// <summary>
/// Output a single feature recursively
/// </summary>
/// <param name="featurename">Name of the feature to output information for, if possible</param>
/// <param name="includeLongDescription">True if the long description should be formatted and output, false otherwise</param>
2024-02-28 19:19:50 -05:00
public void OutputIndividualFeature(string? featurename, bool includeLongDescription = false)
2019-02-08 13:51:10 -08:00
{
// Start building the output list
2024-02-28 19:19:50 -05:00
List<string> output = [];
2019-02-08 13:51:10 -08:00
2020-09-30 10:01:04 -07:00
// If the feature name is null, empty, or just consisting of `-` characters, just show everything
if (string.IsNullOrEmpty(featurename?.TrimStart('-')))
{
OutputGenericHelp();
return;
}
2019-02-08 13:51:10 -08:00
// Now try to find the feature that has the name included
2024-02-28 19:19:50 -05:00
string? realname = null;
List<string> startsWith = [];
2019-02-08 13:51:10 -08:00
foreach (string feature in _features.Keys)
{
// If we have a match to the feature name somehow
if (feature == featurename)
{
realname = feature;
break;
}
2024-02-28 19:19:50 -05:00
// If we have an invalid feature
else if (!_features.ContainsKey(feature) || _features[feature] == null)
{
startsWith.Add(feature);
}
2019-02-08 13:51:10 -08:00
// If we have a match within the flags
2024-02-28 23:14:21 -05:00
else if (_features[feature]!.ContainsFlag(featurename!))
2019-02-08 13:51:10 -08:00
{
realname = feature;
break;
}
// Otherwise, we want to get features with the same start
2024-02-28 23:14:21 -05:00
else if (_features[feature]!.StartsWith(featurename!.TrimStart('-')[0]))
2019-02-08 13:51:10 -08:00
{
startsWith.Add(feature);
}
}
// If we have a real name found, append all available subflags recursively
if (realname != null)
{
output.Add($"Available options for {realname}:");
2024-02-28 19:19:50 -05:00
output.AddRange(_features[realname]!.OutputRecursive(0, pre: 2, midpoint: 30, includeLongDescription: includeLongDescription));
2019-02-08 13:51:10 -08:00
}
// If no name was found but we have possible matches, show them
else if (startsWith.Count > 0)
{
output.Add($"\"{featurename}\" not found. Did you mean:");
2019-02-08 13:51:10 -08:00
foreach (string possible in startsWith)
{
2024-02-28 19:19:50 -05:00
output.AddRange(_features[possible]!.Output(pre: 2, midpoint: 30, includeLongDescription: includeLongDescription));
2019-02-08 13:51:10 -08:00
}
}
// Now write out everything in a staged manner
WriteOutWithPauses(output);
}
/// <summary>
/// Check if a flag is a top-level (main application) flag
/// </summary>
/// <param name="flag">Name of the flag to check</param>
/// <returns>True if the feature was found, false otherwise</returns>
public bool TopLevelFlag(string flag)
{
2024-11-12 21:12:06 -05:00
foreach (var key in _features.Keys)
{
// Skip invalid features
var feature = _features[key];
if (feature == null)
continue;
// If validation passes
if (feature.ValidateInput(flag, exact: true))
return true;
}
// No feature could be found
return false;
2019-02-08 13:51:10 -08:00
}
/// <summary>
/// Retrieve a list of enabled features
/// </summary>
/// <returns>List of Features representing what is enabled</returns>
2024-02-28 19:19:50 -05:00
public Dictionary<string, Feature?> GetEnabledFeatures()
2019-02-08 13:51:10 -08:00
{
2024-02-28 19:19:50 -05:00
Dictionary<string, Feature?> enabled = [];
2019-02-08 13:51:10 -08:00
// Loop through the features
2024-02-28 19:19:50 -05:00
foreach (var feature in _features)
2019-02-08 13:51:10 -08:00
{
2024-02-28 19:19:50 -05:00
var temp = GetEnabledSubfeatures(feature.Key, feature.Value);
foreach (var tempfeat in temp)
2019-02-08 13:51:10 -08:00
{
if (!enabled.ContainsKey(tempfeat.Key))
enabled.Add(tempfeat.Key, null);
enabled[tempfeat.Key] = tempfeat.Value;
}
}
return enabled;
}
/// <summary>
/// Retrieve a nested list of subfeatures from the current feature
/// </summary>
/// <param name="key">Name that should be assigned to the feature</param>
/// <param name="feature">Feature with possible subfeatures to test</param>
/// <returns>List of Features representing what is enabled</returns>
2024-10-23 21:11:07 -04:00
private static Dictionary<string, Feature?> GetEnabledSubfeatures(string key, Feature? feature)
2019-02-08 13:51:10 -08:00
{
2024-02-28 19:19:50 -05:00
Dictionary<string, Feature?> enabled = [];
2024-02-28 23:14:21 -05:00
2024-02-28 19:19:50 -05:00
// If the feature is invalid
if (feature == null)
return enabled;
2019-02-08 13:51:10 -08:00
// First determine if the current feature is enabled
if (feature.IsEnabled())
enabled.Add(key, feature);
// Now loop through the subfeatures recursively
2024-02-28 19:19:50 -05:00
foreach (KeyValuePair<string, Feature?> sub in feature.Features)
2019-02-08 13:51:10 -08:00
{
2024-02-28 19:19:50 -05:00
var temp = GetEnabledSubfeatures(sub.Key, sub.Value);
foreach (var tempfeat in temp)
2019-02-08 13:51:10 -08:00
{
if (!enabled.ContainsKey(tempfeat.Key))
enabled.Add(tempfeat.Key, null);
2019-02-08 13:51:10 -08:00
enabled[tempfeat.Key] = tempfeat.Value;
}
}
return enabled;
}
/// <summary>
/// Write out the help text with pauses, if needed
/// </summary>
private static void WriteOutWithPauses(List<string> helptext)
2019-02-08 13:51:10 -08:00
{
// Now output based on the size of the screen
int i = 0;
for (int line = 0; line < helptext.Count; line++)
{
string help = helptext[line];
Console.WriteLine(help);
i++;
// If we're not being redirected and we reached the size of the screen, pause
if (i == Console.WindowHeight - 3 && line != helptext.Count - 1)
{
i = 0;
Pause();
}
}
2019-02-08 13:51:10 -08:00
Pause();
}
/// <summary>
/// Pause on console output
/// </summary>
private static void Pause()
{
2024-02-28 23:14:21 -05:00
#if NET452_OR_GREATER || NETCOREAPP
2019-02-08 13:51:10 -08:00
if (!Console.IsOutputRedirected)
2024-02-28 23:14:21 -05:00
#endif
2019-02-08 13:51:10 -08:00
{
Console.WriteLine();
Console.WriteLine("Press enter to continue...");
Console.ReadLine();
}
}
#endregion
}
}