Skip to content

Commit

Permalink
Rich Text: Clean up FormattedMessage.Builder users
Browse files Browse the repository at this point in the history
  • Loading branch information
Efruit committed Dec 17, 2021
1 parent a403b56 commit b6d0e3a
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 102 deletions.
32 changes: 19 additions & 13 deletions Robust.Client/Console/Completions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
Expand Down Expand Up @@ -83,7 +84,7 @@ public Entry(LiteResult result)
{
MouseFilter = MouseFilterMode.Stop;
Result = result;
var compl = new FormattedMessage.Builder();
var compl = new List<Section>();
var dim = Color.FromHsl((0f, 0f, 0.8f, 1f));

// warning: ew ahead
Expand All @@ -106,21 +107,26 @@ public Entry(LiteResult result)
basen = "field name";

Color basec = ScriptingColorScheme.ColorScheme[basen];
compl.PushColor(basec * dim);
compl.AddText(Result.DisplayTextPrefix);
compl.PushColor(basec);
compl.AddText(Result.DisplayText);
compl.PushColor(basec * dim);
compl.AddText(Result.DisplayTextSuffix);
compl.AddText(" [" + String.Join(", ", Result.Tags) + "]");
Color dimmed = basec * dim;
compl.AddRange(new []
{
new Section() { Color=dimmed.ToArgb(), Content=Result.DisplayTextPrefix },
new Section() { Color=basec.ToArgb(), Content=Result.DisplayText },
new Section() { Color=dimmed.ToArgb(), Content=Result.DisplayTextSuffix }
}
);

compl.Add(new Section() { Color=dimmed.ToArgb(), Content=" [" + String.Join(", ", Result.Tags) + "]" });
if (Result.InlineDescription.Length != 0)
{
compl.PushNewline();
compl.AddText(": ");
compl.PushColor(Color.LightSlateGray);
compl.AddText(Result.InlineDescription);
compl.AddRange(new []
{
new Section() { Color=(basec * dim).ToArgb(), Content="\n: " },
new Section() { Color=Color.LightSlateGray.ToArgb(), Content=Result.InlineDescription }
}
);
}
SetMessage(compl.Build());
SetMessage(new FormattedMessage(compl.ToArray()));
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions Robust.Client/Console/ScriptClient.ScriptConsoleServer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.Localization;
using Robust.Shared.Maths;
using Robust.Shared.Network.Messages;
using Robust.Shared.Utility;

Expand Down Expand Up @@ -95,11 +94,13 @@ public void ReceiveResponse(MsgScriptResponse response)
_linesEntered = 0;

// Echo entered script.
var echoMessage = new FormattedMessage.Builder();
echoMessage.PushColor(Color.FromHex("#D4D4D4"));
echoMessage.AddText("> ");
echoMessage.AddMessage(response.Echo);
OutputPanel.AddMessage(echoMessage.Build());
OutputPanel.AddMessage(new FormattedMessage(new []
{
new Section() { Color=unchecked((int) 0xFFD4D4D4), Content="> " },
}
));

OutputPanel.AddMessage(response.Echo);

OutputPanel.AddMessage(response.Response);

Expand Down
40 changes: 21 additions & 19 deletions Robust.Client/Console/ScriptConsoleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,11 @@ protected override async void Run()
newScript.Compile();

// Echo entered script.
var echoMessage = new FormattedMessage.Builder();
echoMessage.PushColor(Color.FromHex("#D4D4D4"));
echoMessage.AddText("> ");
var echoMessage = new List<Section>();
echoMessage.Add(new Section() { Color=unchecked((int) 0xFFD4D4D4), Content="> " });
ScriptInstanceShared.AddWithSyntaxHighlighting(newScript, echoMessage, code, _highlightWorkspace);

OutputPanel.AddMessage(echoMessage.Build());
OutputPanel.AddMessage(new FormattedMessage(echoMessage.ToArray()));

try
{
Expand All @@ -148,17 +147,17 @@ protected override async void Run()
}
catch (CompilationErrorException e)
{
var msg = new FormattedMessage.Builder();

msg.PushColor(Color.Crimson);
var strb = new StringBuilder();

foreach (var diagnostic in e.Diagnostics)
{
msg.AddText(diagnostic.ToString());
msg.AddText("\n");
}
strb.Append(diagnostic.ToString() + "\n");

OutputPanel.AddMessage(new FormattedMessage(new []
{
new Section() { Color=Color.Crimson.ToArgb(), Content=strb.ToString() }
}
));

OutputPanel.AddMessage(msg.Build());
OutputPanel.AddText(">");

PromptAutoImports(e.Diagnostics, code);
Expand All @@ -167,16 +166,19 @@ protected override async void Run()

if (_state.Exception != null)
{
var msg = new FormattedMessage.Builder();
msg.PushColor(Color.Crimson);
msg.AddText(CSharpObjectFormatter.Instance.FormatException(_state.Exception));
OutputPanel.AddMessage(msg.Build());
OutputPanel.AddMessage(new FormattedMessage(new []
{
new Section() { Color=Color.Crimson.ToArgb(), Content=CSharpObjectFormatter.Instance.FormatException(_state.Exception) }
}
));
}
else if (ScriptInstanceShared.HasReturnValue(newScript))
{
var msg = new FormattedMessage.Builder();
msg.AddText(ScriptInstanceShared.SafeFormat(_state.ReturnValue));
OutputPanel.AddMessage(msg.Build());
OutputPanel.AddMessage(new FormattedMessage(new []
{
new Section() { Content=ScriptInstanceShared.SafeFormat(_state.ReturnValue) }
}
));
}

OutputPanel.AddText(">");
Expand Down
32 changes: 15 additions & 17 deletions Robust.Client/Log/DebugConsoleLogHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Robust.Client.Console;
using System.Collections.Generic;
using Robust.Client.Console;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
Expand All @@ -23,24 +24,21 @@ public void Log(string sawmillName, LogEvent message)
if (sawmillName == "CON")
return;

var formatted = new FormattedMessage.Builder();
var formatted = new List<Section>();
var robustLevel = message.Level.ToRobust();
formatted.PushColor(Color.DarkGray);
formatted.AddText("[");
formatted.PushColor(LogLevelToColor(robustLevel));
formatted.AddText(LogMessage.LogLevelToName(robustLevel));
formatted.Pop();
formatted.AddText($"] {sawmillName}: ");
formatted.Pop();
formatted.PushColor(Color.LightGray);
formatted.AddText(message.RenderMessage());
formatted.Pop();
formatted.AddRange(new []
{
new Section() { Color=Color.DarkGray.ToArgb(), Content="[" },
new Section() { Color=LogLevelToColor(robustLevel).ToArgb(), Content=LogMessage.LogLevelToName(robustLevel) },
new Section() { Color=Color.DarkGray.ToArgb(), Content=$"] {sawmillName}: " },
new Section() { Color=Color.LightGray.ToArgb(), Content=message.RenderMessage() }
}
);

if (message.Exception != null)
{
formatted.AddText("\n");
formatted.AddText(message.Exception.ToString());
}
Console.AddFormattedLine(formatted.Build());
formatted.Add(new Section() { Content="\n" + message.Exception.ToString() });

Console.AddFormattedLine(new FormattedMessage(formatted.ToArray()));
}

private static Color LogLevelToColor(LogLevel level)
Expand Down
12 changes: 6 additions & 6 deletions Robust.Client/UserInterface/Controls/OutputPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void RemoveEntry(Index index)
_invalidateEntries();
}

