Skip to content

Commit a252cd4

Browse files
committed
Merge pull request scriptcs#1156 from gregoryyoung/printer
Pretty Printing
2 parents 84eafdd + 539576a commit a252cd4

14 files changed

Lines changed: 184 additions & 69 deletions

File tree

src/ScriptCs.Contracts/IScriptEnvironment.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ namespace ScriptCs.Contracts
99
public interface IScriptEnvironment
1010
{
1111
IReadOnlyList<string> ScriptArgs { get; }
12+
void AddCustomPrinter<T>(Func<T, string> printer);
13+
void Print<T>(T o);
14+
void Print(object o);
1215
}
1316
}

src/ScriptCs.Contracts/Printers.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ScriptCs.Contracts
5+
{
6+
public class Printers
7+
{
8+
private readonly IObjectSerializer _serializer;
9+
private readonly Dictionary<Type, Func<object, string>> _dictionary = new Dictionary<Type, Func<object, string>>();
10+
public Printers(IObjectSerializer serializer)
11+
{
12+
_serializer = serializer;
13+
}
14+
15+
public void AddCustomPrinter<T>(Func<T, string> printer)
16+
{
17+
_dictionary[typeof(T)] = x => printer((T) x);
18+
}
19+
20+
private string GetStringFor(Type t, object obj)
21+
{
22+
Func<object, string> printer;
23+
if(_dictionary.TryGetValue(t, out printer)) {
24+
return printer(obj);
25+
} else {
26+
return _serializer.Serialize(obj);
27+
}
28+
}
29+
30+
public string GetStringFor(object obj)
31+
{
32+
return GetStringFor(obj.GetType(), obj);
33+
}
34+
35+
public string GetStringFor<T>(T obj)
36+
{
37+
return GetStringFor(typeof(T), obj);
38+
}
39+
}
40+
}

src/ScriptCs.Contracts/ScriptCs.Contracts.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
<Compile Include="LogLevel.cs" />
106106
<Compile Include="LogProviderExtensions.cs" />
107107
<Compile Include="ModuleAttribute.cs" />
108+
<Compile Include="Printers.cs" />
108109
<Compile Include="ProjectItem.cs" />
109110
<Compile Include="Properties\AssemblyInfo.cs" />
110111
<Compile Include="ScriptPack.cs" />

src/ScriptCs.Core/Repl.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class Repl : ScriptExecutor, IRepl
1212
private readonly string[] _scriptArgs;
1313

1414
private readonly IObjectSerializer _serializer;
15+
private readonly Printers _printers;
1516
private readonly ILog _log;
1617

1718
[Obsolete("Support for Common.Logging types was deprecated in version 0.15.0 and will soon be removed.")]
@@ -24,7 +25,8 @@ public Repl(
2425
IScriptLibraryComposer composer,
2526
IConsole console,
2627
IFilePreProcessor filePreProcessor,
27-
IEnumerable<IReplCommand> replCommands)
28+
IEnumerable<IReplCommand> replCommands,
29+
Printers printers)
2830
: this(
2931
scriptArgs,
3032
fileSystem,
@@ -34,7 +36,8 @@ public Repl(
3436
composer,
3537
console,
3638
filePreProcessor,
37-
replCommands)
39+
replCommands,
40+
printers)
3841
{
3942
}
4043

