forked from yeqown/go-qrcode
/
writer.go
80 lines (61 loc) · 1.62 KB
/
writer.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package terminal
import (
"github.com/SmokingTheGoaT/go-qrcode-custom/v2"
termbox "github.com/nsf/termbox-go"
)
var _ qrcode.Writer = (*Writer)(nil)
// Writer implements qrcode.Writer based on termbox to print QRCode into
// terminal / console.
type Writer struct{}
func New() *Writer {
w := &Writer{}
w.init()
return w
}
func (w Writer) init() {
err := termbox.Init()
if err != nil {
panic(err)
}
termbox.SetInputMode(termbox.InputEsc)
termbox.SetOutputMode(termbox.Output256)
}
func (w Writer) preDraw(width, height, padding int, bg termbox.Attribute) {
for i := 0; i < width+2*padding; i++ {
for j := 0; j < height+2*padding; j++ {
w.drawBlock(i, j, 0, bg, bg)
}
}
}
// drawBlock draws a block at (x, y) with fg and bg colors.
// each block takes 2 times width of one character terminal, it looks like: ██
func (w Writer) drawBlock(x, y, padding int, fg termbox.Attribute, bg termbox.Attribute) {
x1, y1 := x*2+2*padding, y+padding
x2, y2 := x1+1, y1
termbox.SetCell(x1, y1, '█', fg, bg)
termbox.SetCell(x2, y2, '█', fg, bg)
}
func (w Writer) Write(mat qrcode.Matrix) error {
//width, height, whratio := terminalSize()
//_ = width
//_ = height
//_ = whratio
ww, hh := mat.Width(), mat.Height()
bg := termbox.ColorWhite
fg := termbox.ColorBlack
padding := 1
w.preDraw(ww, hh, padding, bg)
mat.Iterate(qrcode.IterDirection_ROW, func(x int, y int, state qrcode.QRValue) {
if state.IsSet() {
fg = termbox.ColorBlack
} else {
fg = termbox.ColorWhite
}
w.drawBlock(x, y, padding, fg, bg)
})
return termbox.Flush()
}
func (w Writer) Close() error {
termbox.Close()
return nil
}