The list block prints items with a dot, dash, number or checkbox bullet,
wrapping a long item with a hanging indent; a checkbox list ticks its
done items as [√], or [x] on code pages without √. It stands alone or
inside a box, and its items, bullet and done fields carry their enums
and limits as jsonschema tags.
internal/pkg/modules holds modules that build ordinary jobs from their
own payloads: a registry by name, an Env with the clock, time zone and
time layout, strict payload decoding that reports every problem, ErrSkip
and PayloadError. The note module prints a boxed note with an optional
checklist and a time stamp in the footer.
Three routes join the route table: POST /api/v1/print/{module} and
POST /api/v1/preview/{module}, which take ?copies and a reserved
?printer, and GET /api/v1/modules. Print and the module route share one
submit path, and preview shares one too. New error codes:
invalid_payload, invalid_query, unknown_printer, module_not_found and
module_failed; a module that builds an invalid receipt is logged and
reported to Sentry once. [format] gains timezone and time_layout, the
binary embeds the time zone database, and uberbringer preview takes
--module.
The note receipt passed on the TM-T88IV, and its bytes are pinned in
TestPaperReceipts. docs/api.md describes the list block, the module
routes and the note payload; docs/swagger.json is regenerated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
package modules
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"uberbringer/internal/pkg/receipt"
|
|
)
|
|
|
|
// noteStyles are the box styles a note may ask for.
|
|
var noteStyles = []string{"single", "double", "double-h", "double-v"}
|
|
|
|
// Note returns the note module: a box with an optional title, the text and
|
|
// an optional checklist, stamped with the time in its bottom border.
|
|
func Note() Module {
|
|
return note{}
|
|
}
|
|
|
|
type note struct{}
|
|
|
|
// notePayload is the body of a note.
|
|
type notePayload struct {
|
|
Title string `json:"title"`
|
|
Text string `json:"text"`
|
|
Items []string `json:"items"`
|
|
Done []int `json:"done"`
|
|
Style string `json:"style"`
|
|
Footer string `json:"footer"`
|
|
Stamp *bool `json:"stamp"`
|
|
}
|
|
|
|
func (note) Name() string { return "note" }
|
|
|
|
func (note) Description() string { return "Boxed note with an optional checklist" }
|
|
|
|
// Build lays the note out as a box: the text, then, when there are items,
|
|
// an empty line and a checklist. The footer is the payload's, or the build
|
|
// time unless stamp is false.
|
|
func (note) Build(_ context.Context, body []byte, env Env) (receipt.Job, error) {
|
|
var p notePayload
|
|
if err := DecodeStrict(body, &p); err != nil {
|
|
return receipt.Job{}, err
|
|
}
|
|
if problems := p.problems(); len(problems) > 0 {
|
|
return receipt.Job{}, &PayloadError{Problems: problems}
|
|
}
|
|
|
|
children := []receipt.Element{{Type: receipt.TypeText, Text: p.Text}}
|
|
if len(p.Items) > 0 {
|
|
children = append(children,
|
|
receipt.Element{Type: receipt.TypeFeed},
|
|
receipt.Element{Type: receipt.TypeList, Bullet: "checkbox", Items: p.Items, Done: p.Done},
|
|
)
|
|
}
|
|
footer := p.Footer
|
|
if footer == "" && (p.Stamp == nil || *p.Stamp) {
|
|
footer = env.Stamp()
|
|
}
|
|
box := receipt.Element{
|
|
Type: receipt.TypeBox,
|
|
Title: p.Title,
|
|
Style: strings.ToLower(p.Style),
|
|
Footer: footer,
|
|
Children: children,
|
|
}
|
|
return receipt.Job{Elements: []receipt.Element{box}}, nil
|
|
}
|
|
|
|
// problems checks what the box and its list would refuse, in the payload's
|
|
// own terms.
|
|
func (p notePayload) problems() []string {
|
|
var problems []string
|
|
if strings.TrimSpace(p.Text) == "" {
|
|
problems = append(problems, "text: required")
|
|
}
|
|
if p.Style != "" && !containsFold(noteStyles, p.Style) {
|
|
problems = append(problems, "style: must be "+strings.Join(noteStyles[:len(noteStyles)-1], ", ")+" or "+noteStyles[len(noteStyles)-1])
|
|
}
|
|
if n := len(p.Items); n > receipt.MaxItems {
|
|
problems = append(problems, fmt.Sprintf("items: %d items, limit is %d", n, receipt.MaxItems))
|
|
}
|
|
for i, item := range p.Items {
|
|
if item == "" {
|
|
problems = append(problems, fmt.Sprintf("items[%d]: must not be empty", i))
|
|
}
|
|
}
|
|
seen := make(map[int]bool, len(p.Done))
|
|
for i, index := range p.Done {
|
|
switch {
|
|
case index < 0 || index >= len(p.Items):
|
|
problems = append(problems, fmt.Sprintf("done[%d]: index %d is out of range", i, index))
|
|
case seen[index]:
|
|
problems = append(problems, fmt.Sprintf("done[%d]: index %d is repeated", i, index))
|
|
}
|
|
seen[index] = true
|
|
}
|
|
return problems
|
|
}
|
|
|
|
func containsFold(list []string, s string) bool {
|
|
for _, item := range list {
|
|
if strings.EqualFold(item, s) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|