git.sophuwu.com > qrstr
added a bunch of examples
parent

e0f5ba97d352e9d505774f0180d702ae5d919d36

M .gitignore.gitignore

@@ -1,3 +1,2 @@

.idea -build/ -test/ +build/
A example/html/html.go

@@ -0,0 +1,35 @@

+/* +- Example using qrstr to generate a QR code in HTML format. +- When run, it will print the HTML representation of the QR code to the console. +*/ +package main + +import ( + "fmt" + + "git.sophuwu.com/qrstr" +) + +var ( + + // data is the string to be encoded in the QR code + data = "https://git.sophuwu.com/qrstr" + + // If provided, headers will be displayed as text above the QR code. + // headers intended as human-readable labels, titles or descriptions. + // The headers will be displayed in the order they are provided. + headers = []string{"qrstr: QR text encoder", "scan for git repo"} +) + +func main() { + qr, err := qrstr.NewEncoder(qrstr.HTMLMode, qrstr.ErrorCorrection25Percent) + if err != nil { + panic(err) + } + var out string + out, err = qr.Encode(data, headers...) + if err != nil { + panic(err) + } + fmt.Println(out) +}
A example/plain-text/plain-text.go

@@ -0,0 +1,42 @@

+/* +- Example: using qrstr to generate a QR code as plain text + +Plain text QR codes have different encoders for light and dark backgrounds, +So you need to choose the appropriate encoder based on your background color. + +You will need use a monospaced font to display the QR code correctly. +Plain text requires fixed-width characters to align the QR code properly. + +This example makes a QR code for a dark background, with white text. +*/ +package main + +import ( + "fmt" + + "git.sophuwu.com/qrstr" +) + +var ( + + // data is the string to be encoded in the QR code + data = "https://git.sophuwu.com/qrstr" + + // If provided, headers will be displayed as text above the QR code. + // headers intended as human-readable labels, titles or descriptions. + // The headers will be displayed in the order they are provided. + headers = []string{"qrstr: QR text encoder", "scan for git repo"} +) + +func main() { + qr, err := qrstr.NewEncoder(qrstr.TextDarkMode, qrstr.ErrorCorrection7Percent) + if err != nil { + panic(err) + } + var out string + out, err = qr.Encode(data, headers...) + if err != nil { + panic(err) + } + fmt.Println(out) +}
A example/terminal/terminal.go

@@ -0,0 +1,35 @@

+/* +- Example using qrstr to write a QR code to the terminal using xterm control sequences. +- When run in an xterm-compatible terminal, it will display the QR code directly in the terminal. +*/ +package main + +import ( + "fmt" + + "git.sophuwu.com/qrstr" +) + +var ( + + // data is the string to be encoded in the QR code + data = "https://git.sophuwu.com/qrstr" + + // If provided, headers will be displayed as text above the QR code. + // headers intended as human-readable labels, titles or descriptions. + // The headers will be displayed in the order they are provided. + headers = []string{"qrstr: QR text encoder", "scan for git repo"} +) + +func main() { + qr, err := qrstr.NewEncoder(qrstr.TerminalMode, qrstr.ErrorCorrection7Percent) + if err != nil { + panic(err) + } + var out string + out, err = qr.Encode(data, headers...) + if err != nil { + panic(err) + } + fmt.Println(out) +}
M go.modgo.mod

@@ -1,5 +1,7 @@

module git.sophuwu.com/qrstr -go 1.24.2 +go 1.24.6 require github.com/boombuler/barcode v1.0.2 + +require git.sophuwu.com/gophuwu v1.0.2
M go.sumgo.sum

@@ -1,2 +1,4 @@

+git.sophuwu.com/gophuwu v1.0.2 h1:cqYmVixQUmUGeKaJokpW1Pl7xTRBGrRiPy2xamOyW1o= +git.sophuwu.com/gophuwu v1.0.2/go.mod h1:KdwrRmjOD1XRyB/+kZAxn1TH956+DCUyG/V7gDNzATQ= github.com/boombuler/barcode v1.0.2 h1:79yrbttoZrLGkL/oOI8hBrUKucwOL0oOjUgEguGMcJ4= github.com/boombuler/barcode v1.0.2/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
M qr.goqr.go

@@ -1,11 +1,17 @@

package qrstr /* - * This file is to turn strings into unicode or html qr codes. + * File: qr.go * Author: sophuwu <sophie@sophuwu.com> - * Feel free to use this code in any way you want. + * License: MIT + + * qrstr + * qrstr provides simple standardised interface + various formats, including text, HTML, and SVG. It supports different error + correction levels and allows for optional headers to be displayed above the QR + code. * The header will display the string above the qr code. - */ +*/ import ( "fmt"