Add the notify and ntfy modules and scoped tokens
Breaking: a job with a raw element prints only with a token issued with uberbringer token --raw. Every other token, every existing one included, gets 403 token_scope for it, with the raw elements in details, and keeps every other route. The notify module prints a notification card from a title, message, priority and tags, with topic, click, up to three actions, image, markdown and time as options; it decodes strictly and refuses a payload over its length caps. The ntfy module takes one ntfy message as ntfy sends it, such as the ntfy client's $NTFY_RAW, ignores fields it does not know, cuts text over the caps, and answers 204 for other events and binary bodies. Both print every message plain: one message cannot tell the first of a sequence from a replacement, since a publisher may choose the sequence ID. internal/pkg/ntfy holds ntfy's message types. Both print the same card. The priority picks the form: a dark URGENT rule and a double box, a double box headed HIGH, a single box with the title in its border, or a rule with no box. Tags print as markers in front of the title, the rest on a Tags row; links print as QR codes with their labels; attached PNG, JPEG, GIF and WebP images print after the card. A line-level markdown subset is laid out without a dependency, keeping marks inside code spans and URLs, and emoji are removed after a few typed ones become markers. A card that cannot be laid out prints as a plain receipt, and the server logs the fault once at ERROR and reports it to Sentry once. [ntfy.format] and [ntfy.markers] shape the cards; the markers table is a setting with the user's own keys, also settable as UBERBRINGER_NTFY_MARKERS=name=value,... uberbringer token --modules issues a token limited to those modules' print and preview routes, job status and the module list; each route in the table says which tokens it takes, and a scope claim that could read as absent makes the token invalid. image elements take optional, which also shrinks a too-wide image to the paper; a QR code that could be too wide for the paper prints with smaller modules; box takes title_bold. Submit takes a Meta whose source shows in job status and, with a copy of the request ID, in every worker log line of the job. The four ntfy cards passed on the TM-T88IV, posted as the client recipe posts them, and their bytes are pinned in TestPaperReceipts. docs/api.md describes the modules, the card and the client recipe, with tokens read from a header file; docs/swagger.json is regenerated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
fa15cb27b1
commit
1335ed0687
119 files changed
+5626
-468
No files matched your search
@@ -68,11 +68,13 @@ var fields = []field{
|
||||
{"hri", func(e Element) bool { return e.HRI != "" }},
|
||||
{"url", func(e Element) bool { return e.URL != "" }},
|
||||
{"base64", func(e Element) bool { return e.Base64 != "" }},
|
||||
{"optional", func(e Element) bool { return e.Optional }},
|
||||
{"hex", func(e Element) bool { return e.Hex != "" }},
|
||||
{"mode", func(e Element) bool { return e.Mode != "" }},
|
||||
{"children", func(e Element) bool { return e.Children != nil }},
|
||||
{"title", func(e Element) bool { return e.Title != "" }},
|
||||
{"title_style", func(e Element) bool { return e.TitleStyle != "" }},
|
||||
{"title_bold", func(e Element) bool { return e.TitleBold }},
|
||||
{"footer", func(e Element) bool { return e.Footer != "" }},
|
||||
{"style", func(e Element) bool { return e.Style != "" }},
|
||||
{"padding", func(e Element) bool { return e.Padding != nil }},
|
||||
@@ -86,11 +88,16 @@ var fields = []field{
|
||||
}
|
||||
|
||||
// blockOnly are the fields no other type takes.
|
||||
var blockOnly = fieldSet("children title title_style footer style padding fit columns pairs leader items bullet done")
|
||||
var blockOnly = fieldSet("children title title_style title_bold footer style padding fit columns pairs leader items bullet done")
|
||||
|
||||
// ownFields maps each field that only one of the other types takes to that
|
||||
// type. The rest of the other types refuse it, as they refuse the block
|
||||
// fields.
|
||||
var ownFields = map[string]string{"optional": TypeImage}
|
||||
|
||||
// blockFields are the fields each block takes at the top level.
|
||||
var blockFields = map[string]map[string]bool{
|
||||
TypeBox: fieldSet("text bold underline invert align size font children title title_style footer style padding fit columns"),
|
||||
TypeBox: fieldSet("text bold underline invert align size font children title title_style title_bold footer style padding fit columns"),
|
||||
TypeRule: fieldSet("text bold align size font style columns"),
|
||||
TypeKV: fieldSet("pairs leader size font columns"),
|
||||
TypeList: fieldSet("items bullet done size font columns"),
|
||||
@@ -118,7 +125,8 @@ func fieldSet(names string) map[string]bool {
|
||||
|
||||
// checkFields reports fields set on el that its type does not take. The
|
||||
// blocks and the children of a box take only their own fields; the other
|
||||
// types ignore unrelated fields, except the block fields.
|
||||
// types ignore unrelated fields, except the block fields and the fields
|
||||
// another type owns.
|
||||
func (el Element) checkFields(inBox bool) []string {
|
||||
allowed, strict := blockFields[el.Type]
|
||||
if inBox {
|
||||
@@ -138,7 +146,8 @@ func (el Element) checkFields(inBox bool) []string {
|
||||
problems = append(problems, fmt.Sprintf("%s is inherited from the box", f.name))
|
||||
case inBox && !allowed[f.name]:
|
||||
problems = append(problems, fmt.Sprintf("field %q does not apply to %s inside a box", f.name, el.Type))
|
||||
case strict && !allowed[f.name], !strict && blockOnly[f.name]:
|
||||
case strict && !allowed[f.name], !strict && blockOnly[f.name],
|
||||
!strict && ownFields[f.name] != "" && ownFields[f.name] != el.Type:
|
||||
problems = append(problems, fmt.Sprintf("field %q does not apply to %s", f.name, el.Type))
|
||||
}
|
||||
}
|
||||
@@ -166,6 +175,7 @@ func checkColumns(columns int) error {
|
||||
type boxSpec struct {
|
||||
style boxStyle
|
||||
capped bool
|
||||
title textStyle
|
||||
text textStyle
|
||||
align escpos.Justification
|
||||
padding int
|
||||
@@ -177,6 +187,7 @@ type boxSpec struct {
|
||||
|
||||
func (el Element) boxSpec() (boxSpec, error) {
|
||||
s := boxSpec{
|
||||
title: textStyle{bold: el.TitleBold},
|
||||
text: textStyle{bold: el.Bold, invert: el.Invert},
|
||||
padding: defaultPadding,
|
||||
fit: el.Fit,
|
||||
|
||||
@@ -140,7 +140,7 @@ func (lo *layout) box(el Element, spec boxSpec, cols int) []blockLine {
|
||||
padding = min(padding, (width-minBoxWidth)/2)
|
||||
content := width - 2 - 2*padding
|
||||
|
||||
lines := []blockLine{set.top(title, spec.capped, width)}
|
||||
lines := []blockLine{set.top(title, spec.title, spec.capped, width)}
|
||||
for _, it := range items {
|
||||
if it.kind == itemDivider {
|
||||
lines = append(lines, lo.divider(set, it, width))
|
||||
@@ -285,8 +285,8 @@ func (it boxItem) contentLines(width int) []blockLine {
|
||||
return lines
|
||||
}
|
||||
|
||||
// top draws the top border, with the title set into it.
|
||||
func (b borders) top(title string, capped bool, width int) blockLine {
|
||||
// top draws the top border, with the title set into it in style.
|
||||
func (b borders) top(title string, style textStyle, capped bool, width int) blockLine {
|
||||
h := string(b.horizontal)
|
||||
inner := width - 2
|
||||
room := inlineTitleRoom
|
||||
@@ -306,7 +306,7 @@ func (b borders) top(title string, capped bool, width int) blockLine {
|
||||
rest := width - runeLen(head) - runeLen(title) - runeLen(tail) - 1
|
||||
return blockLine{
|
||||
plain(head),
|
||||
plain(title),
|
||||
{text: title, style: style},
|
||||
plain(tail + fill(h, rest) + string(b.topRight)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,6 +181,32 @@ func TestBlockStyleRuns(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoxTitleBold(t *testing.T) {
|
||||
p, err := newPreviewer(t, "tm-t88", escpos.CodePage437).Preview(context.Background(), receipt.Job{Elements: []receipt.Element{
|
||||
{Type: "box", Title: "Title", TitleBold: true, Text: "x"},
|
||||
{Type: "box", Title: "Capped", TitleStyle: "capped", TitleBold: true, Style: "double", Text: "x"},
|
||||
}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bold := escpos.Run{Bold: true, Font: escpos.FontA, W: 1, H: 1}
|
||||
tests := []struct {
|
||||
line int
|
||||
text string
|
||||
want []escpos.Run
|
||||
}{
|
||||
{0, "┌─ Title ", []escpos.Run{withSpan(bold, 3, 8)}},
|
||||
{1, "│ x ", nil},
|
||||
{3, "╔═╡ Capped ╞", []escpos.Run{withSpan(bold, 4, 10)}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
line := p.Lines[tt.line]
|
||||
if !strings.HasPrefix(line.Text, tt.text) || fmt.Sprint(line.Runs) != fmt.Sprint(tt.want) {
|
||||
t.Errorf("line %d %q runs = %+v, want %q... with %+v", tt.line, line.Text, line.Runs, tt.text, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func withSpan(r escpos.Run, from, to int) escpos.Run {
|
||||
r.From, r.To = from, to
|
||||
return r
|
||||
@@ -287,6 +313,7 @@ func TestPreview(t *testing.T) {
|
||||
{Type: "raw", Hex: "1B 61 01 07"},
|
||||
{Type: "image", URL: "https://images.example/logo.png?size=2", Width: 384},
|
||||
{Type: "image", Base64: "iVBORw0KGgo=", Width: 600},
|
||||
{Type: "image", URL: "https://images.example/wide.png", Width: 600, Optional: true},
|
||||
{Type: "qr", Data: "https://example.com"},
|
||||
{Type: "barcode", Symbology: "ean8", Data: "9638507"},
|
||||
{Type: "cut", Mode: "full"},
|
||||
@@ -303,6 +330,7 @@ func TestPreview(t *testing.T) {
|
||||
"[raw 4 bytes]",
|
||||
"[image 384 dots: logo.png]",
|
||||
"[image 600 dots: base64]",
|
||||
"[image 512 dots: wide.png]",
|
||||
"[qr: https://example.com]",
|
||||
"[barcode ean8: 9638507]",
|
||||
" - - - - - - - - full cut - - - - - - - -",
|
||||
@@ -315,8 +343,8 @@ func TestPreview(t *testing.T) {
|
||||
if p.Lines[2].Data != "https://images.example/logo.png?size=2" || p.Lines[3].Data != "base64" {
|
||||
t.Errorf("image data = %q, %q", p.Lines[2].Data, p.Lines[3].Data)
|
||||
}
|
||||
if p.Columns != 42 || p.CodePage != escpos.CodePage437 || p.LineCount != 10 {
|
||||
t.Errorf("columns %d, code page %s, %d lines; want 42, cp437 and 10", p.Columns, p.CodePage, p.LineCount)
|
||||
if p.Columns != 42 || p.CodePage != escpos.CodePage437 || p.LineCount != 12 {
|
||||
t.Errorf("columns %d, code page %s, %d lines; want 42, cp437 and 12", p.Columns, p.CodePage, p.LineCount)
|
||||
}
|
||||
wantWarnings := []string{
|
||||
"elements[0]: 1 character not in cp437 printed as ?",
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -74,11 +75,17 @@ func TestRenderCompat(t *testing.T) {
|
||||
var updatePaper = flag.Bool("update-paper", false, "rewrite testdata/paper/*.hex from the current renderer")
|
||||
|
||||
// paperReceipts are jobs whose printed result was checked on the TM-T88IV,
|
||||
// with the file their bytes are pinned in. The bytes are rendered for a
|
||||
// TM-T88 in CP437 with the default options.
|
||||
var paperReceipts = []struct{ job, hex string }{
|
||||
{filepath.Join("..", "..", "..", "examples", "blocks.json"), filepath.Join("testdata", "paper", "blocks.hex")},
|
||||
{filepath.Join("testdata", "paper", "note.json"), filepath.Join("testdata", "paper", "note.hex")},
|
||||
// with the file their bytes are pinned in and, for a job with an image
|
||||
// URL, the image it was printed with. The bytes are rendered for a TM-T88
|
||||
// in CP437 with the default options.
|
||||
var paperReceipts = []struct{ job, hex, image string }{
|
||||
{job: filepath.Join("..", "..", "..", "examples", "blocks.json"), hex: filepath.Join("testdata", "paper", "blocks.hex")},
|
||||
{job: filepath.Join("testdata", "paper", "note.json"), hex: filepath.Join("testdata", "paper", "note.hex")},
|
||||
{job: filepath.Join("testdata", "paper", "ntfy-high.json"), hex: filepath.Join("testdata", "paper", "ntfy-high.hex"),
|
||||
image: filepath.Join("testdata", "paper", "graph.png")},
|
||||
{job: filepath.Join("testdata", "paper", "ntfy-urgent.json"), hex: filepath.Join("testdata", "paper", "ntfy-urgent.hex")},
|
||||
{job: filepath.Join("testdata", "paper", "ntfy-default.json"), hex: filepath.Join("testdata", "paper", "ntfy-default.hex")},
|
||||
{job: filepath.Join("testdata", "paper", "ntfy-min.json"), hex: filepath.Join("testdata", "paper", "ntfy-min.hex")},
|
||||
}
|
||||
|
||||
// TestPaperReceipts fails when the bytes of a receipt checked on paper
|
||||
@@ -90,17 +97,27 @@ func TestPaperReceipts(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fetcher := &fakeFetcher{err: errors.New("the job has no image")}
|
||||
if receiptFile.image != "" {
|
||||
if fetcher.data, err = os.ReadFile(receiptFile.image); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fetcher.err = nil
|
||||
}
|
||||
r := receipt.NewRenderer(receipt.Options{
|
||||
Profile: profile,
|
||||
CodePage: escpos.CodePage437,
|
||||
MaxPixels: 25_000_000,
|
||||
MaxHeight: 4000,
|
||||
MaxLines: 2000,
|
||||
}, &fakeFetcher{})
|
||||
}, fetcher)
|
||||
got, err := r.Render(context.Background(), readJob(t, receiptFile.job))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Contains(got, []byte("[image unavailable]")) {
|
||||
t.Fatal("an image of the receipt did not load")
|
||||
}
|
||||
if *updatePaper {
|
||||
if err := os.MkdirAll(filepath.Dir(receiptFile.hex), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -87,7 +87,9 @@ type Element struct {
|
||||
Bold bool `json:"bold,omitempty"`
|
||||
// Size sets width and height together: 1-8 for text, 1-3 for a block,
|
||||
// whose borders scale with its text. For a QR code it is the module size
|
||||
// in dots, 1-16, default 6. 0 means the default.
|
||||
// in dots, 1-16, default 6, made smaller for a code that could be wider
|
||||
// than the paper, its width worked out as if its data were bytes. 0
|
||||
// means the default.
|
||||
Size int `json:"size,omitempty" jsonschema:"minimum=0,maximum=16"`
|
||||
// Width is the character width for text (1-8), the module width of a
|
||||
// barcode (2-6, default 3), or the target width of an image in dots. 0
|
||||
@@ -132,6 +134,10 @@ type Element struct {
|
||||
// Base64 is an image's data in base64, or a data:image/...;base64, URI:
|
||||
// PNG, JPEG, GIF or WebP.
|
||||
Base64 string `json:"base64,omitempty"`
|
||||
// Optional prints the line [image unavailable] in place of an image
|
||||
// that cannot be fetched or decoded, instead of failing the whole job,
|
||||
// and shrinks an image whose width is wider than the paper to it.
|
||||
Optional bool `json:"optional,omitempty"`
|
||||
|
||||
// Hex is a raw element's ESC/POS bytes in hex, spaces allowed, e.g.
|
||||
// "1B 61 01". It is sent as given.
|
||||
@@ -147,6 +153,8 @@ type Element struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
// TitleStyle is inline, the default, or capped.
|
||||
TitleStyle string `json:"title_style,omitempty" jsonschema:"enum=inline,enum=capped"`
|
||||
// TitleBold prints a box's title bold.
|
||||
TitleBold bool `json:"title_bold,omitempty"`
|
||||
// Footer is text set into a box's bottom border, on the right.
|
||||
Footer string `json:"footer,omitempty"`
|
||||
// Style is a box's border style (single, double, double-h, double-v or
|
||||
|
||||
@@ -104,6 +104,61 @@ func foldFor(r rune, cp escpos.CodePage) (string, bool) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
// codePages are the code pages a printer may be set to.
|
||||
var codePages = []escpos.CodePage{escpos.CodePage437, escpos.CodePage850, escpos.CodePage858, escpos.CodePage1252}
|
||||
|
||||
// StripEmoji removes the emoji from s, which normalize would print as '?':
|
||||
// each emoji sequence, with its selectors, skin tones and the emoji joined
|
||||
// to it, and each flag. Symbols among the emoji that a code page prints or
|
||||
// folds to a look-alike, such as ■, → and ✓, stay. So that a removed emoji
|
||||
// leaves no gap, the spaces after it go when the text before it is empty or
|
||||
// ends in a space, and spaces before it go when it ends a line.
|
||||
func StripEmoji(s string) string {
|
||||
runes := []rune(s)
|
||||
out := make([]rune, 0, len(runes))
|
||||
removed := false
|
||||
trim := func() {
|
||||
for len(out) > 0 && out[len(out)-1] == ' ' {
|
||||
out = out[:len(out)-1]
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(runes); i++ {
|
||||
r := runes[i]
|
||||
switch {
|
||||
case invisible(r):
|
||||
continue
|
||||
case pictographic(r) && !keptSymbol(r):
|
||||
i = emojiSequenceEnd(runes, i)
|
||||
removed = true
|
||||
continue
|
||||
case r == ' ' && removed && (len(out) == 0 || out[len(out)-1] == ' ' || out[len(out)-1] == '\n'):
|
||||
continue
|
||||
case r == '\n' && removed:
|
||||
trim()
|
||||
}
|
||||
removed = false
|
||||
out = append(out, r)
|
||||
}
|
||||
if removed {
|
||||
trim()
|
||||
}
|
||||
return string(out)
|
||||
}
|
||||
|
||||
// keptSymbol reports whether StripEmoji keeps r: a code page prints it, or
|
||||
// it folds to a look-alike.
|
||||
func keptSymbol(r rune) bool {
|
||||
if _, ok := folds[r]; ok {
|
||||
return true
|
||||
}
|
||||
for _, cp := range codePages {
|
||||
if cp.CanEncode(r) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// invisible reports whether r only changes how the characters around it
|
||||
// look: variation selectors, zero-width spaces and joiners, emoji skin
|
||||
// tones, tag characters and the keycap mark.
|
||||
|
||||
@@ -74,6 +74,36 @@ func TestNormalizeLeavesPrintableTextAlone(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripEmoji(t *testing.T) {
|
||||
tests := []struct{ in, want string }{
|
||||
{"plain text", "plain text"},
|
||||
{"Party 🎉", "Party"},
|
||||
{"🎉 Party", "Party"},
|
||||
{"Backup 🎉 done", "Backup done"},
|
||||
{"Backup 🎉 done", "Backup done"},
|
||||
{"a🎉b", "ab"},
|
||||
{"Done 🎉\nNext 🎉 line", "Done\nNext line"},
|
||||
{"👍🏽 skin tone", "skin tone"},
|
||||
{"👨\u200D👩\u200D👧 family", "family"},
|
||||
{"🇬🇧 flag", "flag"},
|
||||
{"🏴\U000E0067\U000E0062\U000E0065\U000E006E\U000E0067\U000E007F tag sequence", "tag sequence"},
|
||||
{"⚠\uFE0F careful ❤\uFE0F", "careful"},
|
||||
{"1\uFE0F\u20E3 keycap", "1 keycap"},
|
||||
{"zero\u200Bwidth", "zerowidth"},
|
||||
// Symbols a code page prints or folds stay.
|
||||
{"■ → ← ✓ ✔ ⌐ √ ° ·", "■ → ← ✓ ✔ ⌐ √ ° ·"},
|
||||
{"box ┌─┐ ░▒▓█", "box ┌─┐ ░▒▓█"},
|
||||
{"✔\uFE0F checked", "✔ checked"},
|
||||
{"", ""},
|
||||
{"🎉", ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := StripEmoji(tt.in); got != tt.want {
|
||||
t.Errorf("StripEmoji(%q) = %q, want %q", tt.in, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRunsBeforeWrap(t *testing.T) {
|
||||
// The ellipsis takes three columns, so "word…" no longer fits in 6.
|
||||
text, _ := normalize("ab word…", escpos.CodePage437)
|
||||
|
||||
@@ -53,7 +53,7 @@ func (r *Renderer) Preview(ctx context.Context, job Job) (Preview, error) {
|
||||
if err != nil {
|
||||
return Preview{}, fmt.Errorf("reading back the rendered receipt: %w", err)
|
||||
}
|
||||
labelImages(lines, job)
|
||||
labelImages(lines, job, r.options.Profile.DotsPerLine)
|
||||
|
||||
var warnings []string
|
||||
for i := range job.Elements {
|
||||
@@ -79,7 +79,8 @@ func (r *Renderer) Preview(ctx context.Context, job Job) (Preview, error) {
|
||||
|
||||
// placeholderImages returns a blank one-row raster across the paper for
|
||||
// every image element, and a warning for each image that would fail to
|
||||
// print because it is wider than the paper.
|
||||
// print because it is wider than the paper. An optional image that is
|
||||
// wider shrinks to the paper instead.
|
||||
func (r *Renderer) placeholderImages(job Job) (map[int]*image.Paletted, map[int]string) {
|
||||
paper := r.options.Profile.DotsPerLine
|
||||
images := make(map[int]*image.Paletted)
|
||||
@@ -88,7 +89,7 @@ func (r *Renderer) placeholderImages(job Job) (map[int]*image.Paletted, map[int]
|
||||
if el.Type != TypeImage {
|
||||
continue
|
||||
}
|
||||
if el.Width > paper {
|
||||
if el.Width > paper && !el.Optional {
|
||||
warnings[i] = fmt.Sprintf("elements[%d] (image): width %d is wider than the %d-dot paper, so the job would fail", i, el.Width, paper)
|
||||
}
|
||||
images[i] = image.NewPaletted(image.Rect(0, 0, paper, 1), color.Palette{color.White, color.Black})
|
||||
@@ -97,8 +98,9 @@ func (r *Renderer) placeholderImages(job Job) (map[int]*image.Paletted, map[int]
|
||||
}
|
||||
|
||||
// labelImages replaces the size of each image placeholder with the source
|
||||
// of its element, in order, copy after copy.
|
||||
func labelImages(lines []escpos.PreviewLine, job Job) {
|
||||
// of its element, in order, copy after copy. An optional image wider than
|
||||
// the paper, paper dots, is labelled with the width it prints at.
|
||||
func labelImages(lines []escpos.PreviewLine, job Job, paper int) {
|
||||
var sources []Element
|
||||
for _, el := range job.Elements {
|
||||
if el.Type == TypeImage {
|
||||
@@ -117,8 +119,11 @@ func labelImages(lines []escpos.PreviewLine, job Job) {
|
||||
name, data = imageName(el.URL), el.URL
|
||||
}
|
||||
lines[i].Text = fmt.Sprintf("[image: %s]", name)
|
||||
if el.Width > 0 {
|
||||
lines[i].Text = fmt.Sprintf("[image %d dots: %s]", el.Width, name)
|
||||
if width := el.Width; width > 0 {
|
||||
if el.Optional {
|
||||
width = min(width, paper)
|
||||
}
|
||||
lines[i].Text = fmt.Sprintf("[image %d dots: %s]", width, name)
|
||||
}
|
||||
lines[i].Data = data
|
||||
}
|
||||
|
||||
@@ -285,6 +285,119 @@ func TestRenderFetchFailureProducesNothing(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderOptionalImage(t *testing.T) {
|
||||
fetchErr := errors.New("blocked")
|
||||
job := receipt.Job{Elements: []receipt.Element{
|
||||
{Type: "text", Text: "before"},
|
||||
{Type: "image", URL: "https://images.example/x.png", Optional: true, Align: "center"},
|
||||
{Type: "text", Text: "after"},
|
||||
}}
|
||||
for name, fetcher := range map[string]*fakeFetcher{
|
||||
"fetch fails": {err: fetchErr},
|
||||
"decode fails": {data: []byte("not an image")},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
out, err := newRenderer(t, fetcher).Render(context.Background(), job)
|
||||
if err != nil {
|
||||
t.Fatalf("Render: %v", err)
|
||||
}
|
||||
// Centred, as the image would have been.
|
||||
at := bytes.Index(out, []byte("[image unavailable]\n"))
|
||||
if at < 0 {
|
||||
t.Fatalf("no placeholder line in % X", out)
|
||||
}
|
||||
if centre := bytes.LastIndex(out[:at], []byte{0x1B, 0x61}); centre < 0 || out[centre+2] != 1 {
|
||||
t.Errorf("the placeholder is not centred: % X", out)
|
||||
}
|
||||
if bytes.Contains(out, []byte{0x1D, 0x76, 0x30}) {
|
||||
t.Error("an image that failed to load was printed")
|
||||
}
|
||||
if !bytes.Contains(out, []byte("after")) {
|
||||
t.Error("the rest of the receipt is missing")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("not optional", func(t *testing.T) {
|
||||
job := job
|
||||
job.Elements = append([]receipt.Element(nil), job.Elements...)
|
||||
job.Elements[1].Optional = false
|
||||
out, err := newRenderer(t, &fakeFetcher{err: fetchErr}).Render(context.Background(), job)
|
||||
if !errors.Is(err, fetchErr) || out != nil {
|
||||
t.Errorf("Render = %d bytes, %v; want nil and the fetch error", len(out), err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("loads", func(t *testing.T) {
|
||||
out, err := newRenderer(t, &fakeFetcher{data: pngData(t, 100, 10)}).Render(context.Background(), job)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Contains(out, []byte("[image unavailable]")) || !bytes.Contains(out, []byte{0x1D, 0x76, 0x30}) {
|
||||
t.Error("an optional image that loads did not print as an image")
|
||||
}
|
||||
})
|
||||
|
||||
// The renderer is a 576-dot TM-T20: a width of 600 shrinks to 576 dots,
|
||||
// 72 bytes a raster row, while a required image of that width fails.
|
||||
t.Run("wider than the paper", func(t *testing.T) {
|
||||
wide := job
|
||||
wide.Elements = append([]receipt.Element(nil), job.Elements...)
|
||||
wide.Elements[1].Width = 600
|
||||
out, err := newRenderer(t, &fakeFetcher{data: pngData(t, 600, 10)}).Render(context.Background(), wide)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Contains(out, []byte("[image unavailable]")) || !bytes.Contains(out, []byte{0x1D, 0x76, 0x30, 0, 72, 0}) {
|
||||
t.Errorf("the image did not print 576 dots wide: % X", out)
|
||||
}
|
||||
wide.Elements[1].Optional = false
|
||||
if _, err := newRenderer(t, &fakeFetcher{data: pngData(t, 600, 10)}).Render(context.Background(), wide); err == nil {
|
||||
t.Error("a required image wider than the paper printed")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A QR code that would be wider than the paper prints with smaller
|
||||
// modules; one that fits keeps its size.
|
||||
func TestRenderQRFitsThePaper(t *testing.T) {
|
||||
profile, err := escpos.LookupProfile("generic-58mm")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := receipt.NewRenderer(receipt.Options{Profile: profile, CodePage: escpos.CodePage437, MaxPixels: 1000, MaxHeight: 1000}, &fakeFetcher{})
|
||||
moduleSize := func(data string, size int) byte {
|
||||
t.Helper()
|
||||
out, err := r.Render(context.Background(), receipt.Job{Elements: []receipt.Element{{Type: "qr", Data: data, Size: size}}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
at := bytes.Index(out, []byte{0x1D, '(', 'k', 3, 0, '1', 'C'})
|
||||
if at < 0 {
|
||||
t.Fatalf("no module size in % X", out)
|
||||
}
|
||||
return out[at+7]
|
||||
}
|
||||
// 26 bytes at M is version 2, 25 modules: 125 dots at size 5.
|
||||
if got := moduleSize("https://grafana.lan/d/disk", 5); got != 5 {
|
||||
t.Errorf("a code that fits: size %d, want 5", got)
|
||||
}
|
||||
// 500 bytes at M is version 17, 85 modules: 425 dots at size 5, wider
|
||||
// than 384; size 4 makes 340.
|
||||
if got := moduleSize(strings.Repeat("x", 500), 5); got != 4 {
|
||||
t.Errorf("a code too wide: size %d, want 4", got)
|
||||
}
|
||||
// 2331 bytes at M is version 40, 177 modules: only size 2 fits.
|
||||
if got := moduleSize(strings.Repeat("x", 2331), 16); got != 2 {
|
||||
t.Errorf("the largest code: size %d, want 2", got)
|
||||
}
|
||||
// 2400 digits fit only a denser mode than bytes, so they count as version
|
||||
// 40 too.
|
||||
if got := moduleSize(strings.Repeat("7", 2400), 6); got != 2 {
|
||||
t.Errorf("2400 digits: size %d, want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -12,6 +12,10 @@ import (
|
||||
"uberbringer/internal/pkg/raster"
|
||||
)
|
||||
|
||||
// unavailableImage is the line printed in place of an optional image that
|
||||
// failed to load.
|
||||
const unavailableImage = "[image unavailable]"
|
||||
|
||||
// Fetcher downloads remote image data.
|
||||
type Fetcher interface {
|
||||
Fetch(ctx context.Context, url string) ([]byte, error)
|
||||
@@ -163,7 +167,8 @@ func (r *Renderer) textLineDots() int {
|
||||
}
|
||||
|
||||
// loadImages fetches, decodes and dithers every image before anything is
|
||||
// rendered, keyed by element index.
|
||||
// rendered, keyed by element index. An optional image that fails to load
|
||||
// has no entry, and prints as unavailableImage.
|
||||
func (r *Renderer) loadImages(ctx context.Context, job Job) (map[int]*image.Paletted, error) {
|
||||
images := make(map[int]*image.Paletted)
|
||||
for i, el := range job.Elements {
|
||||
@@ -171,6 +176,9 @@ func (r *Renderer) loadImages(ctx context.Context, job Job) (map[int]*image.Pale
|
||||
continue
|
||||
}
|
||||
img, err := r.loadImage(ctx, el)
|
||||
if err != nil && el.Optional {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("elements[%d] (image): %w", i, err)
|
||||
}
|
||||
@@ -198,10 +206,12 @@ func (r *Renderer) loadImage(ctx context.Context, el Element) (*image.Paletted,
|
||||
paper := r.options.Profile.DotsPerLine
|
||||
width := min(src.Bounds().Dx(), paper)
|
||||
if spec.width > 0 {
|
||||
if spec.width > paper {
|
||||
if spec.width > paper && !el.Optional {
|
||||
return nil, fmt.Errorf("width %d is wider than the %d-dot paper", spec.width, paper)
|
||||
}
|
||||
width = spec.width
|
||||
// An optional image never fails for its width: it shrinks to the
|
||||
// paper.
|
||||
width = min(spec.width, paper)
|
||||
}
|
||||
mono, err := raster.Monochrome(src, width)
|
||||
if err != nil {
|
||||
@@ -264,7 +274,7 @@ func (p *printer) element(el Element, img *image.Paletted) error {
|
||||
p.endLine()
|
||||
p.b.Justify(spec.align)
|
||||
p.lines++
|
||||
return p.b.QRCode([]byte(el.Data), spec.size, spec.correction)
|
||||
return p.b.QRCode([]byte(el.Data), p.qrSize(len(el.Data), spec), spec.correction)
|
||||
case TypeBarcode:
|
||||
spec, err := el.barcodeSpec()
|
||||
if err != nil {
|
||||
@@ -281,6 +291,9 @@ func (p *printer) element(el Element, img *image.Paletted) error {
|
||||
return p.b.Barcode(spec.symbology, el.Data)
|
||||
case TypeImage:
|
||||
p.endLine()
|
||||
if img == nil {
|
||||
return p.text(Element{Type: TypeText, Text: unavailableImage, Align: el.Align})
|
||||
}
|
||||
// Alignment is baked into the image; see Renderer.loadImage.
|
||||
p.b.Justify(escpos.JustifyLeft)
|
||||
p.lines++
|
||||
@@ -308,6 +321,19 @@ func (p *printer) element(el Element, img *image.Paletted) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// qrSize returns the module size of a QR code holding n bytes: the size
|
||||
// spec asks for, made smaller, down to one dot, while the code could be
|
||||
// wider than the paper. The width is worked out as if the data were bytes,
|
||||
// so a code of digits may shrink before it strictly has to.
|
||||
func (p *printer) qrSize(n int, spec qrSpec) int {
|
||||
size := spec.size
|
||||
modules := escpos.QRModules(n, spec.correction)
|
||||
for modules > 0 && size > escpos.MinQRModuleSize && modules*size > p.profile.DotsPerLine {
|
||||
size--
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
func (p *printer) text(el Element) error {
|
||||
spec, err := el.textSpec()
|
||||
if err != nil {
|
||||
|
||||
BIN
Binary file not shown.
|
After Width: | Height: | Size: 884 B |
@@ -0,0 +1,10 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200dac4201b45015bfb5d
|
||||
204e696768746c79206261636b75701b450020c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4bf0ab3207265737469633a20322e312047422061646465642069
|
||||
6e2031346d203032732c20302020202020b30ab3206572726f72732e20202020
|
||||
20202020202020202020202020202020202020202020202020202020b30ab320
|
||||
2020202020202020202020202020202020202020202020202020202020202020
|
||||
20202020202020b30ab320546167733a20726573746963202020202020202020
|
||||
202020202020202020202020202020202020b30ac0c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4c4206261636b75707320fa203330204e6f762031323a323920c4c4d90a1b
|
||||
45001b2d001d42001d21001b4d001d564200
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"elements": [
|
||||
{
|
||||
"type": "box",
|
||||
"children": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "restic: 2.1 GB added in 14m 02s, 0 errors."
|
||||
},
|
||||
{
|
||||
"type": "feed"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Tags: restic"
|
||||
}
|
||||
],
|
||||
"title": "[√] Nightly backup",
|
||||
"title_bold": true,
|
||||
"footer": "backups · 30 Nov 12:29"
|
||||
}
|
||||
]
|
||||
}
|
||||
+336
@@ -0,0 +1,336 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200c9cd201b4501484947
|
||||
481b450020cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdbb0aba201b45015b215d204469736b20616c6d6f73742066756c
|
||||
6c1b450020202020202020202020202020202020202020ba0ac7c4c4c4c4c4c4
|
||||
c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c4c4b60aba202f76617220697320617420393425206f6e206e61733031202020
|
||||
20202020202020202020202020ba0aba20202020202020202020202020202020
|
||||
202020202020202020202020202020202020202020202020ba0aba2054616773
|
||||
3a206e6173303120202020202020202020202020202020202020202020202020
|
||||
202020ba0ac8cdcdcdcdcdcdcdcdcdcdcdcdcdcdcd20616c6572747320fa2033
|
||||
30204e6f762031323a323920cdcdbc0a1b45001b2d001d42001d21001b4d001b
|
||||
64011b61011d286b0400314132001d286b03003143051d286b03003145311d28
|
||||
6b1d0031503068747470733a2f2f67726166616e612e6c616e2f642f6469736b
|
||||
1d286b03003151301b61011b4d011d21001b45001b2d001d42004f70656e2064
|
||||
617368626f6172640a1b45001b2d001d42001d21001b4d001b64011b61001d76
|
||||
30004000a0000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000002000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
000000000000000000000000000000000000000000000000000000003f000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000000000000000000000000000000000000000000000000003ff000000
|
||||
00000000000000000000000000000000e03f03f81f80fc07e03f03f81f80fc07
|
||||
e03f03f81f80fc07e03f03f81f80fc07e03f03fa5ffffe97e03f27fff4000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
000000000000000000000000000000000000004ffffffffffb52ffff49000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000000001fffff557fffffffffe892000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
000000000000000000000000000000000000fff9100088893fffc08524000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
00000000000000000000000000000000002ffb04a56d252480002a5249000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000000000000000000000000000017fd052491090925555491492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
0000000000000000000000000000000007fd0a8924a52549112224a248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
000000000000000000000000000000003fc0a45492489424a494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000000000000000000000000002fe565282492542924a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000000000000000000000000017f5210855249029492129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000000007fa094a528924a94249494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
000000000000000000000000000007fc95251285249242924a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000000000000000000000003fe52248a850924929492129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000000001fe909492452a492494249494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
000000000000000000000000001ff44a42492885249242924a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000000000000000000000ffc2a529248550924929492129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000000000000000000000002ffa14109492522a492494249494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000000036db68000000016ffd2a2a542492945249242924a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00000003fffffffffffffffffd08491429248428924929492129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
0000007fffffffffffffffffc0a524a29492528524924a249494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
000017ff5400006daaaaaaa02a529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
00017fea255555102492492a890849242924842a492494212129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
000ffe91482222a54949248924a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
007fe04a254948492424925252529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
03fd15214924252492924924890849242924842a492494212129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000000000
|
||||
1fe89294924a92924949249252a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000000
|
||||
ff22484a492148492424924908529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000000007
|
||||
fc9525212494252492924924a50849242924842a492494212129249248000000
|
||||
000000000000000000000000000000008000000000000000000000000000007f
|
||||
e1409294924a92924949249252a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c00000000000000000000000000003fe
|
||||
8a2a484a492148492424924908529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000000001fe8
|
||||
514925212494252492924924a50849242924842a492494212129249248000000
|
||||
0000000000000000000000000000000080000000000000000000000000007f92
|
||||
8a249294924a92924949249252a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c000000000000000000000000017fa24
|
||||
514a484a492148492424924908529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c0000000000000000000000001bfd092
|
||||
8a2125212494252492924924a50849242924842a492494212129249248000000
|
||||
000000000000000000000000000000008000000000000000000000003ffe8a49
|
||||
214a9294924952924949249252a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c000000003ffffff0000000fffe05292
|
||||
5490484a492488492424924908529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c000000afffffffffffffffff6152449
|
||||
0a4aa521249252a492924924a50849242924842a492494212129249248000000
|
||||
000000000000000000000000000000008000017fff5ad6b5fffffffd48a49292
|
||||
51241294924909124949249252a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c0000fffa922108555555551224a4849
|
||||
2492a84a4924a4a49224924908529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c0007ff084894a500000000a49212524
|
||||
924905212492521249124924a50849242924842a492494212129249248000000
|
||||
000000000000000000000000000000008007fd0a5254512adb6db6d124949292
|
||||
4924b2949249094924a9249252a524929492528524924a949494924925000000
|
||||
00000000000000000000000000000000c03fe8a294828a8420820824924a4849
|
||||
2492484a4924a4a49244924908529249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c1fe8514425450529529529249212524
|
||||
924912a12492521249292492528849242924842a492494212129249248000000
|
||||
00000000000000000000000000000000c7f928a295228a894494494924949292
|
||||
4924a41492490949248492492452a4929492528524924a949494924925000000
|
||||
00000000000000000000000000000000ffa245144894525429229224924a4849
|
||||
2492494a4924a4a49252492492891249424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000fd0928a2924924829249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d4a4924925249254a494924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c25249249092492212424924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d48924924a4924954929249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c2524a4925249248a494924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d48921249092492212424924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c92494924a4924954929249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c4924a4925249240a494924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d24921249092492a12424924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c92494924a4924954929249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c4924a4925249240a494924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d24921249092492a12424924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c92494924a492492a494924929212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000ca492249252492491249249244949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d124952490924924a9249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c49248924a4924920492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d2492524912492495249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c92490924a924924a9249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c4924a49244924920492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d249252492924924aa49249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c92490924849249241249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c4924a49252492492c92492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d2492524929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c92490924849249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000ca924a49252492492492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d0492524929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c52490924849249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d2924a49252492492492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c8492524929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c52490924849249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d2924a49252492492492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c8492524929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c52490924849249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d2924a49252492492492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c8492524929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000caa490924849249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d1124a49252492492492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c4a52524929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d24890924849249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c9252549252492492492492484212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c4909224929249249249249252949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d24a49492449249249249249294a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c9252424929249249249249244212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000ca909292484924924924924922949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d04a49492524924924924924944a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c5252424929249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d2909292484924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c84a49492524924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c5252424929249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d2909292484924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c84a49492524924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c5252424929249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d2909292484924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c84a49492524924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000caa52424929249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000d1109292484924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c4a524a52524924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d2124910929249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c94924a5244924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000c4a492124924924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d2124949249249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c94924a4924924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000ca5492124924924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000d1024949249249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c4a924a4924924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d25492124924924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c9024949249249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000c4a924a4924924924924924924949292
|
||||
492492149249094924849249249212129492528524924a949494924925000000
|
||||
00000000000000000000000000000000d24a92124924924924924924924a4849
|
||||
2492494a4924a4a49252492492494949424929509249214a4a42492492000000
|
||||
00000000000000000000000000000000c9244949249249249249249249212524
|
||||
924924a124925212492924924924a4a42924842a492494212129249248000000
|
||||
00000000000000000000000000000000ffffffffffffffffffffffffffffffff
|
||||
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000
|
||||
00000000000000000000000000000000b6db6db6db6db6db6db6db6db6db6db6
|
||||
db6db6b6db6dadb6db56db6db6db5b5b6db6db56db6db6db6db6db6db6000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000000000000000000000000000000000000000000000000000000000
|
||||
0000000000001d564200
|
||||
@@ -0,0 +1,58 @@
|
||||
{
|
||||
"elements": [
|
||||
{
|
||||
"type": "box",
|
||||
"children": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "[!] Disk almost full",
|
||||
"bold": true
|
||||
},
|
||||
{
|
||||
"type": "rule"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "/var is at 94% on nas01"
|
||||
},
|
||||
{
|
||||
"type": "feed"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Tags: nas01"
|
||||
}
|
||||
],
|
||||
"title": "HIGH",
|
||||
"title_bold": true,
|
||||
"footer": "alerts · 30 Nov 12:29",
|
||||
"style": "double"
|
||||
},
|
||||
{
|
||||
"type": "feed"
|
||||
},
|
||||
{
|
||||
"type": "qr",
|
||||
"size": 5,
|
||||
"align": "center",
|
||||
"data": "https://grafana.lan/d/disk",
|
||||
"correction": "M"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Open dashboard",
|
||||
"font": "b",
|
||||
"align": "center"
|
||||
},
|
||||
{
|
||||
"type": "feed"
|
||||
},
|
||||
{
|
||||
"type": "image",
|
||||
"width": 384,
|
||||
"align": "center",
|
||||
"url": "http://127.0.0.1:8099/graph.png",
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200c4c4206261636b7570
|
||||
7320fa203330204e6f762031323a323920c4c4c4c4c4c4c4c4c4c4c4c4c4c4c4
|
||||
c40a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45011b2d00
|
||||
1d42005072756e652066696e69736865640a1b45001b2d001d42001d21001b4d
|
||||
001b61001b4d001d21001b45001b2d001d4200343120736e617073686f747320
|
||||
6b6570742c20332072656d6f7665642e0a1b45001b2d001d42001d21001b4d00
|
||||
1d564200
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"elements": [
|
||||
{
|
||||
"type": "rule",
|
||||
"text": "backups · 30 Nov 12:29",
|
||||
"align": "left"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Prune finished",
|
||||
"bold": true
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "41 snapshots kept, 3 removed."
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
1b401b74001b61001b4d001d21001b45001b2d001d4200b2b2b2b2b2b2b2b2b2
|
||||
b2b2b2b2b2b2b2b2201b4501555247454e541b450020b2b2b2b2b2b2b2b2b2b2
|
||||
b2b2b2b2b2b2b20a1b45001b2d001d42001d21001b4d001b61001b4d001d2100
|
||||
1b45001b2d001d4200c9cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdbb0aba201b45011d420128212920
|
||||
4261636b7570204641494c4544206f6e206e617330311b45001d420020202020
|
||||
202020202020202020ba0acccdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdb90aba204a6f62206e696768
|
||||
746c792d6f66667369746520657869746564207769746820636f6465203220ba
|
||||
0aba20616674657220336831326d2e204c61737420676f6f642072756e3a2032
|
||||
20646179732020202020ba0aba2061676f2e2020202020202020202020202020
|
||||
202020202020202020202020202020202020202020ba0ac8cdcdcdcdcdcdcdcd
|
||||
cdcdcdcdcdcd206261636b75707320fa203330204e6f762031323a323920cdcd
|
||||
bc0a1b45001b2d001d42001d21001b4d001b61001b4d001d21001b45001b2d00
|
||||
1d4200b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2
|
||||
b2b2b2b2b2b2b2b2b2b2b2b2b20a1b45001b2d001d42001d21001b4d001d5642
|
||||
00
|
||||
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"elements": [
|
||||
{
|
||||
"type": "rule",
|
||||
"text": "URGENT",
|
||||
"bold": true,
|
||||
"style": "dark"
|
||||
},
|
||||
{
|
||||
"type": "box",
|
||||
"children": [
|
||||
{
|
||||
"type": "text",
|
||||
"text": "(!) Backup FAILED on nas01",
|
||||
"bold": true,
|
||||
"invert": true
|
||||
},
|
||||
{
|
||||
"type": "rule",
|
||||
"style": "double"
|
||||
},
|
||||
{
|
||||
"type": "text",
|
||||
"text": "Job nightly-offsite exited with code 2 after 3h12m. Last good run: 2 days ago."
|
||||
}
|
||||
],
|
||||
"footer": "backups · 30 Nov 12:29",
|
||||
"style": "double"
|
||||
},
|
||||
{
|
||||
"type": "rule",
|
||||
"style": "dark"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -111,9 +111,21 @@ func TestValidateBlocks(t *testing.T) {
|
||||
{"list problems inside a box", box(receipt.Element{Type: "list", Bullet: "checkbox", Items: []string{"a"}, Done: []int{3}}),
|
||||
[]string{"elements[0].children[0] (list): done[0]: index 3 is out of range"}},
|
||||
|
||||
{"title_bold on another type", receipt.Element{Type: "rule", TitleBold: true},
|
||||
[]string{`elements[0] (rule): field "title_bold" does not apply to rule`}},
|
||||
{"title_bold on an old type", receipt.Element{Type: "text", Text: "x", TitleBold: true},
|
||||
[]string{`elements[0] (text): field "title_bold" does not apply to text`}},
|
||||
{"optional on another type", receipt.Element{Type: "qr", Data: "x", Optional: true},
|
||||
[]string{`elements[0] (qr): field "optional" does not apply to qr`}},
|
||||
{"optional on a block", receipt.Element{Type: "kv", Pairs: []receipt.Pair{{Key: "a"}}, Optional: true},
|
||||
[]string{`elements[0] (kv): field "optional" does not apply to kv`}},
|
||||
{"optional inside a box", box(receipt.Element{Type: "text", Text: "x", Optional: true}),
|
||||
[]string{`elements[0].children[0] (text): field "optional" does not apply to text inside a box`}},
|
||||
{"an optional image", receipt.Element{Type: "image", URL: "https://x/y.png", Optional: true, Width: 384, Align: "center"}, nil},
|
||||
|
||||
{"old types stay lenient", receipt.Element{Type: "feed", Text: "ignored", Bold: true}, nil},
|
||||
{"a full box", receipt.Element{
|
||||
Type: "box", Style: "double-v", Title: "t", TitleStyle: "capped", Footer: "f", Padding: intPtr(0),
|
||||
Type: "box", Style: "double-v", Title: "t", TitleStyle: "capped", TitleBold: true, Footer: "f", Padding: intPtr(0),
|
||||
Fit: true, Columns: 30, Size: 2, Font: "b", Align: "right", Bold: true, Underline: 2, Invert: true, Children: []receipt.Element{
|
||||
{Type: "text", Text: "x", Bold: true, Underline: 1, Invert: true, Align: "center", Wrap: boolPtr(false), Newline: boolPtr(true)},
|
||||
{Type: "feed", Lines: 2},
|
||||
|
||||
Reference in new issue
Block a user