diff --git a/CHANGELOG.md b/CHANGELOG.md index bc88db7b..37fa7525 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/Directory.Packages.props b/Directory.Packages.props index 1f8ad09c..29c84966 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -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. --> - - - - + + + + diff --git a/src/SharpMUTerm.Core/Text/MarkupLineParser.cs b/src/SharpMUTerm.Core/Text/MarkupLineParser.cs index 3205f6fd..5634777f 100644 --- a/src/SharpMUTerm.Core/Text/MarkupLineParser.cs +++ b/src/SharpMUTerm.Core/Text/MarkupLineParser.cs @@ -274,13 +274,14 @@ private IReadOnlyList Convert(IReadOnlyList lines, out var panes = new List<(string Name, List Spans)>(); var lineFrame = FrameName(decoded.Destination); MxpFlagMarkup? flagSeen = null; + var elements = new Dictionary, 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; @@ -291,7 +292,7 @@ private IReadOnlyList Convert(IReadOnlyList 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; @@ -307,16 +308,16 @@ private IReadOnlyList Convert(IReadOnlyList 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); @@ -340,14 +341,21 @@ private IReadOnlyList Convert(IReadOnlyList lines, out return (line, panes.Count > 0); } - private static void Add(List spans, string text, TextStyle style, SpanInteraction? interaction) + /// + /// 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. holds the + /// element of each list's last span. + /// + private static void Add(List spans, string text, TextStyle style, SpanInteraction? interaction, ElementMarkup? element, Dictionary, 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; @@ -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. is replaced for a picture, whose text is its label. /// - 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]; @@ -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; } } @@ -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) diff --git a/src/SharpMUTerm.Core/Text/StyledText.cs b/src/SharpMUTerm.Core/Text/StyledText.cs index e669dd5c..c1b1f3ab 100644 --- a/src/SharpMUTerm.Core/Text/StyledText.cs +++ b/src/SharpMUTerm.Core/Text/StyledText.cs @@ -78,6 +78,7 @@ public static StyledLine StripColour(StyledLine line) } var spans = new List(line.Spans.Count); + var previous = default(TextStyle); foreach (var span in line.Spans) { var style = span.Style @@ -85,16 +86,21 @@ public static StyledLine StripColour(StyledLine line) .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); diff --git a/tests/SharpMUTerm.Core.Tests/Protocols/MxpDefinitionTests.cs b/tests/SharpMUTerm.Core.Tests/Protocols/MxpDefinitionTests.cs index 4b488e86..873b3582 100644 --- a/tests/SharpMUTerm.Core.Tests/Protocols/MxpDefinitionTests.cs +++ b/tests/SharpMUTerm.Core.Tests/Protocols/MxpDefinitionTests.cs @@ -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(); + parser.Flagged += (_, e) => flags.Add(e); + + Lines(parser, Secure + "", Secure + "northsouth"); + + await Assert.That(flags.Select(f => f.Text)).IsEquivalentTo( + new[] { "north", "south" }, TUnit.Assertions.Enums.CollectionOrdering.Matching); + } + + [Test] + [Arguments("ab")] + [Arguments("ab")] + [Arguments("ab")] + 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 + "ab")[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() { diff --git a/tests/SharpMUTerm.Core.Tests/Text/StyledTextTests.cs b/tests/SharpMUTerm.Core.Tests/Text/StyledTextTests.cs index 1d8849dd..36ccb859 100644 --- a/tests/SharpMUTerm.Core.Tests/Text/StyledTextTests.cs +++ b/tests/SharpMUTerm.Core.Tests/Text/StyledTextTests.cs @@ -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() {