diff --git a/CHANGELOG.md b/CHANGELOG.md index 05f2307b..97b983f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,17 @@ follow [Semantic Versioning](https://semver.org/). ### Added +- `out=file` sends a block's stdout to `.assets/.log` (or the + path given), and its output fence becomes an include of the file: the + first `fold_output` lines, or the window you pick by adding `match=`, + `before=`, `lines=`, `until=`, `from=` or `to=` to the fence, which + every run keeps. So a long build log stays out of the document, and + the document points at the part that matters. Copies keep their own + windows, includes of the log show the latest run, the file is + rewritten only when it changed, and `history=N` keeps earlier logs. + + ![A test block with out=file: its output fence shows the first 25 lines of the log, and a copy under "The first failure" shows the lines around the first --- FAIL](docs/changelog/spill-output.png) + - An include block can find its lines by what they hold, so it stays on them when the file changes above: `match="^FAIL"` starts at the first line matching a regexp, `before=N` adds lines above it, and `lines=N` diff --git a/README.md b/README.md index 29e88a3d..a952d310 100644 --- a/README.md +++ b/README.md @@ -488,6 +488,7 @@ running it, so `setopt ignore_close_braces` in the block can't help. | `out=markdown` | Insert output as raw markdown | | `out=image[:path]` | Output is image bytes (PNG, JPEG, GIF, SVG): save it and link it | | `out=screenshot[:path]` | Draw the text output as a terminal-style image: SVG, or PNG if the path ends in `.png` | +| `out=file[:path]` | Write stdout to a file (default `.assets/.log`) and show a window of it; see Long output in a file below | | `embed=inline` | With `out=image` or `out=screenshot`, embed a data URI instead of a file | | `history[=N]` | Keep the block's N earlier runs under its output, newest first (bare: the front matter's `history:`, else 10); see Earlier runs below | @@ -524,6 +525,37 @@ GITHUB_TOKEN is not set output. A copy keeps what it holds when its block doesn't run, and mdsh warns about a copy for a block that doesn't exist. +## Long output in a file + +A block that prints a build log or a test run swamps the document. +`out=file` sends its stdout to `.assets/.log`, or the path given +(`out=file:res://build.log`, `out=file:logs/build.txt`), and its output +fence becomes an include of that file: + +````markdown +```sh {#build out=file} +make 2>&1 +``` + +```output {for=build exit=2 .assets/.log` (or the + path given), and its output fence becomes an include of the file: the + first `fold_output` lines, or the window you pick by adding `match=`, + `before=`, `lines=`, `until=`, `from=` or `to=` to the fence, which + every run keeps. So a long build log stays out of the document, and + the document points at the part that matters. Copies keep their own + windows, includes of the log show the latest run, the file is + rewritten only when it changed, and `history=N` keeps earlier logs. + + ![A test block with out=file: its output fence shows the first 25 lines of the log, and a copy under "The first failure" shows the lines around the first --- FAIL](docs/changelog/spill-output.png) + - An include block can find its lines by what they hold, so it stays on them when the file changes above: `match="^FAIL"` starts at the first line matching a regexp, `before=N` adds lines above it, and `lines=N` diff --git a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/README.md b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/README.md index 29e88a3d..a952d310 100644 --- a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/README.md +++ b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/README.md @@ -488,6 +488,7 @@ running it, so `setopt ignore_close_braces` in the block can't help. | `out=markdown` | Insert output as raw markdown | | `out=image[:path]` | Output is image bytes (PNG, JPEG, GIF, SVG): save it and link it | | `out=screenshot[:path]` | Draw the text output as a terminal-style image: SVG, or PNG if the path ends in `.png` | +| `out=file[:path]` | Write stdout to a file (default `.assets/.log`) and show a window of it; see Long output in a file below | | `embed=inline` | With `out=image` or `out=screenshot`, embed a data URI instead of a file | | `history[=N]` | Keep the block's N earlier runs under its output, newest first (bare: the front matter's `history:`, else 10); see Earlier runs below | @@ -524,6 +525,37 @@ GITHUB_TOKEN is not set output. A copy keeps what it holds when its block doesn't run, and mdsh warns about a copy for a block that doesn't exist. +## Long output in a file + +A block that prints a build log or a test run swamps the document. +`out=file` sends its stdout to `.assets/.log`, or the path given +(`out=file:res://build.log`, `out=file:logs/build.txt`), and its output +fence becomes an include of that file: + +````markdown +```sh {#build out=file} +make 2>&1 +``` + +```output {for=build exit=2 .gif"}, {Name: "frame", Kind: AttrNumber, Default: "the last", Help: "With tty, or on a cast fence, the frame of the recording that still views show and the player opens at, counting from 1; mdsh-gui sets it where you leave the player"}, - {Name: "out", Kind: AttrChoice, Values: []string{"text", "markdown", "image", "image:", "screenshot", "screenshot:", "recording:"}, Default: "text", Help: "How output is inserted: fenced text, raw markdown, or an image saved beside the file"}, + {Name: "out", Kind: AttrChoice, Values: []string{"text", "markdown", "image", "image:", "screenshot", "screenshot:", "recording:", "file", "file:"}, Default: "text", Help: "How output is inserted: fenced text, raw markdown, an image saved beside the file, or a file in the assets folder that the output fence shows a window of"}, {Name: "raw", Kind: AttrFlag, Default: "off", Help: "On an http block, save the response body as it came instead of pretty-printing JSON"}, {Name: "embed", Kind: AttrChoice, Values: []string{"inline"}, Default: "off", Help: "With out=image or out=screenshot, write a data URI instead of a file"}, {Name: IncludeAttr, Kind: AttrFlag, Include: true, Help: "Fill the block with lines from another file; it never runs"}, diff --git a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/doc.go b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/doc.go index 9e17402e..caa0e549 100644 --- a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/doc.go +++ b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/doc.go @@ -70,6 +70,10 @@ type Block struct { // outMain and outErr are set once Output holds a stdout part (a // fence or region) and a stderr fence. Another one is a copy. outMain, outErr bool + // OutWindow is the window attributes (see WindowAttrs) of the + // fence of b's current stdout output: for out=file, what part of the + // file it shows, which write-back keeps. + OutWindow Attrs // outNoSave is set when a fence in Output is marked nosave: the // output is kept as the file has it, as if the block were. outNoSave bool @@ -127,6 +131,9 @@ type Copy struct { // NoSave is set for a copy fence marked nosave: write-back keeps // what it holds, whatever its block does. NoSave bool + // Window is the copy fence's window attributes (see WindowAttrs): + // for a copy of out=file output, what part of the file it shows. + Window Attrs } // HasOutput reports whether output is already saved after b in the file. @@ -287,6 +294,12 @@ func parseLines(d *Document, pos, stop int, c counts) (map[string]bool, error) { last.Output.End = next last.addPart(b.Attrs["stream"] == "stderr") last.addCurrentPart(Span{pos, next}, b.Attrs["at"], b.Attrs["stream"] == "stderr") + if b.Attrs["stream"] != "stderr" { + last.OutWindow = windowOf(b.Attrs) + } + if err := checkWindow(b); err != nil { + return nil, fmt.Errorf("line %d: %w", lineNo(src, pos), err) + } if b.Attrs.Has("nosave") { last.outNoSave = true } @@ -296,8 +309,11 @@ func parseLines(d *Document, pos, stop int, c counts) (map[string]bool, error) { if b.Lang == "output" { // Output away from its block is a copy, part of the // prose. An unclosed one is just prose. + if err := checkWindow(b); err != nil { + return nil, fmt.Errorf("line %d: %w", lineNo(src, pos), err) + } if b.BodySpan.End < b.Span.End && b.Attrs["for"] != "" { - d.Copies = append(d.Copies, &Copy{For: b.Attrs["for"], Stream: b.Attrs["stream"], Span: b.Span, Prefix: b.Prefix, Lead: b.Lead, NoSave: b.Attrs.Has("nosave")}) + d.Copies = append(d.Copies, &Copy{For: b.Attrs["for"], Stream: b.Attrs["stream"], Span: b.Span, Prefix: b.Prefix, Lead: b.Lead, NoSave: b.Attrs.Has("nosave"), Window: windowOf(b.Attrs)}) } last = nil pos = next @@ -342,6 +358,9 @@ func parseLines(d *Document, pos, stop int, c counts) (map[string]bool, error) { if err := checkHistoryAttr(b); err != nil { return nil, fmt.Errorf("line %d: %w", lineNo(src, pos), err) } + if err := checkSpill(b); err != nil { + return nil, fmt.Errorf("line %d: %w", lineNo(src, pos), err) + } if v := b.Attrs["var"]; v != "" && b.IsInput() { // Only one input sets a variable: the first. A later // one that is the same control is a copy of it, which diff --git a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/name.go b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/name.go index 73f7c4a0..eb576cac 100644 --- a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/name.go +++ b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/doc/name.go @@ -120,7 +120,9 @@ func (d *Document) rename(renames []Rename, named map[*Block]string, links bool) rewrite{regexp.MustCompile(`\{\{#` + old + `/`), "{{#" + r.New + "/"}, rewrite{regexp.MustCompile(`(for=)("?)` + old + `("|[\s}]|$)`), "${1}${2}" + r.New + "${3}"}) if links { - assets = append(assets, rewrite{regexp.MustCompile(`(\.assets/)` + old + `\.`), "${1}" + r.New + "."}) + assets = append(assets, + rewrite{regexp.MustCompile(`(\.assets/)` + old + `\.`), "${1}" + r.New + "."}, + rewrite{regexp.MustCompile(`( 0 { + a["lines"] = fmt.Sprint(lines) + } + } + return (&Block{Attrs: a}).Include() +} + +// checkSpill checks the attributes out=file can't go with. +func checkSpill(b *Block) error { + if !b.SpillsOutput() { + return nil + } + switch { + case b.Attrs.Has("secret"): + return fmt.Errorf("block %s: out=file can't go with secret: the file would hold the output unmasked", b.ID) + case b.Attrs.Has("tty"): + return fmt.Errorf("block %s: a tty block's output is a recording, so it can't have out=file", b.ID) + case b.IsHTTP(): + return fmt.Errorf("block %s: an http block's output is the response, so it can't have out=file", b.ID) + } + return nil +} + +// checkWindow checks the window attributes of output fence f, when it is +// an include of an out=file file. +func checkWindow(f *Block) error { + if !f.Attrs.Has(IncludeAttr) { + return nil + } + if _, err := SpillInclude("file", windowOf(f.Attrs), 0); err != nil { + return fmt.Errorf("output for %s: %w", f.Attrs["for"], err) + } + return nil +} diff --git a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/changelog/spill-output.png b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/changelog/spill-output.png new file mode 100644 index 00000000..a27f0e42 Binary files /dev/null and b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/changelog/spill-output.png differ diff --git a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/cheatsheet.md b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/cheatsheet.md index c21ed63e..dbf0a1ea 100644 --- a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/cheatsheet.md +++ b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/docs/cheatsheet.md @@ -104,6 +104,7 @@ other languages are left alone. Unknown attributes are kept as written. | `out=markdown` | Output is inserted as markdown, between `` and `` | | `out=image[:path]` | Output is image bytes (PNG, JPEG, GIF, SVG), saved beside the file and linked | | `out=screenshot[:path]` | The text output drawn as a terminal-style image: SVG, or PNG when the path ends in `.png` | +| `out=file[:path]` | stdout to a file (`.assets/.log`); the output fence is an include of it, whose window attributes (`match=`, `lines=`…) you set and mdsh keeps | | `out=recording:path` | With `tty`, where the cast goes; a path ending in `.gif` links an animated GIF too | | `embed=inline` | With `out=image` or `out=screenshot`, a data URI instead of a file | | `raw` | On an `http` block, save the response body as it came instead of pretty-printing JSON | diff --git a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/render/history.go b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/render/history.go index 960cb525..f9749ba5 100644 --- a/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/render/history.go +++ b/cmd/mdsh-gui/vendor/github.com/gdey/markdownshell/render/history.go @@ -83,7 +83,22 @@ func entryText(src []byte, parts []doc.Span, k int, at string, rename bool) stri } first, rest := text[:nl+1], text[nl+1:] region := strings.Contains(first, "` and `` | | `out=image[:path]` | Output is image bytes (PNG, JPEG, GIF, SVG), saved beside the file and linked | | `out=screenshot[:path]` | The text output drawn as a terminal-style image: SVG, or PNG when the path ends in `.png` | +| `out=file[:path]` | stdout to a file (`.assets/.log`); the output fence is an include of it, whose window attributes (`match=`, `lines=`…) you set and mdsh keeps | | `out=recording:path` | With `tty`, where the cast goes; a path ending in `.gif` links an animated GIF too | | `embed=inline` | With `out=image` or `out=screenshot`, a data URI instead of a file | | `raw` | On an `http` block, save the response body as it came instead of pretty-printing JSON | diff --git a/docs/spec.md b/docs/spec.md index 923f062f..2be4b317 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -89,11 +89,11 @@ Block attributes in v1: | `background` | Start the block and go on to the next; its output is kept apart, and the run waits for it at the end | off | | `tty` | Run on the terminal, for commands that need one (`docker run -it`); the session is saved as an asciinema cast, which mdsh-gui plays | off | | `frame=N` | With `tty`, the frame of the GIF that still views (PDF, and later the TUI and GUI) show, counting from 1 | the last | -| `out=text\|markdown\|image[:path]\|screenshot[:path]` | How output is inserted | `text` | +| `out=text\|markdown\|image[:path]\|screenshot[:path]\|file[:path]` | How output is inserted | `text` | | `raw` | On an `http` block, save the response body as it came instead of pretty-printing JSON | off | | `embed=inline` | With `out=image` or `out=screenshot`, write a data URI instead of a file | off | -**Output kinds (`out=`).** `text` fences the output. `markdown` inserts it raw, between `` and `` so a re-run can replace it. `image` expects stdout to be image bytes (PNG, JPEG, GIF, SVG) and fails otherwise. `screenshot` draws stdout as a terminal-style image (ANSI escapes dropped), as SVG by default or PNG when the path ends in `.png`; PNGs use the bundled Go Mono font so they look the same everywhere. For both, mdsh saves the image to `.assets/.`, and inserts a markdown image link between the same markers. `out=image:plots/sales.png` picks the path, relative to the document; `out=image:res://sales.png` is `.assets/sales.png`, as for includes. The path must stay inside the document's root, as for includes, and a symlink at it is replaced, not written through. Files beside the doc are the default because GitHub doesn't render `data:` image URIs; `embed=inline` writes a data URI anyway for viewers that do. Showing images inline in terminals that support it (kitty, iTerm2, sixel) is planned, not built. +**Output kinds (`out=`).** `text` fences the output. `markdown` inserts it raw, between `` and `` so a re-run can replace it. `image` expects stdout to be image bytes (PNG, JPEG, GIF, SVG) and fails otherwise. `screenshot` draws stdout as a terminal-style image (ANSI escapes dropped), as SVG by default or PNG when the path ends in `.png`; PNGs use the bundled Go Mono font so they look the same everywhere. For both, mdsh saves the image to `.assets/.`, and inserts a markdown image link between the same markers. `out=image:plots/sales.png` picks the path, relative to the document; `out=image:res://sales.png` is `.assets/sales.png`, as for includes. The path must stay inside the document's root, as for includes, and a symlink at it is replaced, not written through. Files beside the doc are the default because GitHub doesn't render `data:` image URIs; `embed=inline` writes a data URI anyway for viewers that do. Showing images inline in terminals that support it (kitty, iTerm2, sixel) is planned, not built. `file` writes stdout to `.assets/.log`, or the path `out=file:path` names under the same rules, and the output fence is an include of it: `output {for=id exit=N [exited] [truncated] at=… .` in its output fence and copies. **Output copies.** The output right after a block is its output: mdsh replaces it on every run, reads `exit=` from it, and adds one there if it is missing. An `output {for=id}` fence anywhere else, or an `` region (for `out=markdown`, image and recording output), is a copy: like a `{{$VAR}}` placeholder, mdsh refills it with block `id`'s output on every run, so a summary near the end of a document can repeat what an earlier block found. A copy with `stream=stderr` gets the stderr fence instead, shown by the block's usual rules. The output right after a block holds at most one stdout part and one stderr fence; a second one is a copy too. A copy is rewritten whole, keeping its own place, list item or block quote, and mdsh writes the kind of output the block has now (a fence or a region) whatever kind the copy was. When the block doesn't run (skipped, `run=` false, `--only`, an earlier stop, `--no-run`) the copy keeps what it holds. When the block has nothing to show for that part (no stdout, or stderr hidden), write-back stores an empty copy so a later run can fill it, and printing leaves it out. Copies are output: `--hide-output` and the block's `show-output=` hide them when printing. A copy whose `for=` names no block is left as it is and mdsh warns about it. While mdsh prints as it runs, a copy above its block is printed before the block runs, so it shows what it held; `-w`, `-o` and `--pdf` are always up to date. diff --git a/render/history.go b/render/history.go index 960cb525..f9749ba5 100644 --- a/render/history.go +++ b/render/history.go @@ -83,7 +83,22 @@ func entryText(src []byte, parts []doc.Span, k int, at string, rename bool) stri } first, rest := text[:nl+1], text[nl+1:] region := strings.Contains(first, "