Tidy up, make main a cmd/

This commit is contained in:
2026-05-06 15:43:30 +09:30
parent 15f7ce6d8f
commit 96c0c4b253
3 changed files with 82 additions and 47 deletions

79
cmd/main.go Normal file
View File

@@ -0,0 +1,79 @@
package main
import (
"flag"
"fmt"
"os"
"code.ppl.town/justin/qr_labels/qr_labels"
)
var flagLabel string
var flagQRCode string
var flagQRSize float64
var flagRows, flagCols uint
var flagFontSize float64
var flagFont string
var flagFontStyle string
var flagBorders bool = true
var flagFilename string
var flagCodeIsLabel bool
func main() {
flag.StringVar(&flagLabel, "label", "", "label (printed above QR code)")
flag.StringVar(&flagQRCode, "code", "", "string to turn into a QR code (URL, text etc)")
flag.Float64Var(&flagQRSize, "size", 50.0, "size of the QR code")
flag.UintVar(&flagRows, "rows", 4, "number of rows on the page")
flag.UintVar(&flagCols, "cols", 3, "number of columns on the page")
flag.StringVar(&flagFont, "font", "Helvetica", "name of the font")
flag.Float64Var(&flagFontSize, "font-size", 24.0, "font-size, in pts")
flag.StringVar(&flagFontStyle, "font-style", "", "font style, combine 'B', 'U', 'S', 'I' characters")
flag.BoolVar(&flagBorders, "borders", false, "print borders between labels")
flag.StringVar(&flagFilename, "output", "", "filename to write the PDF")
flag.BoolVar(&flagCodeIsLabel, "code-is-label", false, "use the -code as the -label")
flag.Parse()
if flagFilename == "" {
fmt.Println("you need to supply an -output filename")
os.Exit(1)
}
if flagQRCode == "" {
fmt.Println("you need to supply a QR -code string")
os.Exit(1)
}
if flagCodeIsLabel && flagLabel != "" {
fmt.Println("you cannot use both -code-is-label and -label")
os.Exit(1)
}
if flagCodeIsLabel {
flagLabel = flagQRCode
}
f, err := os.Create(flagFilename)
if err != nil {
fmt.Printf("failed to create output file: %s\n", err.Error())
os.Exit(1)
}
err = qr_labels.GeneratePage(f,
flagQRCode,
flagLabel,
qr_labels.WithQRSize(flagQRSize),
qr_labels.WithFont(flagFont, flagFontSize, flagFontStyle),
qr_labels.WithBorders(flagBorders),
qr_labels.WithGrid(int(flagRows), int(flagCols)),
)
if err != nil {
fmt.Printf("could not generate pdf: %s\n", err.Error())
os.Exit(1)
}
}

38
main.go
View File

@@ -1,38 +0,0 @@
package main
import (
"log/slog"
"os"
"code.ppl.town/justin/qr_labels/qr_labels"
)
func main() {
f, _ := os.Create("hello.pdf")
err := qr_labels.GeneratePage(f,
`https://signal.me/#eu/tardisx.81`,
"Wow Nic is cool :-)",
qr_labels.WithQRSize(55),
// qr_labels.WithFont("Helvetica", 16, ""),
// qr_labels.WithBorders(false),
// qr_labels.WithGrid(4, 3),
)
// err := qr_labels.GeneratePage(f, qr_labels.PageOptions{
// // qrURL: "geo:-34.9285,138.6007",
// qrURL: `https://signal.me/#eu/tardisx.81`,
// qrLabel: "Á[Hjqy]|",
// qrSize: 54,
// font: "Helvetica",
// fontStyle: "B",
// fontSize: 34.0,
// rows: 4,
// cols: 3,
// borders: true,
// })
if err != nil {
slog.Error("could not generate pdf", "error", err)
os.Exit(1)
}
}

View File

@@ -3,7 +3,6 @@ package qr_labels
import ( import (
"fmt" "fmt"
"io" "io"
"log/slog"
"codeberg.org/go-pdf/fpdf" "codeberg.org/go-pdf/fpdf"
"github.com/yeqown/go-qrcode/v2" "github.com/yeqown/go-qrcode/v2"
@@ -117,20 +116,15 @@ func GeneratePage(w io.Writer, url, label string, opts ...Option) error {
cellWidth := po.qrSize cellWidth := po.qrSize
if cellHeight > rowHeight-requiredCellPadding { if cellHeight > rowHeight-requiredCellPadding {
slog.Warn("qr is too big for row", "cell_height", cellHeight, "row_height", rowHeight) return fmt.Errorf("qr is too big for row size - reduce number of rows or QR size")
return fmt.Errorf("qr is too big for row size")
} }
// slog.Info("check width", "cell_width", cellWidth, "col_width", colWidth)
if cellWidth > colWidth-requiredCellPadding { if cellWidth > colWidth-requiredCellPadding {
slog.Warn("qr is too big for column", "cell_width", cellWidth, "col_width", colWidth) return fmt.Errorf("QR is too big for col size - reduce number of columns or QR size")
return fmt.Errorf("qr is too big for col size")
} }
if textWidth > colWidth { if textWidth > colWidth {
slog.Warn("text probably too wide") return fmt.Errorf("text is too wide for col size - reduce label length or reduce font size")
return fmt.Errorf("text is too wide for col size")
} }
for rowIdx := range po.rows { for rowIdx := range po.rows {