public void AddText(string text)
{
var msg = new FormattedMessage.Builder();
msg.AddText(text);
AddMessage(msg.Build());
}
public void AddText(string text) =>
AddMessage(new FormattedMessage(new []
{
new Section() { Content=text }
}
));

public void AddMessage(FormattedMessage message)
{
Expand Down
12 changes: 6 additions & 6 deletions Robust.Client/UserInterface/Controls/RichTextLabel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public void SetMessage(FormattedMessage message)
InvalidateMeasure();
}

public void SetMessage(string message)
{
var msg = new FormattedMessage.Builder();
msg.AddText(message);
SetMessage(msg.Build());
}
public void SetMessage(string message) =>
SetMessage(new FormattedMessage(new []
{
new Section { Content=message }
}
));

protected override Vector2 MeasureOverride(Vector2 availableSize)
{
Expand Down
14 changes: 6 additions & 8 deletions Robust.Client/UserInterface/CustomControls/DebugConsole.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,12 @@ private void OnHistoryChanged()
_flushHistoryToDisk();
}

public void AddLine(string text, Color color)
{
var formatted = new FormattedMessage.Builder();
formatted.PushColor(color);
formatted.AddText(text);
formatted.Pop();
AddFormattedLine(formatted.Build());
}
public void AddLine(string text, Color color) =>
AddFormattedLine(new FormattedMessage(new []
{
new Section() { Color=color.ToArgb(), Content=text }
}
));

