diff --git a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Extensions/StringExtensions.cs b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Extensions/StringExtensions.cs index 39501387..a1f11f3e 100644 --- a/essentials-framework/Essentials Core/PepperDashEssentialsBase/Extensions/StringExtensions.cs +++ b/essentials-framework/Essentials Core/PepperDashEssentialsBase/Extensions/StringExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.ComponentModel; using Crestron.SimplSharp; namespace PepperDash.Essentials.Core @@ -20,5 +21,109 @@ namespace PepperDash.Essentials.Core { return string.IsNullOrEmpty(s) ? newString : s; } + + /// + /// Formats string to meet specified parameters + /// + /// string to be formatted + /// length of output string + /// character to pad string with to reach desired output length + /// Justification of input string with regards to the overall string + /// If true, seperate input string from pad characters with a single space + /// + public static string AutoPadAndJustify(this string inputString, int outputStringLength, char padCharacter, + AutoPadJustification justification, bool separateInput) + { + var returnString = inputString; + var justifiedAndSeparateLength = ( + separateInput + ? (justification == AutoPadJustification.Center ? 2 : 1) + : 0); + if (outputStringLength <= inputString.Length + justifiedAndSeparateLength) return returnString; + var fillLength = outputStringLength - inputString.Length - justifiedAndSeparateLength; + switch (justification) + { + case (AutoPadJustification.Left): + { + returnString = + inputString + + new string(' ', separateInput ? 1 : 0) + + new string(padCharacter, fillLength); + break; + } + case (AutoPadJustification.Right): + { + returnString = + new string(padCharacter, fillLength) + + new string(' ', separateInput ? 1 : 0) + + inputString; + break; + } + case (AutoPadJustification.Center): + { + var halfFill = fillLength / 2; + returnString = + new string(padCharacter, halfFill + (fillLength % 2)) + + new string(' ', separateInput ? 1 : 0) + + inputString + + new string(' ', separateInput ? 1 : 0) + + new string(padCharacter, halfFill); + break; + } + } + return returnString; + } + + /// + /// Formats string to meet specified parameters + /// + /// string to be formatted + /// String formatting options + /// + public static string AutoPadAndJustify(this string inputString, AutoPadJustificationOptions options) + { + if (options == null) + return inputString; + + var outputStringLength = options.OutputStringLength; + var padCharacter = options.PadCharacter; + var justification = options.Justification; + var separateInput = options.SeparateInput; + + + + return AutoPadAndJustify(inputString, outputStringLength, padCharacter, justification, + separateInput); + } + } + + public enum AutoPadJustification + { + Center, + Left, + Right + } + + /// + /// Options for setting AutoPadJustification + /// + public class AutoPadJustificationOptions + { + /// + /// Text Justification for the string, relative to the length + /// + public AutoPadJustification Justification { get; set; } + /// + /// If true, separate input string from pad characters by a single ' ' character + /// + public bool SeparateInput { get; set; } + /// + /// Pad character to be inserted + /// + public char PadCharacter { get; set; } + /// + /// Total length of the output string + /// + public int OutputStringLength { get; set; } } } \ No newline at end of file