added an xterm auto color mode for terminal output this way if outputting to a terminal, no need to know the colour scheme. default light/dark theme still available for non-terminal usage.
sophuwu sophie@skisiel.com
Thu, 08 May 2025 19:45:16 +0200
1 files changed,
17 insertions(+),
1 deletions(-)
jump to
M
qr.go
→
qr.go
@@ -183,7 +183,7 @@ output = strings.TrimSuffix(output, prefix) + suffix
} if hashead { - output += string(whole) + pad(dx+2, lower) + string(whole) + "\n" + output += pad(dx+4, wr) + "\n" } else { output += pad(dx+2, wr) + "\n" }@@ -262,6 +262,9 @@ TextLightMode encoderType = 1
// HTMLMode makes qr codes for embedding in HTML documents or web pages. // Generates a table using HTML tags, does not require a monospace font. HTMLMode encoderType = 2 + // TextXTermMode makes qr codes for printing on xterm terminals with auto color. + // MUST BE PRINTED/DISPLAYED USING A MONOSPACE FONT. + TextXTermMode encoderType = 3 // ErrorCorrection7Percent indicates 7% of lost data can be recovered, makes the qr code smaller ErrorCorrection7Percent errorCorrectionLevel = 0@@ -291,6 +294,19 @@ q.strFunc = text
break case HTMLMode: q.strFunc = html + break + case TextXTermMode: + q.rc = &darkMode + q.strFunc = func(rc *runeCol, code *image.Image, headers *[]string) (string, error) { + s, e := text(rc, code, headers) + if e != nil { + return "", e + } + front := "\033[40;97m" + back := "\033[0m\n" + s = strings.ReplaceAll(s, "\n", back+front) + return front + strings.TrimSuffix(s, front), nil + } break default: return nil, fmt.Errorf("invalid encoder type: %d", encoderType)