From 09f08a90319cbbdce4dac243d2ed0f9add93fcd1 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Fri, 10 Jul 2026 09:04:55 -0400 Subject: [PATCH] Clean up formatting to personal taste --- .../CommandSetTests.cs | 14 ++++++-- .../Features/ManpageTests.cs | 17 ++++++---- SabreTools.CommandLine/CommandSet.cs | 12 ++++--- SabreTools.CommandLine/Inputs/UserInput.cs | 29 ++++++++-------- SabreTools.CommandLine/{ => Tools}/Roff.cs | 34 ++++++++++--------- 5 files changed, 61 insertions(+), 45 deletions(-) rename SabreTools.CommandLine/{ => Tools}/Roff.cs (84%) diff --git a/SabreTools.CommandLine.Test/CommandSetTests.cs b/SabreTools.CommandLine.Test/CommandSetTests.cs index cafe044..99664ad 100644 --- a/SabreTools.CommandLine.Test/CommandSetTests.cs +++ b/SabreTools.CommandLine.Test/CommandSetTests.cs @@ -846,7 +846,11 @@ namespace SabreTools.CommandLine.Test public void OutputManpage_Structure_ContainsRequiredSections() { var set = BuildSampleSet(); - var info = new ManpageInfo("sample") { Version = "sample 1.0", Description = "do sample things" }; + var info = new ManpageInfo("sample") + { + Version = "sample 1.0", + Description = "do sample things", + }; string page = set.OutputManpage(info); @@ -965,11 +969,15 @@ namespace SabreTools.CommandLine.Test { var set = new CommandSet("Sample header line"); - var help = new MockFeature("Help", ["?", "h", "help"], "Show this help", + var help = new MockFeature("Help", + ["?", "h", "help"], + "Show this help", "Built-in to most of the programs is a basic help text."); set.Add(help); - var convert = new MockFeature("Convert", "--convert", "Convert input files", + var convert = new MockFeature("Convert", + "--convert", + "Convert input files", "Converts the provided input files into the desired output format."); convert.Add(new StringInput("output", "--output", "Set the output path")); convert.Add(new FlagInput("force", "--force", "Overwrite existing files")); diff --git a/SabreTools.CommandLine.Test/Features/ManpageTests.cs b/SabreTools.CommandLine.Test/Features/ManpageTests.cs index 169f67c..f3aaec9 100644 --- a/SabreTools.CommandLine.Test/Features/ManpageTests.cs +++ b/SabreTools.CommandLine.Test/Features/ManpageTests.cs @@ -17,7 +17,6 @@ namespace SabreTools.CommandLine.Test.Features var original = Console.Out; var writer = new StringWriter(); - string output; try { Console.SetOut(writer); @@ -29,10 +28,10 @@ namespace SabreTools.CommandLine.Test.Features Console.SetOut(original); } - output = writer.ToString(); - Assert.Contains(".TH \"SAMPLE\" \"1\"", output); - Assert.Contains(".SH NAME", output); - Assert.Contains(".B \\-\\-convert", output); + string actual = writer.ToString(); + Assert.Contains(".TH \"SAMPLE\" \"1\"", actual); + Assert.Contains(".SH NAME", actual); + Assert.Contains(".B \\-\\-convert", actual); } /// @@ -42,11 +41,15 @@ namespace SabreTools.CommandLine.Test.Features { var set = new CommandSet("Sample header line"); - var help = new MockFeature("Help", ["?", "h", "help"], "Show this help", + var help = new MockFeature("Help", + ["?", "h", "help"], + "Show this help", "Built-in to most of the programs is a basic help text."); set.Add(help); - var convert = new MockFeature("Convert", "--convert", "Convert input files", + var convert = new MockFeature("Convert", + "--convert", + "Convert input files", "Converts the provided input files into the desired output format."); convert.Add(new StringInput("output", "--output", "Set the output path")); convert.Add(new FlagInput("force", "--force", "Overwrite existing files")); diff --git a/SabreTools.CommandLine/CommandSet.cs b/SabreTools.CommandLine/CommandSet.cs index 52c4cd3..ec787b3 100644 --- a/SabreTools.CommandLine/CommandSet.cs +++ b/SabreTools.CommandLine/CommandSet.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using SabreTools.CommandLine.Features; using SabreTools.CommandLine.Inputs; +using SabreTools.CommandLine.Tools; namespace SabreTools.CommandLine { @@ -991,12 +992,13 @@ namespace SabreTools.CommandLine output.Add(".SH NAME"); string nameLine = Roff.Escape(info.Name); if (!string.IsNullOrEmpty(info.Description)) - nameLine += " \\- " + Roff.Escape(info.Description); + nameLine += $" \\- {Roff.Escape(info.Description)}"; + output.Add(nameLine); // Synopsis section output.Add(".SH SYNOPSIS"); - output.Add(".B " + Roff.Escape(info.Name)); + output.Add(item: $".B {Roff.Escape(info.Name)}"); output.Add("[options]"); // Description section, derived from the header lines @@ -1010,7 +1012,7 @@ namespace SabreTools.CommandLine } // Options section, derived from all available inputs - output.Add(".SH " + info.OptionsHeading); + output.Add($".SH {info.OptionsHeading}"); foreach (var input in _inputs.Values) { output.AddRange(input.FormatManpage(includeVerbose)); @@ -1036,7 +1038,7 @@ namespace SabreTools.CommandLine } // Join the lines with a trailing newline for a well-formed file - return string.Join("\n", output.ToArray()) + "\n"; + return string.Join("\n", [.. output]) + "\n"; } /// @@ -1073,7 +1075,7 @@ namespace SabreTools.CommandLine /// Value to quote, if any /// The escaped, quoted value private static string Quote(string? value) - => "\"" + Roff.EscapeField(value) + "\""; + => $"\"{Roff.EscapeField(value)}\""; #endregion diff --git a/SabreTools.CommandLine/Inputs/UserInput.cs b/SabreTools.CommandLine/Inputs/UserInput.cs index 0426fa9..1413018 100644 --- a/SabreTools.CommandLine/Inputs/UserInput.cs +++ b/SabreTools.CommandLine/Inputs/UserInput.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using SabreTools.CommandLine.Tools; namespace SabreTools.CommandLine.Inputs { @@ -717,6 +718,11 @@ namespace SabreTools.CommandLine.Inputs public List FormatRecursive(int pre, int midpoint, bool detailed = false) => FormatRecursive(tabLevel: 0, pre, midpoint, detailed); + /// + /// Pre-format the flags for output + /// + protected abstract string FormatFlags(); + /// /// Create roff man page entries for this input and all of its children /// @@ -758,20 +764,6 @@ namespace SabreTools.CommandLine.Inputs return output; } - /// - /// Pre-format the flags for output - /// - protected abstract string FormatFlags(); - - /// - /// Create a padding space based on the given length - /// - /// Number of padding spaces to add - /// String with requested number of blank spaces - private static string CreatePadding(int spaces) => spaces > 0 - ? string.Empty.PadRight(spaces) - : string.Empty; - /// /// Format the standard help output line /// @@ -880,6 +872,15 @@ namespace SabreTools.CommandLine.Inputs return outputList; } + /// + /// Create a padding space based on the given length + /// + /// Number of padding spaces to add + /// String with requested number of blank spaces + private static string CreatePadding(int spaces) => spaces > 0 + ? string.Empty.PadRight(spaces) + : string.Empty; + /// /// Create formatted help text including all children /// diff --git a/SabreTools.CommandLine/Roff.cs b/SabreTools.CommandLine/Tools/Roff.cs similarity index 84% rename from SabreTools.CommandLine/Roff.cs rename to SabreTools.CommandLine/Tools/Roff.cs index de55afc..7944407 100644 --- a/SabreTools.CommandLine/Roff.cs +++ b/SabreTools.CommandLine/Tools/Roff.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; using System.Text; -namespace SabreTools.CommandLine +namespace SabreTools.CommandLine.Tools { /// /// Helpers for emitting roff-formatted man page text @@ -55,7 +55,8 @@ namespace SabreTools.CommandLine if (string.IsNullOrEmpty(value)) return string.Empty; - return value!.Replace("\\", "\\e").Replace("\"", "\\(dq"); + return value!.Replace("\\", "\\e") + .Replace("\"", "\\(dq"); } /// @@ -71,7 +72,7 @@ namespace SabreTools.CommandLine // Normalize line endings and split into paragraphs on blank lines string normalized = text!.Replace("\r\n", "\n").Replace("\r", "\n"); - string[] paragraphs = normalized.Split(new string[] { "\n\n" }, StringSplitOptions.None); + string[] paragraphs = normalized.Split(["\n\n"], StringSplitOptions.None); for (int i = 0; i < paragraphs.Length; i++) { @@ -84,7 +85,8 @@ namespace SabreTools.CommandLine if (output.Count > 0) output.Add(".sp"); - output.AddRange(WrapParagraph(paragraph)); + List wrapped = WrapParagraph(paragraph); + output.AddRange(wrapped); } return output; @@ -100,32 +102,32 @@ namespace SabreTools.CommandLine List lines = []; string[] words = paragraph.Split(' '); - var current = new StringBuilder(); + var sb = new StringBuilder(); for (int i = 0; i < words.Length; i++) { string word = Escape(words[i]); if (word.Length == 0) continue; - if (current.Length == 0) + if (sb.Length == 0) { - current.Append(word); + sb.Append(word); } - else if (current.Length + 1 + word.Length <= LineWidth) + else if (sb.Length + 1 + word.Length <= LineWidth) { - current.Append(' '); - current.Append(word); + sb.Append(' '); + sb.Append(word); } else { - lines.Add(Protect(current.ToString())); - current = new StringBuilder(); - current.Append(word); + lines.Add(Protect(sb.ToString())); + sb = new StringBuilder(); + sb.Append(word); } } - if (current.Length > 0) - lines.Add(Protect(current.ToString())); + if (sb.Length > 0) + lines.Add(Protect(sb.ToString())); return lines; } @@ -140,7 +142,7 @@ namespace SabreTools.CommandLine // A line beginning with a control character would be treated as a // request; a leading zero-width escape forces it to be text if (line.Length > 0 && (line[0] == '.' || line[0] == '\'')) - return "\\&" + line; + return $"\\&{line}"; return line; }