Skip to content

Commit

Permalink
1. Supporting labels in almost all cases
Browse files Browse the repository at this point in the history
2. Improvements for System.Reflection type renderring
  • Loading branch information
MoaidHathot committed Jul 15, 2023
1 parent 10604d5 commit ce33e8d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 33 deletions.
46 changes: 37 additions & 9 deletions src/Dumpify.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,55 @@
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using Color = System.Drawing.Color;

//DumpConfig.Default.Renderer = Renderers.Text;
//DumpConfig.Default.ColorConfig = ColorConfig.NoColors;

//DumpConfig.Default.Output = Outputs.Debug;

Console.WriteLine("---------------------");
//TestSpecific();
TestObjectWithLargeWidth();
TestSpecific();
//TestObjectWithLargeWidth();
//TestSingle();
// ShowEverything();


//todo: improve labels, make them work with simple objects as strings (not wrapped in other object) and consider changing colors


#pragma warning disable CS8321
#pragma warning disable CS0168
void TestSpecific()
{
try
{
throw new Exception("Bla bla", new ArgumentNullException("paramName", "inner bla fla"));
}
catch (Exception e)
//try
//{
// throw new Exception("Bla bla", new ArgumentNullException("paramName", "inner bla fla"));
//}
//catch (Exception e)
//{
// e.Dump(maxDepth: 1, label: "Test Ex", colors: new ColorConfig { LabelValueColor = Color.DarkOrange });
//}

//Enumerable.Range(1, 3).Dump("This is Enumerable", colors: new ColorConfig { LabelValueColor = Color.Orange });
//Enumerable.Range(1, 3).ToArray().Dump("This is Array", colors: new ColorConfig { LabelValueColor = Color.Orange });
//Enumerable.Range(1, 3).ToList().Dump("This is List", colors: new ColorConfig { LabelValueColor = Color.Orange });
//1.Dump("This is one", colors: new ColorConfig { LabelValueColor = Color.Fuchsia });
//"Moaid".Dump("this is my name");
//Guid.NewGuid().Dump("This is Guid", colors: new ColorConfig { LabelValueColor = Color.SlateBlue });
new
{
e.Dump();
}
Property = typeof(Person).GetProperty("FirstName"),
Ctor = typeof(Person).GetConstructors().First(),
Type = typeof(Person),
Field = typeof(Person).GetFields().First(),
Method = typeof(Person).GetMethods().First(),
}.Dump("This is a test");

typeof(Person).Dump("This is a type");
}

void TestObjectWithLargeWidth()
Expand Down Expand Up @@ -323,6 +347,10 @@ public record class Person
public Person? Spouse { get; set; }

public Profession Profession { get; set; }
public string? _fooField;

public string? FooMethod(int a)
=> "";
}

public class Family
Expand Down
3 changes: 3 additions & 0 deletions src/Dumpify/Config/ColorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class ColorConfig : IColorConfig<DumpColor>
public static DumpColor? DefaultIgnoredValueColor { get; } = null;
public static DumpColor? DefaultMetadataInfoColor { get; } = new ("#87D7D7");
public static DumpColor? DefaultMetadataErrorColor { get; } = new ("#D78700");
public static DumpColor? DefaultLabelValueColor { get; } = new ("#87D7D7");

public DumpColor? TypeNameColor { get; set; } = DefaultTypeNameColor;
public DumpColor? ColumnNameColor { get; set; } = DefaultColumnNameColor;
Expand All @@ -21,6 +22,7 @@ public class ColorConfig : IColorConfig<DumpColor>
public DumpColor? IgnoredValueColor { get; set; } = DefaultIgnoredValueColor;
public DumpColor? MetadataInfoColor { get; set; } = DefaultMetadataInfoColor;
public DumpColor? MetadataErrorColor { get; set; } = DefaultMetadataErrorColor;
public DumpColor? LabelValueColor { get; set; } = DefaultLabelValueColor;

public static ColorConfig DefaultColors => new ();
public static ColorConfig NoColors => new (null);
Expand All @@ -35,6 +37,7 @@ public ColorConfig(DumpColor? value)
IgnoredValueColor = value;
MetadataInfoColor = value;
MetadataErrorColor = value;
LabelValueColor = value;
}

public ColorConfig()
Expand Down
9 changes: 2 additions & 7 deletions src/Dumpify/Config/IColorConfig.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dumpify;
namespace Dumpify;

