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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
package qrcodeTerminal
import (
"fmt"
"github.com/skip2/go-qrcode"
"github.com/mattn/go-colorable"
"image/png"
nbytes "bytes"
)
type consoleColor string
type consoleColors struct {
NormalBlack consoleColor
NormalRed consoleColor
NormalGreen consoleColor
NormalYellow consoleColor
NormalBlue consoleColor
NormalMagenta consoleColor
NormalCyan consoleColor
NormalWhite consoleColor
BrightBlack consoleColor
BrightRed consoleColor
BrightGreen consoleColor
BrightYellow consoleColor
BrightBlue consoleColor
BrightMagenta consoleColor
BrightCyan consoleColor
BrightWhite consoleColor
}
type qrcodeRecoveryLevel qrcode.RecoveryLevel
type qrcodeRecoveryLevels struct {
Low qrcodeRecoveryLevel
Medium qrcodeRecoveryLevel
High qrcodeRecoveryLevel
Highest qrcodeRecoveryLevel
}
var (
ConsoleColors consoleColors = consoleColors{
NormalBlack: "\033[38;5;0m \033[0m",
NormalRed: "\033[38;5;1m \033[0m",
NormalGreen: "\033[38;5;2m \033[0m",
NormalYellow: "\033[38;5;3m \033[0m",
NormalBlue: "\033[38;5;4m \033[0m",
NormalMagenta: "\033[38;5;5m \033[0m",
NormalCyan: "\033[38;5;6m \033[0m",
NormalWhite: "\033[38;5;7m \033[0m",
BrightBlack: "\033[48;5;0m \033[0m",
BrightRed: "\033[48;5;1m \033[0m",
BrightGreen: "\033[48;5;2m \033[0m",
BrightYellow: "\033[48;5;3m \033[0m",
BrightBlue: "\033[48;5;4m \033[0m",
BrightMagenta: "\033[48;5;5m \033[0m",
BrightCyan: "\033[48;5;6m \033[0m",
BrightWhite: "\033[48;5;7m \033[0m"}
QRCodeRecoveryLevels = qrcodeRecoveryLevels{
Low: qrcodeRecoveryLevel(qrcode.Low),
Medium: qrcodeRecoveryLevel(qrcode.Medium),
High: qrcodeRecoveryLevel(qrcode.High),
Highest: qrcodeRecoveryLevel(qrcode.Highest)}
)
type QRCodeString string
func (v *QRCodeString) Print() {
fmt.Fprint(outer, *v)
}
type qrcodeTerminal struct {
front consoleColor
back consoleColor
level qrcodeRecoveryLevel
}
func (v *qrcodeTerminal) Get(content interface{}) (result *QRCodeString) {
var qr *qrcode.QRCode
var err error
if t, ok := content.(string); ok {
qr, err = qrcode.New(t, qrcode.RecoveryLevel(v.level))
} else if t, ok := content.([]byte); ok {
qr, err = qrcode.New(string(t), qrcode.RecoveryLevel(v.level))
}
if qr != nil && err == nil {
data := qr.Bitmap()
result = v.getQRCodeString(data)
}
return
}
func (v *qrcodeTerminal) Get2(bytes []byte) (result *QRCodeString) {
data, err := parseQR(bytes)
if err == nil {
result = v.getQRCodeString(data)
}
return
}
func New2(front, back consoleColor, level qrcodeRecoveryLevel) *qrcodeTerminal {
obj := qrcodeTerminal{front: front, back: back, level: level}
return &obj
}
func New() *qrcodeTerminal {
front, back, level := ConsoleColors.BrightBlack, ConsoleColors.BrightWhite, QRCodeRecoveryLevels.Medium
return New2(front, back, level)
}
func (v *qrcodeTerminal) getQRCodeString(data [][]bool) (result *QRCodeString) {
str := ""
for ir, row := range data {
lr := len(row)
if ir == 0 || ir == 1 || ir == 2 ||
ir == lr-1 || ir == lr-2 || ir == lr-3 {
continue
}
for ic, col := range row {
lc := len(data)
if ic == 0 || ic == 1 || ic == 2 ||
ic == lc-1 || ic == lc-2 || ic == lc-3 {
continue
}
if col {
str += fmt.Sprint(v.front)
} else {
str += fmt.Sprint(v.back)
}
}
str += fmt.Sprintln()
}
obj := QRCodeString(str)
result = &obj
return
}
func parseQR(bytes []byte) (data [][]bool, err error) {
r := nbytes.NewReader(bytes)
img, err := png.Decode(r)
if err == nil {
rect := img.Bounds()
mx, my := rect.Max.X, rect.Max.Y
data = make([][]bool, mx)
for x := 0; x < mx; x++ {
data[x] = make([]bool, my)
for y := 0; y < my; y++ {
c := img.At(x, y)
r, _, _, _ := c.RGBA()
data[x][y] = r == 0
}
}
}
return
}
var outer = colorable.NewColorableStdout()
|