Files
ScadaBridge/src/ScadaLink.CLI/OutputFormatter.cs
T
Joseph Doherty 1eb6e972b0 docs: add XML doc comments across src + Sister Projects section in CLAUDE.md
Bulk CommentChecker pass: fills in <param>/<inheritdoc> tags on public
APIs across all 23 src/ projects so the doc-coverage gate is green. Also
adds a Sister Projects section to CLAUDE.md pointing at the MxAccess
Gateway and OtOpcUa sibling repos, and gitignores local credential
captures (*login*.txt) and the wonder-app-vd03 deploy/ artifacts.
2026-05-28 01:55:24 -04:00

50 lines
2.1 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace ScadaLink.CLI;
public static class OutputFormatter
{
private static readonly JsonSerializerOptions JsonOptions = new()
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
/// <summary>Serializes <paramref name="data"/> to indented JSON and writes it to standard output.</summary>
/// <param name="data">The object to serialize; <c>null</c> is serialized as JSON <c>null</c>.</param>
public static void WriteJson(object? data)
{
Console.WriteLine(JsonSerializer.Serialize(data, JsonOptions));
}
/// <summary>Writes a JSON error envelope with the given message and code to standard error.</summary>
/// <param name="message">Human-readable error description.</param>
/// <param name="code">Machine-readable error code.</param>
public static void WriteError(string message, string code)
{
Console.Error.WriteLine(JsonSerializer.Serialize(new { error = message, code }, JsonOptions));
}
/// <summary>Writes a plain-text padded table to standard output with the given column headers and data rows.</summary>
/// <param name="rows">Data rows; each inner array corresponds to a column in the same order as <paramref name="headers"/>.</param>
/// <param name="headers">Column header labels.</param>
public static void WriteTable(IEnumerable<string[]> rows, string[] headers)
{
var allRows = new List<string[]> { headers };
allRows.AddRange(rows);
var widths = new int[headers.Length];
foreach (var row in allRows)
for (int i = 0; i < Math.Min(row.Length, widths.Length); i++)
widths[i] = Math.Max(widths[i], (row[i] ?? "").Length);
foreach (var row in allRows)
{
for (int i = 0; i < headers.Length; i++)
Console.Write((i < row.Length ? row[i] ?? "" : "").PadRight(widths[i] + 2));
Console.WriteLine();
}
}
}