public interface IColorConfig<TColor>
{
Expand All @@ -16,4 +10,5 @@ public interface IColorConfig<TColor>
TColor? IgnoredValueColor { get; set; }
TColor? MetadataInfoColor { get; set; }
TColor? MetadataErrorColor { get; set; }
TColor? LabelValueColor { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public CustomValuesGenerator(ConcurrentDictionary<RuntimeTypeHandle, Func<object

if ((type.Namespace?.StartsWith("System.Reflection") ?? false) && !type.IsArray)
{
_customTypeHandlers.TryAdd(type.TypeHandle, (obj, t, valProvider, memProvider) => t.Name.ToString());
return new CustomDescriptor(type, valueProvider);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class ObjectTableBuilder
private readonly IDescriptor _descriptor;
private readonly object _sourceObject;

private List<ITableBuilderBehavior> _behaviors = new();
private readonly List<ITableBuilderBehavior> _behaviors = new();

private readonly List<IEnumerable<IRenderable>> _rows = new();
private readonly List<IRenderable> _columnNames = new(2);
Expand Down Expand Up @@ -64,7 +64,7 @@ private Style GetColumnNameStyle()
public ObjectTableBuilder SetColumnNames(params string[] columnNames)
=> SetColumnNames((IEnumerable<string>)columnNames);

public ObjectTableBuilder SetTitle(string? title, Style? style = null)
public ObjectTableBuilder SetTitle(string? title, Style? style)
{
_title = title switch
{
Expand Down Expand Up @@ -168,7 +168,7 @@ private Table CreateTable()

if (_context.Config.Label is { } label && _context.CurrentDepth == 0 && object.ReferenceEquals(_context.RootObject, _sourceObject))
{
table.Caption = new TableTitle(Markup.Escape(label));
table.Caption = new TableTitle(Markup.Escape(label), new Style(foreground: _context.State.Colors.LabelValueColor));
}

var columns = GetBehaviorColumns().Concat(_columnNames);
Expand Down
3 changes: 1 addition & 2 deletions src/Dumpify/Renderers/Spectre.Console/SpectreColorConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

using Dumpify;
using Spectre.Console;

namespace Dumpify;
Expand All @@ -14,4 +12,5 @@ public class SpectreColorConfig : IColorConfig<Color?>
public required Color? IgnoredValueColor { get; set; }
public required Color? MetadataInfoColor { get; set; }
public required Color? MetadataErrorColor { get; set; }
public required Color? LabelValueColor { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ protected virtual IRenderable RenderSingleValue(object value, RenderContext<Spec
=> new Markup(Markup.Escape(value.ToString() ?? ""), new Style(foreground: color));

protected override IRenderable RenderSingleValueDescriptor(object obj, SingleValueDescriptor descriptor, RenderContext<SpectreRendererState> context)
=> RenderSingleValue(obj is string str ? $"\"{str}\"" : obj, context, context.State.Colors.PropertyValueColor);
{
var singleValue = RenderSingleValue(obj is string str ? $"\"{str}\"" : obj, context, context.State.Colors.PropertyValueColor);

if (context.Config.Label is { } label && context.CurrentDepth == 0 && object.ReferenceEquals(context.RootObject, obj))
{
var builder = new ObjectTableBuilder(context, descriptor, obj);
return builder
.AddColumnName("Value")
.AddRow(descriptor, obj, singleValue)
.HideTitle()
.HideHeaders()
.Build();
}

return singleValue;
}

public override IRenderable RenderNullValue(IDescriptor? descriptor, RenderContext<SpectreRendererState> context)
=> RenderSingleValue("null", context, context.State.Colors.NullValueColor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@

using Dumpify;
using Spectre.Console;

namespace Dumpify;
Expand All @@ -20,6 +18,7 @@ public SpectreRendererState(RendererConfig config)
PropertyNameColor = config.ColorConfig.PropertyNameColor.ToSpectreColor(),
MetadataErrorColor = config.ColorConfig.MetadataErrorColor.ToSpectreColor(),
PropertyValueColor = config.ColorConfig.PropertyValueColor.ToSpectreColor(),
LabelValueColor = config.ColorConfig.LabelValueColor.ToSpectreColor(),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
using Dumpify.Descriptors;
using Spectre.Console;
using Spectre.Console.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Dumpiyf;

Expand All @@ -23,9 +18,48 @@ public SystemReflectionTypeRenderer(IRendererHandler<IRenderable, SpectreRendere
public IRenderable Render(IDescriptor descriptor, object obj, RenderContext context, object? handleContext)
{
var typeColor = context.Config.ColorConfig.TypeNameColor?.HexString ?? "default";
var typeName = context.Config.TypeNameProvider.GetTypeName(obj.GetType());
var c = $"[[[{typeColor}]{Markup.Escape(typeName)}[/]]]";
return new Markup(c);
var metadataColor = context.Config.ColorConfig.MetadataInfoColor?.HexString ?? "default";
var propertyValueColor = context.Config.ColorConfig.PropertyValueColor?.HexString ?? "default";

if (obj is PropertyInfo property)
{
var typeName = context.Config.TypeNameProvider.GetTypeName(property.PropertyType).EscapeMarkup();
var propertyDescription = $"[{typeColor}]{typeName}[/] [{propertyValueColor}]{property.Name.EscapeMarkup()}[/] {{ {(property.SetMethod is not null ? $"[{metadataColor}]set[/]; " : "")}{(property.GetMethod is not null ? $"[{metadataColor}]get[/]; " : "")}}}";
return new Markup(propertyDescription);
//return GeneralMarkup(propertyDescription, context);
}

if (obj is ConstructorInfo ctor)
{
var ctorDescription = $"Constructor {ctor.Name}({string.Join(", ", ctor.GetParameters().Select(p => $"{p.ParameterType} {p.Name}"))})".EscapeMarkup();
return GeneralMarkup(ctorDescription, context);
}

if (obj is MethodInfo method)
{

var methodDescription = $"{method.ReturnType.Name} {method.Name}({string.Join(", ", method.GetParameters().Select(p => $"{p.ParameterType} {p.Name}"))})";
return GeneralMarkup(methodDescription, context);
}

if (obj is FieldInfo field)
{
var typeName = context.Config.TypeNameProvider.GetTypeName(field.FieldType).EscapeMarkup();
var fieldDescription = $"[{typeColor}]{typeName}[/] [{propertyValueColor}]{field.Name.EscapeMarkup()}[/];";
return new Markup(fieldDescription);
}

var formattedTypeName = context.Config.TypeNameProvider.GetTypeName(obj.GetType());
return GeneralMarkup(formattedTypeName, context);
}

private Markup GeneralMarkup(string interpolated, RenderContext context)
{
var typeColor = context.Config.ColorConfig.TypeNameColor?.HexString ?? "default";
var escaped = interpolated.EscapeMarkup();
var markup = $"[{typeColor}]{escaped}[/]";

return new Markup(markup);
}

public (bool shouldHandle, object? handleContext) ShouldHandle(IDescriptor descriptor, object obj)
Expand Down

0 comments on commit ce33e8d

Please sign in to comment.