public static string TextDump(object target, TextDumpOptions options) { if (options == null) { options = new TextDumpOptions(); } var depth = options.Depth; options.Depth += 1; try { if (!isComplexType(target)) { return(GetScalarText(target, options.Defaults)); } var headerStyle = options.HeaderStyle; if (target is IEnumerable e) { var objs = e.Map(x => x); var isEmpty = objs.Count == 0; if (isEmpty) { return(options.CaptionIfEmpty ?? string.Empty); } var first = objs[0]; if (first is IDictionary && objs.Count > 1) { return(TextList(objs, options)); } var sb = StringBuilderCacheAlt.Allocate(); string writeCaption = null; var caption = options.Caption; if (caption != null && !options.HasCaption) { writeCaption = caption; options.HasCaption = true; } if (!isEmpty) { var keys = new List <string>(); var values = new List <string>(); string TextKvps(StringBuilder s, IEnumerable <KeyValuePair <string, object> > kvps) { foreach (var kvp in kvps) { if (kvp.Value == target) { break; // Prevent cyclical deps like 'it' binding } keys.Add(ViewUtils.StyleText(kvp.Key, headerStyle) ?? ""); var field = !isComplexType(kvp.Value) ? GetScalarText(kvp.Value, options.Defaults) : TextDump(kvp.Value, options); values.Add(field); } var keySize = keys.Max(x => x.Length); var valuesSize = values.Max(x => x.Length); s.AppendLine(writeCaption != null ? $"| {writeCaption.PadRight(keySize + valuesSize + 2, ' ')} ||" : $"|||"); s.AppendLine(writeCaption != null ? $"|-{"".PadRight(keySize, '-')}-|-{"".PadRight(valuesSize, '-')}-|" : "|-|-|"); for (var i = 0; i < keys.Count; i++) { s.Append("| ") .Append(keys[i].PadRight(keySize, ' ')) .Append(" | ") .Append(values[i].PadRight(valuesSize, ' ')) .Append(" |") .AppendLine(); } return(StringBuilderCache.ReturnAndFree(s)); } if (first is KeyValuePair <string, object> ) { return(TextKvps(sb, objs.Cast <KeyValuePair <string, object> >())); } else { if (!isComplexType(first)) { foreach (var o in objs) { values.Add(GetScalarText(o, options.Defaults)); } var valuesSize = values.Max(MaxLineLength); if (writeCaption?.Length > valuesSize) { valuesSize = writeCaption.Length; } sb.AppendLine(writeCaption != null ? $"| {writeCaption.PadRight(valuesSize)} |" : $"||"); sb.AppendLine(writeCaption != null ? $"|-{"".PadRight(valuesSize,'-')}-|" : "|-|"); foreach (var value in values) { sb.Append("| ") .Append(value.PadRight(valuesSize, ' ')) .Append(" |") .AppendLine(); } } else { if (objs.Count > 1) { if (writeCaption != null) { sb.AppendLine(writeCaption) .AppendLine(); } var rows = objs.Map(x => x.ToObjectDictionary()); var list = TextList(rows, options); sb.AppendLine(list); return(StringBuilderCache.ReturnAndFree(sb)); } else { foreach (var o in objs) { if (!isComplexType(o)) { values.Add(GetScalarText(o, options.Defaults)); } else { var body = TextDump(o, options); values.Add(body); } } var valuesSize = values.Max(MaxLineLength); if (writeCaption?.Length > valuesSize) { valuesSize = writeCaption.Length; } sb.AppendLine(writeCaption != null ? $"| {writeCaption.PadRight(valuesSize, ' ')} |" : $"||"); sb.AppendLine(writeCaption != null ? $"|-{"".PadRight(valuesSize,'-')}-|" : "|-|"); foreach (var value in values) { sb.Append("| ") .Append(value.PadRight(valuesSize, ' ')) .Append(" |") .AppendLine(); } } } } } return(StringBuilderCache.ReturnAndFree(sb)); } return(TextDump(target.ToObjectDictionary(), options)); } finally { options.Depth = depth; } }
public static string TextList(IEnumerable items, TextDumpOptions options) { if (options == null) { options = new TextDumpOptions(); } if (items is IDictionary <string, object> single) { items = new[] { single } } ; var depth = options.Depth; options.Depth += 1; try { var headerStyle = options.HeaderStyle; List <string> keys = null; var table = new MarkdownTable { IncludeRowNumbers = options.IncludeRowNumbers }; foreach (var item in items) { if (item is IDictionary <string, object> d) { if (keys == null) { keys = d.Keys.ToList(); foreach (var key in keys) { table.Headers.Add(ViewUtils.StyleText(key, headerStyle)); } } var row = new List <string>(); foreach (var key in keys) { var value = d[key]; if (ReferenceEquals(value, items)) { break; // Prevent cyclical deps like 'it' binding } if (!isComplexType(value)) { row.Add(GetScalarText(value, options.Defaults)); } else { var cellValue = TextDump(value, options); row.Add(cellValue); } } table.Rows.Add(row); } } var isEmpty = table.Rows.Count == 0; if (isEmpty) { return(options.CaptionIfEmpty ?? string.Empty); } var caption = options.Caption; if (caption != null && !options.HasCaption) { table.Caption = caption; options.HasCaption = true; } var txt = table.Render(); return(txt); } finally { options.Depth = depth; } }
public static string TextDump(this IHtmlHelper html, object target, TextDumpOptions options) => target.TextDump(options);
public IRawString textDump(object target, Dictionary <string, object> options) => TextDump(target, TextDumpOptions.Parse(options, Context.DefaultMethods)).ToRawString();
/// <summary> /// Dump object in Ascii Markdown table /// </summary> public static string dumpTable(object instance, TextDumpOptions options) => DefaultScripts.TextDump(instance, options);