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
@@ -1,6 +1,8 @@
|
||||
package receipt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"uberbringer/internal/pkg/escpos"
|
||||
@@ -67,6 +69,7 @@ const (
|
||||
itemFeed
|
||||
itemDivider
|
||||
itemKV
|
||||
itemList
|
||||
)
|
||||
|
||||
// boxItem is one part of a box body, with its text already normalized.
|
||||
@@ -88,6 +91,9 @@ type boxItem struct {
|
||||
// itemKV.
|
||||
pairs []kvRow
|
||||
leader string
|
||||
|
||||
// itemList.
|
||||
list []listRow
|
||||
}
|
||||
|
||||
// kvRow is a Pair with its text normalized.
|
||||
@@ -189,6 +195,9 @@ func (lo *layout) boxItems(el Element, spec boxSpec) []boxItem {
|
||||
case TypeKV:
|
||||
ks, _ := child.kvSpec()
|
||||
items = append(items, boxItem{kind: itemKV, pairs: lo.kvRows(child.Pairs), leader: leaderFor(ks.leader, lo.cp)})
|
||||
case TypeList:
|
||||
ls, _ := child.listSpec()
|
||||
items = append(items, boxItem{kind: itemList, list: lo.listRows(child, ls)})
|
||||
}
|
||||
}
|
||||
return items
|
||||
@@ -211,6 +220,16 @@ func (it boxItem) naturalWidth(limit int) int {
|
||||
widest = max(widest, runeLen(p.key)+4+runeLen(p.value))
|
||||
}
|
||||
return min(widest, limit)
|
||||
case itemList:
|
||||
widest := 0
|
||||
for _, row := range it.list {
|
||||
bullet := runeLen(row.bullet)
|
||||
room := max(limit-bullet, 1)
|
||||
for _, text := range wrap(row.text, room, room) {
|
||||
widest = max(widest, bullet+runeLen(text))
|
||||
}
|
||||
}
|
||||
return min(widest, limit)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -232,7 +251,8 @@ func (it boxItem) rows(width int) []string {
|
||||
return rows
|
||||
}
|
||||
|
||||
// contentLines lays out a text, feed or kv item at exactly width cells.
|
||||
// contentLines lays out a text, feed, kv or list item at exactly width
|
||||
// cells.
|
||||
func (it boxItem) contentLines(width int) []blockLine {
|
||||
switch it.kind {
|
||||
case itemFeed:
|
||||
@@ -243,6 +263,8 @@ func (it boxItem) contentLines(width int) []blockLine {
|
||||
return lines
|
||||
case itemKV:
|
||||
return kvLines(it.pairs, it.leader, width)
|
||||
case itemList:
|
||||
return listLines(it.list, width)
|
||||
}
|
||||
var lines []blockLine
|
||||
for _, row := range it.rows(width) {
|
||||
@@ -387,6 +409,69 @@ func kvLines(rows []kvRow, leader string, width int) []blockLine {
|
||||
return lines
|
||||
}
|
||||
|
||||
// list lays out a list block on a line of cols cells.
|
||||
func (lo *layout) list(el Element, spec listSpec, cols int) []blockLine {
|
||||
width := blockWidth(spec.columns, cols)
|
||||
return centre(listLines(lo.listRows(el, spec), width), width, cols)
|
||||
}
|
||||
|
||||
// listRow is one list item: its bullet, with the space after it, and its
|
||||
// text, normalized.
|
||||
type listRow struct {
|
||||
bullet, text string
|
||||
}
|
||||
|
||||
// listRows gives each item of a list its bullet: "· ", "- ", a number
|
||||
// right-aligned to the widest, or a checkbox, ticked for the done items.
|
||||
func (lo *layout) listRows(el Element, spec listSpec) []listRow {
|
||||
done := make(map[int]bool, len(el.Done))
|
||||
for _, index := range el.Done {
|
||||
done[index] = true
|
||||
}
|
||||
digits := len(strconv.Itoa(len(el.Items)))
|
||||
rows := make([]listRow, len(el.Items))
|
||||
for i, item := range el.Items {
|
||||
var bullet string
|
||||
switch spec.bullet {
|
||||
case bulletDash:
|
||||
bullet = "- "
|
||||
case bulletNumber:
|
||||
bullet = fmt.Sprintf("%*d. ", digits, i+1)
|
||||
case bulletCheckbox:
|
||||
bullet = "[ ] "
|
||||
if done[i] {
|
||||
bullet = "[" + checkMark(lo.cp) + "] "
|
||||
}
|
||||
default:
|
||||
bullet = listDot(lo.cp) + " "
|
||||
}
|
||||
rows[i] = listRow{bullet: bullet, text: lo.text(item)}
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// listLines lays out rows at exactly width cells: each bullet, then the
|
||||
// item's text wrapped to the room after it, its further lines indented to
|
||||
// line up with the first. On a line too narrow for the whole bullet, the
|
||||
// bullet is cut so that one cell is left for the text.
|
||||
func listLines(rows []listRow, width int) []blockLine {
|
||||
width = max(width, 1)
|
||||
var lines []blockLine
|
||||
for _, row := range rows {
|
||||
bullet := []rune(row.bullet)
|
||||
indent := min(len(bullet), width-1)
|
||||
room := width - indent
|
||||
for i, text := range wrap(row.text, room, room) {
|
||||
lead := spaces(indent)
|
||||
if i == 0 {
|
||||
lead = string(bullet[:indent])
|
||||
}
|
||||
lines = append(lines, blockLine{plain(lead + text + spaces(room-runeLen(text)))})
|
||||
}
|
||||
}
|
||||
return lines
|
||||
}
|
||||
|
||||
// narrowKVLines lays out rows on lines too narrow for a key, a leader and a
|
||||
// value side by side: the key on a line of its own, then the value,
|
||||
// right-aligned.
|
||||
|
||||
Reference in new issue
Block a user