Clean up formatting to personal taste

This commit is contained in:
Matt Nadareski
2026-07-10 09:04:55 -04:00
parent 8695e519c6
commit 09f08a9031
5 changed files with 61 additions and 45 deletions

View File

@@ -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"));

View File

@@ -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);
}
/// <summary>
@@ -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"));

View File

@@ -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";
}
/// <summary>
@@ -1073,7 +1075,7 @@ namespace SabreTools.CommandLine
/// <param name="value">Value to quote, if any</param>
/// <returns>The escaped, quoted value</returns>
private static string Quote(string? value)
=> "\"" + Roff.EscapeField(value) + "\"";
=> $"\"{Roff.EscapeField(value)}\"";
#endregion

View File

@@ -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<string> FormatRecursive(int pre, int midpoint, bool detailed = false)
=> FormatRecursive(tabLevel: 0, pre, midpoint, detailed);
/// <summary>
/// Pre-format the flags for output
/// </summary>
protected abstract string FormatFlags();
/// <summary>
/// Create roff man page entries for this input and all of its children
/// </summary>
@@ -758,20 +764,6 @@ namespace SabreTools.CommandLine.Inputs
return output;
}
/// <summary>
/// Pre-format the flags for output
/// </summary>
protected abstract string FormatFlags();
/// <summary>
/// Create a padding space based on the given length
/// </summary>
/// <param name="spaces">Number of padding spaces to add</param>
/// <returns>String with requested number of blank spaces</returns>
private static string CreatePadding(int spaces) => spaces > 0
? string.Empty.PadRight(spaces)
: string.Empty;
/// <summary>
/// Format the standard help output line
/// </summary>
@@ -880,6 +872,15 @@ namespace SabreTools.CommandLine.Inputs
return outputList;
}
/// <summary>
/// Create a padding space based on the given length
/// </summary>
/// <param name="spaces">Number of padding spaces to add</param>
/// <returns>String with requested number of blank spaces</returns>
private static string CreatePadding(int spaces) => spaces > 0
? string.Empty.PadRight(spaces)
: string.Empty;
/// <summary>
/// Create formatted help text including all children
/// </summary>

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Text;
namespace SabreTools.CommandLine
namespace SabreTools.CommandLine.Tools
{
/// <summary>
/// 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");
}
/// <summary>
@@ -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<string> wrapped = WrapParagraph(paragraph);
output.AddRange(wrapped);
}
return output;
@@ -100,32 +102,32 @@ namespace SabreTools.CommandLine
List<string> 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;
}