summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/components/qr/qr.go
blob: 233bcf524189d2bdf2edfc1a9b433af06014cec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package qr

import (
	"strings"

	"github.com/sst/opencode/internal/styles"
	"github.com/sst/opencode/internal/theme"
	"rsc.io/qr"
)

var tops_bottoms = []rune{' ', '▀', '▄', '█'}

// Generate a text string to a QR code, which you can write to a terminal or file.
func Generate(text string) (string, int, error) {
	code, err := qr.Encode(text, qr.Level(0))
	if err != nil {
		return "", 0, err
	}

	t := theme.CurrentTheme()
	if t == nil {
		return "", 0, err
	}

	// Create lipgloss style for QR code with theme colors
	qrStyle := styles.NewStyle().Foreground(t.Text()).Background(t.Background())

	var result strings.Builder

	// content
	for y := 0; y < code.Size-1; y += 2 {
		var line strings.Builder
		for x := 0; x < code.Size; x += 1 {
			var num int8
			if code.Black(x, y) {
				num += 1
			}
			if code.Black(x, y+1) {
				num += 2
			}
			line.WriteRune(tops_bottoms[num])
		}
		result.WriteString(qrStyle.Render(line.String()) + "\n")
	}

	// add lower border when required (only required when QR size is odd)
	if code.Size%2 == 1 {
		var borderLine strings.Builder
		for range code.Size {
			borderLine.WriteRune('▀')
		}
		result.WriteString(qrStyle.Render(borderLine.String()) + "\n")
	}

	return result.String(), code.Size, nil
}