From 69d263ca2acb7c1c458e2f3b98a12df3f3a36efe Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 5 Oct 2025 11:14:36 -0400 Subject: [PATCH] Simplify long description printing, add test --- SabreTools.CommandLine.Test/FeatureTests.cs | 23 +++++++- SabreTools.CommandLine/Inputs/UserInput.cs | 63 +++++++-------------- 2 files changed, 42 insertions(+), 44 deletions(-) diff --git a/SabreTools.CommandLine.Test/FeatureTests.cs b/SabreTools.CommandLine.Test/FeatureTests.cs index de8d2b1..6adfe98 100644 --- a/SabreTools.CommandLine.Test/FeatureTests.cs +++ b/SabreTools.CommandLine.Test/FeatureTests.cs @@ -1,4 +1,5 @@ -using Xunit; +using System.Net.NetworkInformation; +using Xunit; namespace SabreTools.CommandLine.Test { @@ -90,6 +91,26 @@ namespace SabreTools.CommandLine.Test Assert.Empty(feature.Inputs); } + [Fact] + public void FormatLongDescriptionTest() + { + var feature = new MockFeature("a", "a", "a", "Some long description that is normal"); + var o = feature.FormatLongDescription(pre: 0); + Assert.Equal(2, o.Count); + + feature = new MockFeature("a", "a", "a", "Some long description\nwith a newline"); + o = feature.FormatLongDescription(pre: 0); + Assert.Equal(3, o.Count); + + feature = new MockFeature("a", "a", "a", "Some long description\nwith\nmultiple\nnewlines"); + o = feature.FormatLongDescription(pre: 0); + Assert.Equal(5, o.Count); + + feature = new MockFeature("a", "a", "a", "Some long description\n - With formatting"); + o = feature.FormatLongDescription(pre: 0); + Assert.Equal(3, o.Count); + } + /// /// Mock Feature implementation for testing /// diff --git a/SabreTools.CommandLine/Inputs/UserInput.cs b/SabreTools.CommandLine/Inputs/UserInput.cs index 9222e71..2179f36 100644 --- a/SabreTools.CommandLine/Inputs/UserInput.cs +++ b/SabreTools.CommandLine/Inputs/UserInput.cs @@ -637,7 +637,7 @@ namespace SabreTools.CommandLine.Inputs // Add the long description, if needed if (detailed) - outputList.AddRange(FormatLongDescription(pre, midpoint)); + outputList.AddRange(FormatLongDescription(pre)); return outputList; } @@ -701,9 +701,8 @@ namespace SabreTools.CommandLine.Inputs /// Format the long description help output lines /// /// Positive number representing number of spaces to put in front of the feature - /// Positive number representing the column where the description should start /// Pre-split output lines - private List FormatLongDescription(int pre, int midpoint) + internal List FormatLongDescription(int pre) { // If the long description is null or empty if (string.IsNullOrEmpty(_longDescription)) @@ -711,6 +710,7 @@ namespace SabreTools.CommandLine.Inputs // Normalize the description for output string longDescription = _longDescription!.Replace("\r\n", "\n"); + longDescription = longDescription.Replace("\n", "\n" + CreatePadding(pre + 4)); // Get the width of the console for wrapping reference int width = (Console.WindowWidth == 0 ? 80 : Console.WindowWidth) - 1; @@ -728,45 +728,11 @@ namespace SabreTools.CommandLine.Inputs string segment = split[i]; // If we have a newline character, reset the line and continue - if (segment.Contains("\n")) + bool forceSplit = false; + if (segment.EndsWith("\n")) { - string[] subsplit = segment.Split('\n'); - for (int j = 0; j < subsplit.Length - 1; j++) - { - // Cache the current segment - string subSegment = subsplit[j]; - - // Add the next word only if the total length doesn't go above the width of the screen - if (output.Length + subSegment.Length < width) - { - output.Append(output.Length == pre + 4 ? string.Empty : " "); - output.Append(subSegment); - } - // Otherwise, we want to cache the line to output and create a new blank string - else - { - outputList.Add(output.ToString()); -#if NET20 || NET35 - output = new(); -#else - output.Clear(); -#endif - output.Append(CreatePadding(pre + 4)); - output.Append(output.Length == pre + 4 ? string.Empty : " "); - output.Append(subSegment); - } - - outputList.Add(output.ToString()); -#if NET20 || NET35 - output = new(); -#else - output.Clear(); -#endif - output.Append(CreatePadding(pre + 4)); - } - - output.Append(subsplit[subsplit.Length - 1]); - continue; + forceSplit = true; + segment = segment.TrimEnd('\n'); } // Add the next word only if the total length doesn't go above the width of the screen @@ -785,9 +751,20 @@ namespace SabreTools.CommandLine.Inputs output.Clear(); #endif output.Append(CreatePadding(pre + 4)); - output.Append(output.Length == pre + 4 ? string.Empty : " "); output.Append(segment); } + + // If force splitting, append and clear + if (forceSplit) + { + outputList.Add(output.ToString()); +#if NET20 || NET35 + output = new(); +#else + output.Clear(); +#endif + output.Append(CreatePadding(pre + 4)); + } } // Add the last created output and a blank line for clarity @@ -824,7 +801,7 @@ namespace SabreTools.CommandLine.Inputs // Add the long description, if needed if (detailed) - outputList.AddRange(FormatLongDescription(preAdjusted, midpointAdjusted)); + outputList.AddRange(FormatLongDescription(preAdjusted)); // Append all children recursively foreach (var feature in Children.Values)