From 77d6c3f1a673e57306456d377a2f1bfb202a076d Mon Sep 17 00:00:00 2001 From: Sean McArdle Date: Sat, 11 Nov 2023 17:16:51 -0800 Subject: [PATCH] Pulled in my silly object dumping function. --- RulerDev/MyExtensions.cs | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/RulerDev/MyExtensions.cs b/RulerDev/MyExtensions.cs index 8b5e92f..fbf936f 100644 --- a/RulerDev/MyExtensions.cs +++ b/RulerDev/MyExtensions.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Linq; using System.Collections.Generic; using System.Linq.Expressions; +using System.Text; using ConsoleTables; using McRule; using Newtonsoft.Json; @@ -49,12 +50,45 @@ public static class MyExtensions { table.Write(Format.Minimal); } else { - Console.WriteLine(JsonConvert.SerializeObject(thing, Formatting.Indented)); + Console.WriteLine(ToKvText(thing)); } end: return thing; } + + public static string ToKvText(this object entry) { + if (entry == null) return string.Empty; + + StringBuilder sb = new StringBuilder(); + + var properties = entry.GetType().GetProperties(); + var longestPropertyLength = properties.Select(x => x.Name.Length).Max(); + int indentation = longestPropertyLength + 3; + + foreach (var p in properties) { + sb.Append(p.Name.PadRight(longestPropertyLength)); + sb.Append(" : "); + bool multiLine = false; + var thing = p.GetValue(entry); + if (thing is string) { + foreach (var value in (thing ?? "NULL").ToString().Split("\n")) { + var strValue = FormatProperty((value) ?? "NULL"); + if (multiLine) { + sb.AppendLine((strValue).PadLeft(indentation + strValue.Length)); + } else { + sb.AppendLine((strValue).PadRight(longestPropertyLength)); + multiLine = true; + } + } + } else { + var value = FormatProperty(thing); + sb.AppendLine((value).PadLeft(value.Length)); + } + } + + return sb.ToString(); + } /// /// Format properties for being printed inside a table. For array objects with fewer than 4 @@ -70,18 +104,18 @@ public static class MyExtensions { if (Property is String s) return s; if (Property.GetType().GetInterface("IEnumerable") != null) { - if (Property.Length < 4) { + if (Enumerable.Count(Property) <= 5) { return $"[{String.Join(", ", Property)}]"; } else { var elems = new List(); var count = 0; foreach (var el in Property) { count++; - elems.Add(el); + elems.Add(el.ToString()); if (count >= 3) break; } - return $"[{String.Join(", ", elems)}...({Property.Length})]"; + return $"[{String.Join(", ", elems)}...({Enumerable.Count(Property)})]"; } }