Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ and a release's notes are its section. The procedure is in [`docs/RELEASING.md`]

### Fixed

- Two exits, links or pictures side by side that look the same are no longer read as one. A game that marks
exits with MXP and prints them next to each other now records each exit, and two links to the same command are
two links.

- Abbreviated login commands such as `c name password` and `con name password` are hidden from local echo,
transcripts and credential-filtered command history, including when used inside a command stack.

Expand Down
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
Reading server output: ANSI/VT, MXP and Pueblo into MarkupText, which Core folds into StyledLine
(Text/MarkupLineParser). One version for the family.
-->
<PackageVersion Include="MarkupString" Version="2.28.1" />
<PackageVersion Include="MarkupString.Ansi" Version="2.28.1" />
<PackageVersion Include="MarkupString.Mxp" Version="2.28.1" />
<PackageVersion Include="MarkupString.Pueblo" Version="2.28.1" />
<PackageVersion Include="MarkupString" Version="2.29.1" />
<PackageVersion Include="MarkupString.Ansi" Version="2.29.1" />
<PackageVersion Include="MarkupString.Mxp" Version="2.29.1" />
<PackageVersion Include="MarkupString.Pueblo" Version="2.29.1" />

<!-- Telnet negotiation: TELOPT/NAWS/MTTS/EOR/CHARSET/MSSP/GMCP/MSDP/MCCP/MXP negotiation. -->
<PackageVersion Include="TelnetNegotiationCore" Version="4.11.0" />
Expand Down
31 changes: 22 additions & 9 deletions src/SharpMUTerm.Core/Text/MarkupLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,14 @@ private IReadOnlyList<StyledLine> Convert(IReadOnlyList<DisplayLine> lines, out
var panes = new List<(string Name, List<StyledSpan> Spans)>();
var lineFrame = FrameName(decoded.Destination);
MxpFlagMarkup? flagSeen = null;
var elements = new Dictionary<List<StyledSpan>, ElementMarkup?>(ReferenceEqualityComparer.Instance);
var at = 0;

foreach (var run in content.Runs)
{
if (run.Start > at)
{
Add(main, content.Text.Substring(at, run.Start - at), TextStyle.Default, null);
Add(main, content.Text.Substring(at, run.Start - at), TextStyle.Default, null, null, elements);
}

at = run.End;
Expand All @@ -291,7 +292,7 @@ private IReadOnlyList<StyledLine> Convert(IReadOnlyList<DisplayLine> lines, out
}

var text = content.Text.Substring(run.Start, run.Length);
var (style, interaction, pane, flag) = Style(markups, ref text);
var (style, interaction, pane, flag, element) = Style(markups, ref text);
if (flag is not null)
{
flagSeen = flag;
Expand All @@ -307,16 +308,16 @@ private IReadOnlyList<StyledLine> Convert(IReadOnlyList<DisplayLine> lines, out
target = panes.Count - 1;
}

Add(panes[target].Spans, text, style, interaction);
Add(panes[target].Spans, text, style, interaction, element, elements);
continue;
}

Add(main, text, style, interaction);
Add(main, text, style, interaction, element, elements);
}

if (at < content.Text.Length)
{
Add(main, content.Text[at..], TextStyle.Default, null);
Add(main, content.Text[at..], TextStyle.Default, null, null, elements);
}

EndFlags(flagSeen, lineEnd: true);
Expand All @@ -340,14 +341,21 @@ private IReadOnlyList<StyledLine> Convert(IReadOnlyList<DisplayLine> lines, out
return (line, panes.Count > 0);
}

private static void Add(List<StyledSpan> spans, string text, TextStyle style, SpanInteraction? interaction)
/// <summary>
/// Appends text to a span list, joining it to the last span when it looks the same and belongs to the same element:
/// two links, pictures or exits side by side that look alike stay two spans. <paramref name="elements"/> holds the
/// element of each list's last span.
/// </summary>
private static void Add(List<StyledSpan> spans, string text, TextStyle style, SpanInteraction? interaction, ElementMarkup? element, Dictionary<List<StyledSpan>, ElementMarkup?> elements)
{
if (text.Length == 0)
{
return;
}

if (spans.Count > 0 && spans[^1].Style == style && Equals(spans[^1].Interaction, interaction))
var last = elements.GetValueOrDefault(spans);
elements[spans] = element;
if (spans.Count > 0 && Equals(last, element) && spans[^1].Style == style && Equals(spans[^1].Interaction, interaction))
{
spans[^1] = new StyledSpan(spans[^1].Text + text, style, interaction);
return;
Expand Down Expand Up @@ -430,13 +438,14 @@ private static MediaCommand Sound(SoundMarkup sound)
/// Folds one run's markup, innermost last, into SharpMUTerm's style and interaction, and finds the pane it is sent
/// to and the flag over it. <paramref name="text"/> is replaced for a picture, whose text is its label.
/// </summary>
private static (TextStyle Style, SpanInteraction? Interaction, PaneMarkup? Pane, MxpFlagMarkup? Flag) Style(MarkupSet markups, ref string text)
private static (TextStyle Style, SpanInteraction? Interaction, PaneMarkup? Pane, MxpFlagMarkup? Flag, ElementMarkup? Element) Style(MarkupSet markups, ref string text)
{
var folded = AnsiStyle.None;
CommandMenuMarkup? menu = null;
ImageMarkup? image = null;
PaneMarkup? pane = null;
MxpFlagMarkup? flag = null;
ElementMarkup? element = null;
for (var i = markups.Count - 1; i >= 0; i--)
{
var markup = markups[i];
Expand All @@ -459,6 +468,10 @@ private static (TextStyle Style, SpanInteraction? Interaction, PaneMarkup? Pane,
case MxpFlagMarkup m:
flag = m;
break;
case ElementMarkup m:
// Read outermost to innermost, so the last one is the element opened last.
element = m;
break;
}
}

Expand All @@ -469,7 +482,7 @@ private static (TextStyle Style, SpanInteraction? Interaction, PaneMarkup? Pane,
interaction = Picture(image, interaction, ref text);
}

return (style, interaction, pane, flag);
return (style, interaction, pane, flag, element);
}

private static SpanInteraction? Interaction(AnsiStyle style, CommandMenuMarkup? menu)
Expand Down
10 changes: 8 additions & 2 deletions src/SharpMUTerm.Core/Text/StyledText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,29 @@ public static StyledLine StripColour(StyledLine line)
}

var spans = new List<StyledSpan>(line.Spans.Count);
var previous = default(TextStyle);
foreach (var span in line.Spans)
{
var style = span.Style
.WithForeground(TerminalColor.Default)
.WithBackground(TerminalColor.Default);

// Merge with the previous span when stripping made them identical, so the result has as
// few spans as the text really needs (the renderer emits one markup tag per span).
// few spans as the text really needs (the renderer emits one markup tag per span). Two
// spans that were already identical are kept apart: they are two elements side by side
// (two exits, two links to one command), which the parser left as two on purpose.
if (spans.Count > 0 &&
spans[^1].Style == style &&
Equals(spans[^1].Interaction, span.Interaction))
Equals(spans[^1].Interaction, span.Interaction) &&
previous != span.Style)
{
spans[^1] = new StyledSpan(spans[^1].Text + span.Text, style, span.Interaction);
previous = span.Style;
continue;
}

spans.Add(new StyledSpan(span.Text, style, span.Interaction));
previous = span.Style;
}

return new StyledLine(spans, line.RuleColor);
Expand Down
36 changes: 36 additions & 0 deletions tests/SharpMUTerm.Core.Tests/Protocols/MxpDefinitionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,42 @@ await Assert.That(seen.Exits.Select(e => e.Key)).IsEquivalentTo(
new[] { "north", "east", "down" }, TUnit.Assertions.Enums.CollectionOrdering.Matching);
}