public void AddLine(string text)
{
Expand Down
40 changes: 20 additions & 20 deletions Robust.Server/Scripting/ScriptHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ private async void ReceiveScriptEval(MsgScriptEval message)
newScript.Compile();

// Echo entered script.
var echoMessage = new FormattedMessage.Builder();
var echoMessage = new List<Section>();
ScriptInstanceShared.AddWithSyntaxHighlighting(newScript, echoMessage, code, instance.HighlightWorkspace);

replyMessage.Echo = echoMessage.Build();
replyMessage.Echo = new FormattedMessage(echoMessage.ToArray());

var msg = new FormattedMessage.Builder();
var msg = new List<Section>();

try
{
Expand All @@ -205,17 +205,15 @@ private async void ReceiveScriptEval(MsgScriptEval message)
}
catch (CompilationErrorException e)
{
msg.PushColor(Color.Crimson);
var s = new Section();
s.Color = Color.Crimson.ToArgb();

foreach (var diagnostic in e.Diagnostics)
{
msg.AddText(diagnostic.ToString());
msg.AddText("\n");
}
s.Content = diagnostic.ToString() + "\n";

PromptAutoImports(e.Diagnostics, code, msg, instance);
s.Content += PromptAutoImports(e.Diagnostics, code, instance);

replyMessage.Response = msg.Build();
replyMessage.Response = new FormattedMessage(new [] { s });
_netManager.ServerSendMessage(replyMessage, message.MsgChannel);
return;
}
Expand All @@ -226,21 +224,25 @@ private async void ReceiveScriptEval(MsgScriptEval message)

if (instance.OutputBuffer.Length != 0)
{
msg.AddText(instance.OutputBuffer.ToString());
msg.Add(new Section() { Content=instance.OutputBuffer.ToString() });
instance.OutputBuffer.Clear();
}

if (instance.State.Exception != null)
{
msg.PushColor(Color.Crimson);
msg.AddText(CSharpObjectFormatter.Instance.FormatException(instance.State.Exception));
msg.Add(new Section() {
Color=Color.Crimson.ToArgb(),
Content=CSharpObjectFormatter.Instance.FormatException(instance.State.Exception)
});
}
else if (ScriptInstanceShared.HasReturnValue(newScript))
{
msg.AddText(ScriptInstanceShared.SafeFormat(instance.State.ReturnValue));
msg.Add(new Section() {
Content=ScriptInstanceShared.SafeFormat(instance.State.ReturnValue)
});
}

replyMessage.Response = msg.Build();
replyMessage.Response = new FormattedMessage(msg.ToArray());
_netManager.ServerSendMessage(replyMessage, message.MsgChannel);
}

Expand Down Expand Up @@ -313,18 +315,16 @@ private async void ReceiveScriptCompletion(MsgScriptCompletion message)
_netManager.ServerSendMessage(replyMessage, message.MsgChannel);
}

private void PromptAutoImports(
private string PromptAutoImports(
IEnumerable<Diagnostic> diags,
string code,
FormattedMessage.Builder output,
ScriptInstance instance)
{
if (!ScriptInstanceShared.CalcAutoImports(_reflectionManager, diags, out var found))
return;

output.AddText($"Auto-import {string.Join(", ", found)} (enter 'y')?");
return String.Empty;

instance.AutoImportRepeatBuffer = (found.ToArray(), code);
return $"Auto-import {string.Join(", ", found)} (enter 'y')?" ;
}


Expand Down
12 changes: 5 additions & 7 deletions Robust.Shared.Scripting/ScriptInstanceShared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static ScriptInstanceShared()
"var x = 5 + 5; var y = (object) \"foobar\"; void Foo(object a) { } Foo(y); Foo(x)";
var script = await CSharpScript.RunAsync(code);
var msg = new FormattedMessage.Builder();
var msg = new List<Section>();
// Even run the syntax highlighter!
AddWithSyntaxHighlighting(script.Script, msg, code, new AdhocWorkspace());
});
Expand All @@ -101,7 +101,7 @@ public static bool HasReturnValue(Script script)
return _getDiagnosticArguments(diag);
}

public static void AddWithSyntaxHighlighting(Script script, FormattedMessage.Builder msg, string code,
public static void AddWithSyntaxHighlighting(Script script, List<Section> msg, string code,
Workspace workspace)
{
var compilation = script.GetCompilation();
Expand All @@ -115,7 +115,7 @@ public static void AddWithSyntaxHighlighting(Script script, FormattedMessage.Bui
var start = span.TextSpan.Start;
if (start > current)
{
msg.AddText(code[current..start]);
msg.Add(new Section() { Content=code[current..start] });
}

if (current > start)
Expand All @@ -129,13 +129,11 @@ public static void AddWithSyntaxHighlighting(Script script, FormattedMessage.Bui
if (!ScriptingColorScheme.ColorScheme.TryGetValue(span.ClassificationType, out var color))
color = ScriptingColorScheme.ColorScheme[ScriptingColorScheme.Default];

msg.PushColor(color);
msg.AddText(src);
msg.Pop();
msg.Add(new Section() { Content=src, Color=color.ToArgb() });
current = span.TextSpan.End;
}

msg.AddText(code[current..]);
msg.Add(new Section() { Content=code[current..] });
}

private static IEnumerable<Assembly> GetDefaultReferences(IReflectionManager reflectionManager)
Expand Down

0 comments on commit b6d0e3a

Please sign in to comment.