| 1 |
package terminal |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/yeqown/go-qrcode/v2" |
| 5 |
|
| 6 |
"github.com/mattn/go-runewidth" |
| 7 |
termbox "github.com/nsf/termbox-go" |
| 8 |
) |
| 9 |
|
| 10 |
var _ qrcode.Writer = (*Writer)(nil) |
| 11 |
|
| 12 |
// Writer implements qrcode.Writer based on termbox to print QRCode into |
| 13 |
// terminal / console. |
| 14 |
type Writer struct{} |
| 15 |
|
| 16 |
func New() *Writer { |
| 17 |
w := &Writer{} |
| 18 |
w.init() |
| 19 |
|
| 20 |
return w |
| 21 |
} |
| 22 |
|
| 23 |
func (w Writer) init() { |
| 24 |
err := termbox.Init() |
| 25 |
if err != nil { |
| 26 |
panic(err) |
| 27 |
} |
| 28 |
|
| 29 |
termbox.SetInputMode(termbox.InputEsc) |
| 30 |
termbox.SetOutputMode(termbox.Output256) |
| 31 |
} |
| 32 |
|
| 33 |
func (w Writer) preDraw(width, height, padding int, bg termbox.Attribute) { |
| 34 |
for i := 0; i < width+2*padding; i++ { |
| 35 |
for j := 0; j < height+2*padding; j++ { |
| 36 |
w.drawBlock(i, j, 0, bg, bg) |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// drawBlock draws a block at (x, y) with fg and bg colors. |
| 42 |
// each block takes 2 times width of one character terminal, it looks like: ██ |
| 43 |
func (w Writer) drawBlock(x, y, padding int, fg termbox.Attribute, bg termbox.Attribute) { |
| 44 |
x1, y1 := x*2+2*padding, y+padding |
| 45 |
x2, y2 := x1+1, y1 |
| 46 |
|
| 47 |
termbox.SetCell(x1, y1, '█', fg, bg) |
| 48 |
termbox.SetCell(x2, y2, '█', fg, bg) |
| 49 |
} |
| 50 |
|
| 51 |
func (w Writer) Write(mat qrcode.Matrix) error { |
| 52 |
//width, height, whratio := terminalSize() |
| 53 |
//_ = width |
| 54 |
//_ = height |
| 55 |
//_ = whratio |
| 56 |
|
| 57 |
ww, hh := mat.Width(), mat.Height() |
| 58 |
bg := termbox.ColorWhite |
| 59 |
fg := termbox.ColorBlack |
| 60 |
|
| 61 |
padding, curRow := 1, 0 |
| 62 |
w.preDraw(ww, hh, padding, bg) |
| 63 |
mat.Iterate(qrcode.IterDirection_ROW, func(x int, y int, state qrcode.QRValue) { |
| 64 |
if state.IsSet() { |
| 65 |
fg = termbox.ColorBlack |
| 66 |
} else { |
| 67 |
fg = termbox.ColorWhite |
| 68 |
} |
| 69 |
|
| 70 |
w.drawBlock(x, y, padding, fg, bg) |
| 71 |
curRow = y |
| 72 |
}) |
| 73 |
|
| 74 |
printTip(curRow + 2*padding + 1 + 1) |
| 75 |
return hold() |
| 76 |
} |
| 77 |
|
| 78 |
func printTip(y int) { |
| 79 |
tip := "Press any key to quit." |
| 80 |
x := 0 |
| 81 |
for _, r := range tip { |
| 82 |
w := runewidth.RuneWidth(r) |
| 83 |
if w == 0 || (w == 2 && runewidth.IsAmbiguousWidth(r)) { |
| 84 |
w = 1 |
| 85 |
} |
| 86 |
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault) |
| 87 |
x += w |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func hold() error { |
| 92 |
if err := termbox.Flush(); err != nil { |
| 93 |
return err |
| 94 |
} |
| 95 |
|
| 96 |
wait: |
| 97 |
for { |
| 98 |
switch ev := termbox.PollEvent(); ev.Type { |
| 99 |
case termbox.EventKey: |
| 100 |
break wait |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return nil |
| 105 |
} |
| 106 |
|
| 107 |
func (w Writer) Close() error { |
| 108 |
termbox.Close() |
| 109 |
|
| 110 |
return nil |
| 111 |
} |