[Test]
public async Task TwoEqualFlaggedElementsSideBySideAreTwo()
{
var parser = new MarkupLineParser(ContentFormat.Mxp);
var flags = new List<MxpFlagText>();
parser.Flagged += (_, e) => flags.Add(e);

Lines(parser, Secure + "<!ELEMENT Ex FLAG=RoomExit>", Secure + "<Ex>north</Ex><Ex>south</Ex>");

await Assert.That(flags.Select(f => f.Text)).IsEquivalentTo(
new[] { "north", "south" }, TUnit.Assertions.Enums.CollectionOrdering.Matching);
}

[Test]
[Arguments("<SEND href=go>a</SEND><SEND href=go>b</SEND>")]
[Arguments("<A href='https://example.com/'>a</A><A href='https://example.com/'>b</A>")]
[Arguments("<SEND href='go|look'>a</SEND><SEND href='go|look'>b</SEND>")]
public async Task TwoEqualLinksSideBySideAreTwoSpans(string markup)
{
var parser = new MarkupLineParser(ContentFormat.Mxp);
var line = Lines(parser, Secure + markup)[0]!;

await Assert.That(line.Spans.Where(s => s.IsInteractive).Select(s => s.Text)).IsEquivalentTo(
new[] { "a", "b" }, TUnit.Assertions.Enums.CollectionOrdering.Matching);
}

[Test]
public async Task OneLinkStaysOneSpanAcrossALookAlikeBoundary()
{
var parser = new MarkupLineParser(ContentFormat.Mxp);
var line = Lines(parser, Secure + "<SEND href=go>a<I></I>b</SEND>")[0]!;

await Assert.That(line.Spans.Where(s => s.IsInteractive).Select(s => s.Text)).IsEquivalentTo(
new[] { "ab" }, TUnit.Assertions.Enums.CollectionOrdering.Matching);
}

[Test]
public async Task TheSpecsRoomLineTagsFlagTheirLine()
{
Expand Down
17 changes: 17 additions & 0 deletions tests/SharpMUTerm.Core.Tests/Text/StyledTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ public async Task StripColour_DoesNotMergeAcrossDifferentInteractions()
await Assert.That(stripped.Spans.Count).IsEqualTo(2);
}

[Test]
public async Task StripColour_KeepsTwoEqualLinksSideBySideApart()
{
// The parser leaves two look-alike links as two spans only when they are two elements.
var style = new TextStyle(TerminalColor.FromIndex(2), TerminalColor.Default, TextAttributes.None);
var line = new StyledLine(new[]
{
new StyledSpan("a", style, SpanInteraction.Command("go")),
new StyledSpan("b", style, SpanInteraction.Command("go")),
});

var stripped = StyledText.StripColour(line);

await Assert.That(stripped.Spans.Select(s => s.Text)).IsEquivalentTo(
new[] { "a", "b" }, TUnit.Assertions.Enums.CollectionOrdering.Matching);
}

[Test]
public async Task StripColour_LeavesAnUncolouredLineExactlyAsItWas()
{
Expand Down
Loading