Merge branch 'dev'

dev
Sean McArde 2023-12-11 16:10:41 -08:00
commit 1135eea353
1 changed files with 38 additions and 4 deletions

View File

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