From e7baa7d33f54ee2c0f07df44595852b453f80ddc Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 4 Dec 2024 21:20:38 -0500 Subject: [PATCH] Add self-formatting to Input types --- CHANGELIST.md | 1 + MPF.ExecutionContexts/Data/BooleanInput.cs | 30 +++++++++++++++++ MPF.ExecutionContexts/Data/FlagInput.cs | 21 ++++++++++++ MPF.ExecutionContexts/Data/Input.cs | 8 ++++- MPF.ExecutionContexts/Data/Int16Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/Int32Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/Int64Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/Int8Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/StringInput.cs | 38 ++++++++++++++++++++++ MPF.ExecutionContexts/Data/UInt16Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/UInt32Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/UInt64Input.cs | 37 +++++++++++++++++++++ MPF.ExecutionContexts/Data/UInt8Input.cs | 37 +++++++++++++++++++++ 13 files changed, 393 insertions(+), 1 deletion(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index 78436beb..3ed182ae 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -28,6 +28,7 @@ - Split Input type into typed classes - Add flag for value being set - Add unused inputs to Aaru +- Add self-formatting to Input types ### 3.2.4 (2024-11-24) diff --git a/MPF.ExecutionContexts/Data/BooleanInput.cs b/MPF.ExecutionContexts/Data/BooleanInput.cs index a78e7a94..fe5dfe6b 100644 --- a/MPF.ExecutionContexts/Data/BooleanInput.cs +++ b/MPF.ExecutionContexts/Data/BooleanInput.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace MPF.ExecutionContexts.Data { /// @@ -25,6 +27,34 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString().ToLowerInvariant()); + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/FlagInput.cs b/MPF.ExecutionContexts/Data/FlagInput.cs index 664b5627..3b599ab4 100644 --- a/MPF.ExecutionContexts/Data/FlagInput.cs +++ b/MPF.ExecutionContexts/Data/FlagInput.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace MPF.ExecutionContexts.Data { /// @@ -25,6 +27,25 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == false) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/Input.cs b/MPF.ExecutionContexts/Data/Input.cs index b7ea9972..d7da252c 100644 --- a/MPF.ExecutionContexts/Data/Input.cs +++ b/MPF.ExecutionContexts/Data/Input.cs @@ -71,7 +71,13 @@ namespace MPF.ExecutionContexts.Data #endregion - #region Processors + #region Functionality + + /// + /// Create a formatted representation of the input and possible value + /// + /// Use an equal sign as a separator on output + public abstract string Format(bool useEquals); /// /// Process the current index, if possible diff --git a/MPF.ExecutionContexts/Data/Int16Input.cs b/MPF.ExecutionContexts/Data/Int16Input.cs index 601407ab..8d8f2f6a 100644 --- a/MPF.ExecutionContexts/Data/Int16Input.cs +++ b/MPF.ExecutionContexts/Data/Int16Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == short.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != short.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/Int32Input.cs b/MPF.ExecutionContexts/Data/Int32Input.cs index 7ff2b96e..8c0ab0b2 100644 --- a/MPF.ExecutionContexts/Data/Int32Input.cs +++ b/MPF.ExecutionContexts/Data/Int32Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == int.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != int.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/Int64Input.cs b/MPF.ExecutionContexts/Data/Int64Input.cs index 3d0baf59..bc4ee687 100644 --- a/MPF.ExecutionContexts/Data/Int64Input.cs +++ b/MPF.ExecutionContexts/Data/Int64Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == long.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != long.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/Int8Input.cs b/MPF.ExecutionContexts/Data/Int8Input.cs index 2c3c18bf..539f2272 100644 --- a/MPF.ExecutionContexts/Data/Int8Input.cs +++ b/MPF.ExecutionContexts/Data/Int8Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == sbyte.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != sbyte.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/StringInput.cs b/MPF.ExecutionContexts/Data/StringInput.cs index bfd6585f..93623a02 100644 --- a/MPF.ExecutionContexts/Data/StringInput.cs +++ b/MPF.ExecutionContexts/Data/StringInput.cs @@ -1,3 +1,5 @@ +using System.Text; + namespace MPF.ExecutionContexts.Data { /// @@ -25,6 +27,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == string.Empty) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != string.Empty)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/UInt16Input.cs b/MPF.ExecutionContexts/Data/UInt16Input.cs index 8f05988e..0fda65cd 100644 --- a/MPF.ExecutionContexts/Data/UInt16Input.cs +++ b/MPF.ExecutionContexts/Data/UInt16Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == ushort.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != ushort.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/UInt32Input.cs b/MPF.ExecutionContexts/Data/UInt32Input.cs index c6afb746..f94f0ecd 100644 --- a/MPF.ExecutionContexts/Data/UInt32Input.cs +++ b/MPF.ExecutionContexts/Data/UInt32Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == uint.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != uint.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/UInt64Input.cs b/MPF.ExecutionContexts/Data/UInt64Input.cs index 9d416aea..ba282faa 100644 --- a/MPF.ExecutionContexts/Data/UInt64Input.cs +++ b/MPF.ExecutionContexts/Data/UInt64Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == ulong.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != ulong.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) { diff --git a/MPF.ExecutionContexts/Data/UInt8Input.cs b/MPF.ExecutionContexts/Data/UInt8Input.cs index eb847466..d0b8c495 100644 --- a/MPF.ExecutionContexts/Data/UInt8Input.cs +++ b/MPF.ExecutionContexts/Data/UInt8Input.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Text; namespace MPF.ExecutionContexts.Data { @@ -27,6 +28,42 @@ namespace MPF.ExecutionContexts.Data #endregion + /// + public override string Format(bool useEquals) + { + // Do not output if there is no value + if (Value == null) + return string.Empty; + + // Do not output if required value is invalid + if (_required && Value == byte.MinValue) + return string.Empty; + + // Build the output format + var builder = new StringBuilder(); + + // Flag name + if (_longName != null) + builder.Append(_longName); + else + builder.Append(Name); + + // Only output separator and value if needed + if (_required || (!_required && Value != byte.MinValue)) + { + // Separator + if (useEquals) + builder.Append("="); + else + builder.Append(" "); + + // Value + builder.Append(Value.ToString()); + } + + return builder.ToString(); + } + /// public override bool Process(string[] parts, ref int index) {