@@ -47,7 +50,8 @@ public Repl(
4750
IScriptLibraryComposer composer,
4851
IConsole console,
4952
IFilePreProcessor filePreProcessor,
50-
IEnumerable<IReplCommand> replCommands)
53+
IEnumerable<IReplCommand> replCommands,
54+
Printers printers)
5155
: base(fileSystem, filePreProcessor, scriptEngine, logProvider, composer)
5256
{
5357
Guard.AgainstNullArgument("serializer", serializer);
@@ -56,6 +60,7 @@ public Repl(
5660

5761
_scriptArgs = scriptArgs;
5862
_serializer = serializer;
63+
_printers = printers;
5964
_log = logProvider.ForCurrentType();
6065
Console = console;
6166
Commands = replCommands != null ? replCommands.Where(x => x.CommandName != null).ToDictionary(x => x.CommandName, x => x) : new Dictionary<string, IReplCommand>();
@@ -133,7 +138,7 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
133138
}
134139

135140
Console.ForegroundColor = ConsoleColor.Cyan;
136-
141+
137142
InjectScriptLibraries(FileSystem.CurrentDirectory, preProcessResult, ScriptPackSession.State);
138143

139144
Buffer = (Buffer == null)
@@ -172,9 +177,7 @@ public override ScriptResult Execute(string script, params string[] scriptArgs)
172177
{
173178
Console.ForegroundColor = ConsoleColor.Yellow;
174179

175-
var serializedResult = _serializer.Serialize(result.ReturnValue);
176-
177-
Console.WriteLine(serializedResult);
180+
Console.WriteLine(_printers.GetStringFor(result.ReturnValue));
178181
}
179182

180183
Buffer = null;
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using ScriptCs.Contracts;
34

45
namespace ScriptCs
56
{
67
public class ScriptEnvironment : IScriptEnvironment
78
{
8-
public ScriptEnvironment(string[] scriptArgs)
9+
private readonly IConsole _console;
10+
private readonly Printers _printers;
11+
12+
public ScriptEnvironment(string[] scriptArgs, IConsole console, Printers printers)
913
{
14+
_console = console;
15+
_printers = printers;
1016
ScriptArgs = scriptArgs;
1117
}
1218

1319
public IReadOnlyList<string> ScriptArgs { get; private set; }
20+
21+
public void AddCustomPrinter<T>(Func<T, string> printer)
22+
{
23+
_console.WriteLine("Adding custom printer for " + typeof(T).Name);
24+
_printers.AddCustomPrinter<T>(printer);
25+
}
26+
27+
public void Print(object o)
28+
{
29+
_console.WriteLine(_printers.GetStringFor(o));
30+
}
31+
32+
public void Print<T>(T o)
33+
{
34+
_console.WriteLine(_printers.GetStringFor<T>(o));
35+
}
36+
1437
}
1538
}

src/ScriptCs.Core/ScriptHost.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ScriptCs.Contracts;
1+
using System;
2+
using ScriptCs.Contracts;
23

34
namespace ScriptCs
45
{

src/ScriptCs.Core/ScriptHostFactory.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ namespace ScriptCs
44
{
55
public class ScriptHostFactory : IScriptHostFactory
66
{
7+
private readonly IConsole _console;
8+
private readonly Printers _printers;
9+
10+
public ScriptHostFactory(IConsole console, Printers printers)
11+
{
12+
_console = console;
13+
_printers = printers;
14+
}
15+
716
public IScriptHost CreateScriptHost(IScriptPackManager scriptPackManager, string[] scriptArgs)
817
{
9-
return new ScriptHost(scriptPackManager, new ScriptEnvironment(scriptArgs));
18+
return new ScriptHost(scriptPackManager, new ScriptEnvironment(scriptArgs, _console, _printers));
1019
}
1120
}
1221
}

src/ScriptCs.Hosting/RuntimeServices.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ protected override IContainer CreateContainer()
8585
builder.RegisterType(_replType).As<IRepl>().SingleInstance();
8686
builder.RegisterType<ScriptServices>().SingleInstance();
8787
builder.RegisterType<Repl>().As<IRepl>().SingleInstance();
88+
builder.RegisterType<Printers>().SingleInstance();
8889

8990
RegisterLineProcessors(builder);
9091
RegisterReplCommands(builder);

test/ScriptCs.Core.Tests/ReplCommands/AliasCommandTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public void ShouldAliasCommandWithNewName(
5353
composer.Object,
5454
console.Object,
5555
filePreProcessor.Object,
56-
new List<IReplCommand> { dummyCommand.Object });
56+
new List<IReplCommand> { dummyCommand.Object },
57+
new Printers(serializer.Object));
5758

5859
var cmd = new AliasCommand(console.Object);
5960

test/ScriptCs.Core.Tests/ReplTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ public static Repl GetRepl(Mocks mocks)
6464
mocks.ScriptLibraryComposer.Object,
6565
mocks.Console.Object,
6666
mocks.FilePreProcessor.Object,
67-
mocks.ReplCommands.Select(x => x.Object));
67+
mocks.ReplCommands.Select(x => x.Object),
68+
new Printers(mocks.ObjectSerializer.Object));
6869
}
6970

7071
public class TheConstructor

0 commit comments

Comments
 (0)