Add the list block and the module layer with the note module
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>
This commit is contained in:
1 parent
6b2e402f1a
commit
3e9779f71a
69 files changed
+3092
-262
No files matched your search
@@ -80,16 +80,20 @@ var fields = []field{
|
||||
{"columns", func(e Element) bool { return e.Columns != 0 }},
|
||||
{"pairs", func(e Element) bool { return e.Pairs != nil }},
|
||||
{"leader", func(e Element) bool { return e.Leader != "" }},
|
||||
{"items", func(e Element) bool { return e.Items != nil }},
|
||||
{"bullet", func(e Element) bool { return e.Bullet != "" }},
|
||||
{"done", func(e Element) bool { return e.Done != nil }},
|
||||
}
|
||||
|
||||
// blockOnly are the fields no other type takes.
|
||||
var blockOnly = fieldSet("children title title_style footer style padding fit columns pairs leader")
|
||||
var blockOnly = fieldSet("children title title_style footer style padding fit columns pairs leader items bullet done")
|
||||
|
||||
// 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"),
|
||||
TypeRule: fieldSet("text bold align size font style columns"),
|
||||
TypeKV: fieldSet("pairs leader size font columns"),
|
||||
TypeList: fieldSet("items bullet done size font columns"),
|
||||
}
|
||||
|
||||
// childFields are the fields each type takes inside a box.
|
||||
@@ -98,6 +102,7 @@ var childFields = map[string]map[string]bool{
|
||||
TypeFeed: fieldSet("lines"),
|
||||
TypeRule: fieldSet("text bold align style"),
|
||||
TypeKV: fieldSet("pairs leader"),
|
||||
TypeList: fieldSet("items bullet done"),
|
||||
}
|
||||
|
||||
// inherited are the fields a child takes from its box.
|
||||
@@ -334,3 +339,76 @@ func (el Element) pairProblems() []string {
|
||||
}
|
||||
return problems
|
||||
}
|
||||
|
||||
// listBullet selects what marks each item of a list.
|
||||
type listBullet int
|
||||
|
||||
const (
|
||||
bulletDot listBullet = iota
|
||||
bulletDash
|
||||
bulletNumber
|
||||
bulletCheckbox
|
||||
)
|
||||
|
||||
type listSpec struct {
|
||||
bullet listBullet
|
||||
columns int
|
||||
size int
|
||||
font escpos.Font
|
||||
}
|
||||
|
||||
func (el Element) listSpec() (listSpec, error) {
|
||||
s := listSpec{columns: el.Columns}
|
||||
var err error
|
||||
if s.size, s.font, err = el.blockScale(); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch strings.ToLower(el.Bullet) {
|
||||
case "", "dot":
|
||||
s.bullet = bulletDot
|
||||
case "dash":
|
||||
s.bullet = bulletDash
|
||||
case "number":
|
||||
s.bullet = bulletNumber
|
||||
case "checkbox":
|
||||
s.bullet = bulletCheckbox
|
||||
default:
|
||||
return s, fmt.Errorf("bullet must be dot, dash, number or checkbox")
|
||||
}
|
||||
if err := checkColumns(el.Columns); err != nil {
|
||||
return s, err
|
||||
}
|
||||
switch n := len(el.Items); {
|
||||
case n == 0:
|
||||
return s, fmt.Errorf("items: at least one item is required")
|
||||
case n > MaxItems:
|
||||
return s, fmt.Errorf("items: %d items, limit is %d", n, MaxItems)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// itemProblems reports every empty item and every problem with done: done
|
||||
// on a list that is not a checklist, and an index that is out of range or
|
||||
// repeated.
|
||||
func (el Element) itemProblems() []string {
|
||||
var problems []string
|
||||
for i, item := range el.Items {
|
||||
if item == "" {
|
||||
problems = append(problems, fmt.Sprintf("items[%d]: must not be empty", i))
|
||||
}
|
||||
}
|
||||
if len(el.Done) > 0 && !strings.EqualFold(el.Bullet, "checkbox") {
|
||||
return append(problems, `done: only a list with bullet "checkbox" has done items`)
|
||||
}
|
||||
seen := make(map[int]bool, len(el.Done))
|
||||
for i, index := range el.Done {
|
||||
switch {
|
||||
case index < 0 || index >= len(el.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
|
||||
}
|
||||
Reference in new issue
Block a user