public static string GetNextLine(ref int currentIndex, DrCmdTextBuilderFormat format, string[] words) { int count = GetElementCountForNewLine(currentIndex, format, words); string result = format.SpaceLeft + String.Join(" ", words, currentIndex, count); if ((format.Justify) && (result.Length != format.Width) && (currentIndex + count < words.Length)) // need to Align Left and Right { var deficit = (format.Width - result.Length); var resBuilder = new StringBuilder(); int step = (deficit / count) + 1; int extraDeficit = deficit - (step - 1) * (count - 1); var stepString = new string(' ', step); for (int i = currentIndex; i < currentIndex + count; i++) { if (resBuilder.Length != 0) { resBuilder.Append(stepString); if (extraDeficit > 0) { extraDeficit--; resBuilder.Append(" "); } } else { resBuilder.Append(format.SpaceLeft); // add left margin } resBuilder.Append(words[i]); } result = resBuilder.ToString(); } currentIndex += count - 1; return(result); }
/// <summary> /// Returns help line contains application name /// </summary> /// <returns></returns> private string GetHelpApplicationName() { var text = GetSettingsApplicationName() + " - "; var frmt = new TextBuilder.DrCmdTextBuilderFormat(0, GetSettingsHelpMaxLineLength(), true, true); var builder = new TextBuilder.DrCmdTextBuilder(frmt); text += builder.BuildText(GetSettingsApplicationDescription()); return(text); }
/// <summary> /// Returns help line contains application synopsis like 'Application.exe - {COMMAND1 | COMMAND2}' /// </summary> /// <returns></returns> private string GetHelpSynopsis() { var tab = GetHelpTab(); var text = tab + GetSettingsApplicationName() + " "; var frmt = new TextBuilder.DrCmdTextBuilderFormat(text.Length, GetSettingsHelpMaxLineLength(), true, true); var builder = new TextBuilder.DrCmdTextBuilder(frmt); text += builder.BuildText("{" + GetCommandsNamesForHelp() + "}"); return(text); }
/// <summary> /// Calculates element of word from words array for end of line /// </summary> /// <param name="startIndex">Index for start calculate</param> /// <param name="format">Formats of text</param> /// <param name="words">Words array</param> /// <returns></returns> /// <remarks>Return the number of elements for new line.</remarks>> public static int GetElementCountForNewLine(int startIndex, DrCmdTextBuilderFormat format, string[] words) { var maxLength = format.Length; var lenght = 0; int nextIndex = startIndex; while (nextIndex < words.Length) { lenght += words[nextIndex].Length +1; nextIndex++; if ((nextIndex < words.Length) && ((lenght + words[nextIndex].Length) > maxLength)) break; // it's maximum } return nextIndex - startIndex; }
/// <summary> /// Calculates element of word from words array for end of line /// </summary> /// <param name="startIndex">Index for start calculate</param> /// <param name="format">Formats of text</param> /// <param name="words">Words array</param> /// <returns></returns> /// <remarks>Return the number of elements for new line.</remarks>> static public int GetElementCountForNewLine(int startIndex, DrCmdTextBuilderFormat format, string[] words) { var maxLength = format.Length; var lenght = 0; int nextIndex = startIndex; while (nextIndex < words.Length) { lenght += words[nextIndex].Length + 1; nextIndex++; if ((nextIndex < words.Length) && ((lenght + words[nextIndex].Length) > maxLength)) { break; // it's maximum } } return(nextIndex - startIndex); }
/// <summary> /// Returns help lines contains command description like /// COMMAND1 - it's command 1 /// COMMAND2 - it's command 2 /// </summary> /// <returns></returns> private string GetHelpCommandsDescriptions() { var text = string.Empty; var tab = GetHelpTab(); var maxCommandNameLength = this.GetMaximumCommandNameLength() + 1; var leftMargin = tab.Length + maxCommandNameLength; var frmt = new TextBuilder.DrCmdTextBuilderFormat(leftMargin, GetSettingsHelpMaxLineLength(), true, true); var builder = new TextBuilder.DrCmdTextBuilder(frmt); foreach (var command in Commands) { if (command.IsEnabled()) { text += tab + command.Name + (new string(' ', maxCommandNameLength - command.Name.Length)) + builder.BuildText(command.GetCommandDescription()); } } return(text); }
public static void TestGetElementCountForNewLine(string[] words, DrCmdTextBuilderFormat format, int startIndex) { var actual = DrCmdTextBuilder.GetElementCountForNewLine(startIndex, format, words); var length = 0; int expected = startIndex; while (expected < words.Length) { length += words[expected].Length + 1; expected++; if ((expected < words.Length) && (length + words[expected].Length > format.Length)) break; } expected = expected - startIndex; Assert.AreEqual(expected, actual, string.Format("Actual element count'{0}' not equals expected '{1}'.", actual, expected)); var lineLenght = String.Join(" ", words, startIndex, actual); Assert.IsTrue(lineLenght.Length <= format.Length, string.Format("Actual text '{0}' not equals format.lenght '{1}'.", lineLenght, format.Length)); // The next element can be joined to line. if (expected + startIndex + 1 < words.Length) { Assert.IsFalse( ((lineLenght.Length + 1 + words[startIndex + expected + 1].Length) <= format.Length), "The next element can be joined to line."); } }
public void ValidateFormatedText(DrCmdTextBuilderFormat format, string original, string formated) { var lines = formated.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); CheckStringTextByFormat(format, lines); IntegrityTextCheck(original, formated); }
public void TestBuildTextJustifyTrue() { var marginLeft = 10; var textWidth = 80; var justify = true; var drTextFormat = new DrCmdTextBuilderFormat(marginLeft, textWidth, justify, false); var drCmdTextBuilder = new DrCmdTextBuilder(drTextFormat); var result = drCmdTextBuilder.BuildText(textSample1); ValidateFormatedText(drTextFormat, textSample1, result); }
/// <summary> /// Check a string text specified format /// </summary> /// <param name="format">format</param> /// <param name="lines">array of strings</param> public void CheckStringTextByFormat(DrCmdTextBuilderFormat format, string[] lines) { int lineCount = 0; foreach (var line in lines) { lineCount++; if ((format.Justify) && (lineCount != lines.Length)) // exclude the last line { Assert.IsTrue(line.Length == format.Width, "Enabled aligned left and right, the string width must be equals format width."); } else { Assert.IsTrue(line.Length <= format.Width, "The string width must be less or equals format width."); } if (format.MarginLeftChars != 0) { Assert.IsTrue(line.StartsWith(format.SpaceLeft), String.Format("Each line must begin with '{0}' gaps.", format.MarginLeftChars.ToString())); } Assert.IsFalse(line[format.MarginLeftChars] == ' ', "Each line should not start with a space include left margin."); } }
/// <summary> /// Returns help line contains application synopsis like 'Application.exe - {COMMAND1 | COMMAND2}' /// </summary> /// <returns></returns> private string GetHelpSynopsis() { var tab = GetHelpTab(); var text = tab + GetSettingsApplicationName() + " "; var frmt = new TextBuilder.DrCmdTextBuilderFormat(text.Length, GetSettingsHelpMaxLineLength(), true, true); var builder = new TextBuilder.DrCmdTextBuilder(frmt); text+=builder.BuildText("{" + GetCommandsNamesForHelp() + "}"); return text; }
/// <summary> /// Returns help lines contains command description like /// COMMAND1 - it's command 1 /// COMMAND2 - it's command 2 /// </summary> /// <returns></returns> private string GetHelpCommandsDescriptions() { var text = string.Empty; var tab = GetHelpTab(); var maxCommandNameLength = this.GetMaximumCommandNameLength()+1; var leftMargin = tab.Length + maxCommandNameLength ; var frmt = new TextBuilder.DrCmdTextBuilderFormat(leftMargin, GetSettingsHelpMaxLineLength(), true, true); var builder = new TextBuilder.DrCmdTextBuilder(frmt); foreach (var command in Commands) { if (command.IsEnabled()) { text += tab + command.Name + (new string(' ', maxCommandNameLength - command.Name.Length)) + builder.BuildText(command.GetCommandDescription()) ; } } return text ; }
/// <summary> /// Returns help line contains application name /// </summary> /// <returns></returns> private string GetHelpApplicationName() { var text = GetSettingsApplicationName() + " - "; var frmt = new TextBuilder.DrCmdTextBuilderFormat(0, GetSettingsHelpMaxLineLength(), true, true); var builder = new TextBuilder.DrCmdTextBuilder(frmt); text += builder.BuildText(GetSettingsApplicationDescription()); return text; }
/// <summary> /// Initialyze text builder by words to the lines depending by margins from left to right. /// </summary> /// <param name="format">Text formatting options</param> public DrCmdTextBuilder(DrCmdTextBuilderFormat format) { this.Format = format; }
public static string GetNextLine(ref int currentIndex, DrCmdTextBuilderFormat format, string[] words) { int count = GetElementCountForNewLine(currentIndex, format, words); string result = format.SpaceLeft + String.Join(" ", words, currentIndex, count); if ((format.Justify) && (result.Length != format.Width) && (currentIndex + count < words.Length)) // need to Align Left and Right { var deficit = (format.Width - result.Length); var resBuilder =new StringBuilder(); int step = (deficit / count) + 1; int extraDeficit = deficit - (step -1)* (count - 1); var stepString = new string(' ', step); for (int i = currentIndex; i < currentIndex+count; i++) { if (resBuilder.Length != 0) { resBuilder.Append(stepString); if (extraDeficit > 0) { extraDeficit--; resBuilder.Append(" "); } } else { resBuilder.Append(format.SpaceLeft); // add left margin } resBuilder.Append(words[i]); } result = resBuilder.ToString(); } currentIndex += count-1; return result; }