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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- `MarkupText.ClearScreen()` is written in MXP as `ESC[H ESC[2J` when `WithAnsi()` is registered, in place of
nothing: MXP has no clear of its own, and its clients already read SGR in the same stream. Pueblo keeps
`<xch_page clear="text">` alone. [Formats](docs/formats.md#clearing-the-screen) has a table of which clients
clear: MUSHclient, Mudlet and BeipMU drop both instructions, so a game should not count on a clear having
happened (#109).

- Half-block pictures write a cell whose two halves are the same colour as a space on that background, not
as `█`. A background fills the whole cell in every client, but clients that draw `█` from the font (BeipMU
among them) fill only the glyph's height, which is shorter than a line, so solid areas came out striped.
Expand Down
5 changes: 3 additions & 2 deletions MarkupString.Ansi/AnsiRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class AnsiRegistration
/// <summary>
/// Returns a registry that renders <see cref="AnsiMarkup"/> in all seven formats and serialises
/// it under kind <c>"ansi"</c>. Plain needs nothing: the body passes through. It also writes the
/// bell, a <see cref="ClearScreenMarkup"/> for a terminal, an <see cref="ImageMarkup"/> as
/// bell, a <see cref="ClearScreenMarkup"/> for a terminal and an MXP client, an <see cref="ImageMarkup"/> as
/// BBCode's <c>[img]</c> and Markdown's <c>![]()</c>, and a laid-out block as Markdown's own
/// structure — a table, a list, a rule.
/// </summary>
Expand Down Expand Up @@ -36,7 +36,8 @@ public static MarkupRegistry WithAnsi(this MarkupRegistry registry)
.With(new BellEmitter(MarkupFormat.Mxp))
.With(new BellEmitter(MarkupFormat.Html))
// From the shared vocabulary, what a terminal, BBCode and Markdown can express.
.With(new ClearScreenAnsiEmitter())
.With(new ClearScreenAnsiEmitter(MarkupFormat.Ansi))
.With(new ClearScreenAnsiEmitter(MarkupFormat.Mxp))
.With(new ImageBBCodeEmitter())
.With(new ImageMarkdownEmitter())
// A layout as Markdown's own structure. Markdown has no styling this package does not already
Expand Down
14 changes: 10 additions & 4 deletions MarkupString.Ansi/Emitters/ElementEmitters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
namespace MarkupString.Ansi;

/// <summary>
/// Clears a terminal's screen. The rest of the shared vocabulary is left to the core rule for a
/// terminal: a point writes nothing, and a markup wrapping text writes the text.
/// Clears the screen with the terminal's erase, in <see cref="MarkupFormat.Ansi"/> and in
/// <see cref="MarkupFormat.Mxp"/>, which has no clear of its own but already carries SGR. The rest of
/// the shared vocabulary is left to the core rule for a terminal: a point writes nothing, and a markup
/// wrapping text writes the text.
/// </summary>
internal sealed class ClearScreenAnsiEmitter : IMarkupEmitter
/// <remarks>
/// An MXP client that keeps a scrolling window reads the erase and drops it (Mudlet, MUSHclient), so it
/// costs nothing there and clears wherever the client honours it.
/// </remarks>
internal sealed class ClearScreenAnsiEmitter(MarkupFormat format) : IMarkupEmitter
{
/// <summary>Home the cursor, then erase the display.</summary>
internal const string Sequence = "\u001b[H\u001b[2J";

public Type MarkupType => typeof(ClearScreenMarkup);

public MarkupFormat Format => MarkupFormat.Ansi;
public MarkupFormat Format { get; } = format;

public void Emit(IMarkup markup, ReadOnlySpan<char> body, in EmitContext context, IBufferWriter<char> output)
{
Expand Down
4 changes: 4 additions & 0 deletions MarkupString.Mxp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ You say a thing once; this package is how an MXP client hears it. The same text
client through `MarkupString.Pueblo`, to a browser through `MarkupString.Html`, and to a terminal,
which gets a picture's description, a pane's text, and nothing for a sound.

MXP has no clear of its own, so `MarkupText.ClearScreen()` is written as the terminal's `ESC[H ESC[2J`
when `WithAnsi()` is registered. Mudlet and MUSHclient drop it; see
[the client table](https://github.com/SharpMUSH/MarkupString/blob/main/docs/formats.md#clearing-the-screen).

```sh
dotnet add package MarkupString.Mxp
```
Expand Down
12 changes: 12 additions & 0 deletions MarkupString.Tests/Decoding/MxpDecodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ public async Task OpenTags_WorkOnAnOpenLine()
await Assert.That(spans[2].Style.Foreground).IsEqualTo(new RgbColor(255, 0, 0));
}

/// <summary>MXP has no clear of its own; the terminal's erase a server writes for one reads back as one clear.</summary>
[Test]
public async Task ClearScreen_AsAServerWritesIt_IsOneClear()
{
var written = MarkupText.Concat([MarkupText.ClearScreen(), MarkupText.Plain("top")])
.Render(MarkupFormat.Mxp, MarkupRegistry.Empty.WithAnsi().WithMxp().WithMxpSecureLines());

var (lines, _, _) = Decode(written + "\n");
await Assert.That(Markups(lines[0]).OfType<ClearScreenMarkup>().Count()).IsEqualTo(1);
await Assert.That(lines[0].Content.ToPlainText()).IsEqualTo("top");
}

[Test]
public async Task SecureTags_AreDroppedOnAnOpenLine()
{
Expand Down
2 changes: 1 addition & 1 deletion MarkupString.Tests/SharedVocabularyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public async Task ClearingTheScreenIsEachFormatsOwnInstruction()
await Assert.That(Render(clear, MarkupFormat.Ansi)).IsEqualTo("\e[H\e[2J");
await Assert.That(Render(clear, MarkupFormat.Pueblo)).IsEqualTo("<xch_page clear=\"text\">");
await Assert.That(Render(clear, MarkupFormat.Html)).IsEqualTo("<span class=\"ms-clear\"></span>");
await Assert.That(Render(clear, MarkupFormat.Mxp)).IsEqualTo(string.Empty).Because("MXP has no such instruction");
await Assert.That(Render(clear, MarkupFormat.Mxp)).IsEqualTo("\e[H\e[2J").Because("MXP has no clear of its own");
await Assert.That(Render(clear, MarkupFormat.Plain)).IsEqualTo(string.Empty);
}

Expand Down
7 changes: 6 additions & 1 deletion MarkupString/Elements/ScreenMarkup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ namespace MarkupString;
/// </summary>
/// <remarks>
/// A terminal gets <c>ESC[H ESC[2J</c>, Pueblo <c>&lt;xch_page clear="text"&gt;</c>, and an HTML page an
/// empty <c>ms-clear</c> element to act on. MXP has no such instruction and is sent nothing.
/// empty <c>ms-clear</c> element to act on. MXP has no clear of its own and is sent the terminal's.
/// <para>
/// Most MUD clients keep a scrolling window and drop both instructions, so a clear is a request, not a
/// promise: send nothing after it that only makes sense on an empty screen. docs/formats.md lists which
/// clients clear.
/// </para>
/// </remarks>
public sealed class ClearScreenMarkup : IPointMarkup
{
Expand Down
27 changes: 26 additions & 1 deletion docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ line.Render(MarkupFormat.Ansi); // A map of the city The door creaks.
| `StopSound` | `<SOUND Off>` / `<MUSIC Off>` | `<img xch_sound="stop" xch_device>` | `ms-sound-stop` | nothing |
| `Image` | `<IMAGE>` | `<img>` | `<img>` | the description, or the address; BBCode `[img]`, Markdown `![description](address)` |
| `Pane` | `<FRAME name><DEST name>…</DEST>` | `<xch_pane action="redirect">…` and back to `_previous` | `ms-pane` around the text | the text |
| `ClearScreen` | nothing | `<xch_page clear="text">` | `ms-clear` | ANSI `ESC[H ESC[2J` |
| `ClearScreen` | `ESC[H ESC[2J` (with `WithAnsi`) | `<xch_page clear="text">` | `ms-clear` | ANSI `ESC[H ESC[2J` |
| `Prefetch` | nothing | `<xch_prefetch href xch_prob>` | `<link rel="prefetch">` | nothing |
| `ExpireLinks` | `<EXPIRE>` | nothing | `ms-expire` | nothing |
| `Variable`, `Gauge`, `Status` | `<VAR>`, `<GAUGE>`, `<STAT>` | the text | `ms-variable`, `ms-gauge`, `ms-status` around the text | the text |
Expand Down Expand Up @@ -117,6 +117,31 @@ carrier, so a point can never stand over words that a format would then swallow,
with one out of place drops the point rather than the text. Styling *around* a point is fine: the
styling is written only if the point is.

### Clearing the screen

`MarkupText.ClearScreen()` asks the client to clear. MXP has no clear of its own, so it is sent the
terminal's erase, which its clients already read alongside SGR. Pueblo keeps `<xch_page clear="text">`
alone: the Pueblo client turns `ESC[2J` into that same tag, and the other Pueblo clients act on neither,
so adding the erase would clear nothing more and would make one clear read back as two.

Most MUD clients keep a scrolling window and drop both, without showing anything:

| Client | `ESC[H ESC[2J` | `<xch_page clear="text">` | Where |
|---|---|---|---|
| A terminal (telnet and the like) | clears | shown as text | — |
| Pueblo | clears (rewritten as `xch_page clear=text`) | clears | `ChWConn.cpp`, `ChHeadElement.cpp` |
| MUSHclient | dropped: only `m` and `z` are read after `ESC[` | dropped: `xch_page` only turns MXP off | `telnet_phases.cpp`, `mxp/mxpOpenAtomic.cpp` |
| Mudlet | dropped: erase in display is refused | no Pueblo | `src/TBuffer.cpp` |
| BeipMU | dropped: only SGR and OSC 8 are read | dropped: its attributes are skipped | `src/AnsiParser.cpp`, `src/TextToLine.cpp` |
| SharpMUTerm | read as a `ClearScreenMarkup`, not acted on | the same | `MarkupLineParser.Point` |
| SharpClient | read as a clear, then ignored to keep scrollback | the same | `OutputView.razor` |
| A `MarkupString.Web` page | — | — | `ms-clear` removes what came before |

Nothing is written in place of a clear for a client that will drop it: the server cannot tell which
clients those are, and a blank line or a rule would stay on a screen that did clear. So a clear is a
request, not a promise. Send nothing after it that only makes sense on an empty screen, such as the
same screen again on the assumption the first one is gone.

### What an MXP client said it supports

MXP asks a client which elements it can render with `<SUPPORT>`, and a client that answered `-image`
Expand Down
Loading