qr

package
v0.167.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package qr is a dependency-free QR code encoder.

It is a module-for-module port of the dashboard's ui/src/lib/qr.ts, so the daemon can print the same pairing code in a terminal that the pairing screen draws in the browser. Rule 4 is the reason it exists at all: the pairing URL carries a code that lets a device in, and it must not leave the machine to be drawn, so no image service and no third-party library.

Deliberately narrow, exactly as qr.ts is: byte mode, error correction level M, versions 1 to 10 (up to MaxBytes bytes of UTF-8). Keeping the two encoders identical, including the mask choice, means a code scanned from the terminal and one scanned from the screen are the same symbol, and testdata/vectors.json (generated from qr.ts) proves it.

Index

Constants

View Source
const MaxBytes = 213

MaxBytes is the longest UTF-8 input a version-10, level-M code in byte mode holds: 216 data codewords less 2.5 bytes of mode and length header.

Variables

View Source
var ErrTooLong = errors.New("qr: text too long for a QR code here")

ErrTooLong is returned for input longer than MaxBytes.

Functions

This section is empty.

Types

type Code

type Code struct {
	// Size is the width and height in modules, without a quiet zone.
	Size int
	// contains filtered or unexported fields
}

Code is a square of modules.

func Encode

func Encode(text string) (*Code, error)

Encode encodes text as UTF-8 into the smallest code that holds it.

func (*Code) Dark

func (c *Code) Dark(x, y int) bool

Dark reports whether the module at column x, row y is dark. Coordinates outside the symbol are light, which is what a quiet zone is.

func (*Code) Terminal

func (c *Code) Terminal(quiet int) string

Terminal renders the code with Unicode half blocks, two module rows per line, inside a quiet zone of quiet modules on every side (the standard asks for 4; a negative value is treated as 0).

The drawing is inverted on purpose: a dark module is a space and a light module is a block, as `qrencode -t utf8` does. A space takes the terminal's background and a block its foreground, and on the usual dark terminal that makes the light modules light and the dark modules dark, which is what a scanner needs. Drawing dark modules as blocks instead gives a negative code on a dark terminal, which many phone cameras refuse to read. On a light terminal (dark text on white) this drawing comes out as a negative; the common iOS and Android scanners read inverted codes, and the drawn quiet zone keeps the symbol framed either way. A rendering that is positive on every background would need ANSI colours, which not every output keeps.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL