# Receipt Maker CLI that prints images, notes, and QR codes to an Epson TM-T20II thermal receipt printer (Ethernet via USB adapter, or direct USB with `--usb`). ## Layout - `receipt.py` — the CLI; subcommand per `cmd_*` function - `receipt_tui.py` — Textual TUI (`tui` subcommand); thin layer over the `cmd_*` handlers via `receipt.run_action()`, which captures stdout/stderr for the log panel and returns an `ActionResult` (rc, output, error kind). `textual` is an optional dependency (`[tui]` extra); the CLI works without it - `receipt_web.py` — FastAPI server (`serve` subcommand, `[web]` extra) behind the site's nginx `/receipt/` over Tailscale; requires the `X-Receipt-Token` header nginx adds (`serve --local` drops it, binding to loopback only and refusing non-localhost `Host`s), and `X-Requested-With: receipt` on POSTs. POSTs render the job in the request (`InvalidJob` → 400), queue the bytes in a `PrintQueue` and answer 202; one worker thread sends them, and the page long-polls `/api/jobs/{id}`. `/api/status` only tries the printer lock, so it never waits behind a print. Uploads print from memory. Always runs with `no_setup` (no sudo; the printer must already be reachable). Deployed on stppi, which is online over Wi-Fi and has the printer cabled to its `eth0` (printer 192.168.192.168, Pi 192.168.192.10 via the `receipt-printer` nmcli profile) - Scheduling (all run inside `serve`; `[web]` extra): `receipt_store.py` (SQLite at `$RECEIPT_DATA_DIR/receipt.db`, default `~/.local/share/receipt-maker`; additive-only migrations so `update.sh` rollbacks still read it), `receipt_models.py` (pydantic specs: a schedule is trigger + blocks + options), `receipt_triggers.py` (next fire times, DST-safe, plain-English `describe()`), `receipt_sources.py` (ICS/RSS/JSON fetch + parse), `receipt_content.py` (blocks → `receipt.Block`s, `{var}` template fill — never `str.format`), `receipt_scheduler.py` (`tick(now)` is deterministic; tests drive it with a fake clock). Webhooks are `POST /hook/{id}` with a per-schedule secret - `web/` — the page (vanilla ES modules, no build step), served by token-gated routes with relative URLs - `deploy/` — systemd user units; `update.sh` (run by `receipt-update.timer`) tests the new Gitea commit in a scratch worktree, then fast-forwards the server's clone, restarts `receipt-web` and rolls back if `/healthz` doesn't answer. Pushing to Gitea `master` is the deploy - `connect_receipt.sh` — brings up the printer's network interface (needs sudo); run automatically before any print over Ethernet. The TUI runs it with output captured (`PrinterConfig.quiet_setup`, set by `run_action()`) so it can't corrupt the screen - `tests/` — pytest suite; uses `FakePrinter` + mocked `requests`, never touches hardware or network ## Conventions - Printer/network constants live at the top of `receipt.py`; IP/port overridable via `--ip`/`--port` or `RECEIPT_PRINTER_IP`/`RECEIPT_PRINTER_PORT` - Receipts are lists of `receipt.Block` (height, ESC/POS build, preview parts); `render_blocks()` makes one job of them and refuses one over `MAX_PRINT_HEIGHT_DOTS` - Every print is `render_*()` → bytes → `send(config, payload)`: `render()` builds the job on an escpos `Dummy` (ESC @ first, cut last), and `send()` connects fresh, writes it once and closes. Only the connect is retried; a mid-job drop raises `PrinterDropped` (a `PrinterUnavailable`) and is never replayed, by `send()` or by the scheduler's retries. `cmd_*` handlers take a `PrinterConfig`, not a printer handle - User errors raise `InvalidJob`, printer trouble raises `PrinterUnavailable`; `main()`, `run_action()` and the web server turn them into messages / status codes - Images are prepared in memory (`prepare_for_print`: white-composite alpha → resize to 576px → autocontrast → 1-bit dither); nothing is written except the `img/` download cache. `MAX_PRINT_HEIGHT_DOTS` caps paper per job (tall images shrink, long notes are refused) - `run_command()` dispatches every subcommand except `tui`/`serve`, for both `main()` and `run_action()` (via the `PRINT_COMMANDS` registry) - `--no-setup` (`PrinterConfig.no_setup`) skips the connect script everywhere it would run; check it with `config.needs_setup` ## Checks ```bash pytest # required ruff check . # lint (config in pyproject.toml) ``` Actual printing can only be verified with the physical printer attached.