[Help] Cleanup

This commit is contained in:
Matt Nadareski
2019-02-08 13:51:10 -08:00
parent b8cd0a10bb
commit e108c02eaa

View File

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