Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions writer/terminal/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terminal
import (
"github.com/yeqown/go-qrcode/v2"

"github.com/mattn/go-runewidth"
termbox "github.com/nsf/termbox-go"
)

Expand Down Expand Up @@ -57,20 +58,50 @@ func (w Writer) Write(mat qrcode.Matrix) error {
bg := termbox.ColorWhite
fg := termbox.ColorBlack

padding := 1
padding, curRow := 1, 0
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)
curRow = y
})

return termbox.Flush()
printTip(curRow + 2*padding + 1 + 1)
return hold()
}

func printTip(y int) {
tip := "Press any key to quit."
x := 0
for _, r := range tip {
w := runewidth.RuneWidth(r)
if w == 0 || (w == 2 && runewidth.IsAmbiguousWidth(r)) {
w = 1
}
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault)
x += w
}
}

func hold() error {
if err := termbox.Flush(); err != nil {
return err
}

wait:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
break wait
}
}

return nil
}

func (w Writer) Close() error {
Expand Down