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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ reads that marker.
`.snap` file the macro wrote, and every failure summary, named a
variable the test never wrote. Snapshots already committed still match;
`cargo insta test --force-update-snapshots` rewrites their header. (#851)
- `Screen::parse`'s rustdoc and DESIGN §3 no longer say it accepts
"exactly" what `Display` and `with_styles` write. It also reads a few
shapes they never write, such as `size: +4x+1` or a row's spans split
over two lines, each into the screen its canonical form gives; the docs
now list them, and a test pins each one. (#849)
- Both published crates, `termlens` and `termlens-cli`, ship `LICENSE-MIT`
and `LICENSE-APACHE`. They are `MIT OR Apache-2.0`, and both licences ask
for their text in every copy, but the texts sat only at the repository
Expand Down
70 changes: 67 additions & 3 deletions crates/termlens/src/screen/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ use crate::{Error, Result};
impl Screen {
/// Parse the snapshot text format back into a `Screen`.
///
/// Accepts exactly what [`Display`](std::fmt::Display) and
/// Accepts what [`Display`](std::fmt::Display) and
/// [`with_styles`](Self::with_styles) write: the `size:`/`cursor:`
/// header, the grid one row per line with trailing blanks stripped
/// (missing trailing rows and columns are blank), and optionally a blank
/// line, `styles:` and its span lines (or `(none)`). Everything else is
/// an [`Error::Parse`] naming the line.
/// line, `styles:` and its span lines (or `(none)`). It also reads a few
/// shapes they never write, each into the screen its canonical form
/// gives: a `+` before a number in the header, one space or none before
/// `cursor:`, a row's spans split over several lines, blank lines
/// between span lines, `(none)` after spans, a one-column range written
/// `0-0`, a token repeated or out of order, and a span with no token
/// (the default style). Any other line is an [`Error::Parse`] naming
/// it. A file that parses is therefore not proof that termlens wrote
/// it; rendering it back gives the canonical form.
///
/// **The header's row count decides where the grid ends**, so a grid may
/// contain the words `styles:` or `(none)` as ordinary content. The one
Expand Down Expand Up @@ -502,6 +509,63 @@ mod tests {
assert!(screen.diff(&none).is_empty());
}

/// Shapes `Display` and `with_styles` never write, which the reader
/// takes as the canonical form beside them (#849). Each reads to the
/// screen its canonical form gives, and writes back as that form.
#[test]
fn the_reader_accepts_these_shapes_beside_the_canonical_ones() {
for (written, canonical) in [
// A leading `+` on the size and the cursor.
("size: +4x+1 cursor: 0,0\nhi", "size: 4x1 cursor: 0,0\nhi"),
("size: 4x1 cursor: +0,+0\nhi", "size: 4x1 cursor: 0,0\nhi"),
// One space before `cursor:`, or none.
("size: 4x1 cursor: 0,0\nhi", "size: 4x1 cursor: 0,0\nhi"),
("size: 4x1cursor: 0,0\nhi", "size: 4x1 cursor: 0,0\nhi"),
// A row listed twice, its spans merged.
(
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold\n0: 1 italic",
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold; 1 italic",
),
// `(none)` after spans, and blank lines between span lines.
(
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold\n(none)",
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold",
),
(
"size: 4x2 cursor: 0,0\nhi\nho\n\nstyles:\n0: 0 bold\n\n1: 1 italic",
"size: 4x2 cursor: 0,0\nhi\nho\n\nstyles:\n0: 0 bold\n1: 1 italic",
),
// A one-column range, a repeated token, and a run's tokens in
// another order.
(
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0-0 bold",
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold",
),
(
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold bold",
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold",
),
(
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold fg=1",
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 fg=1 bold",
),
// A span with no token is the default style.
(
"size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0",
"size: 4x1 cursor: 0,0\nhi",
),
] {
let screen = Screen::parse(written).unwrap_or_else(|e| panic!("{written:?}: {e}"));
let expected = Screen::parse(canonical).unwrap();
assert!(screen.diff(&expected).is_empty(), "{written:?}");
assert_eq!(
screen.with_styles().to_string(),
expected.with_styles().to_string(),
"{written:?}"
);
}
}

/// The header's row count decides where the grid ends, so these words
/// are content when they fall inside it (#296).
#[test]
Expand Down
17 changes: 12 additions & 5 deletions docs/DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,9 +974,15 @@ from the one the macro targets.
### Reading it back

`Screen::parse` (0.10.0) is the reader for everything above: it accepts
exactly what `Display` and `with_styles` write — the header, the grid, and
optionally the blank line, `styles:` and its spans — and every other line is
an `Error::Parse` naming it. The `termlens` CLI is built on it: `diff` and
what `Display` and `with_styles` write — the header, the grid, and
optionally the blank line, `styles:` and its spans — and a few shapes they
never write, each read as its canonical form: a `+` before a number in the
header, one space or none before `cursor:`, a row's spans split over
several lines or with blank lines between them, `(none)` after spans, a
range written `0-0`, a token repeated or out of order, and a span with no
token. Every other line is an `Error::Parse` naming it. The tolerances stay
because STABILITY promises that a file any release from 0.10.1 wrote parses
in every later one, and a hand-edited snapshot may hold any of them. The `termlens` CLI is built on it: `diff` and
`render` take saved screens, so a snapshot outlives the test run that
produced it.

Expand Down Expand Up @@ -1025,8 +1031,9 @@ compares `==`.
### What a reader skips, and what it does not

The format proper begins at the `size:` header and ends with the grid or
the `styles:` block. `Screen::parse` accepts exactly that — every other
line is an `Error::Parse` naming it. termlens itself writes two things
the `styles:` block. `Screen::parse` accepts that, with the tolerances
listed under "Reading it back" — every other line is an `Error::Parse`
naming it. termlens itself writes two things
*around* a screen, and its own reader, the `termlens` CLI, skips both:

- **Above:** the insta header, `---` / `source: …` / `---`, which a `.snap`
Expand Down
Loading